Python Forum
[Tkinter] Use function from other class (Tkinter)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Use function from other class (Tkinter)
#1
Hi guys,

I would like to use function defined in main class, in another file/class - Rents.py/class Rents_tab(Frame)

MAIN.PY
import pandas as pd
from tkinter import *
from tkinter.ttk import *
from rents import *

class Application(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        #set window size
        parent.geometry("1600x1600")
        #Set the header of application
        parent.title("My first application")
        self.notebook = Notebook(self)
        self.notebook.grid(row=1, column=1)
        #DETERMINING TAB NAMES
        comment_tab = Comments_tab(self.notebook)
        rents_tab = Rents_tab(self.notebook)

        
        #Assign tab names
        self.notebook.add(rents_tab, text="Rents")
        def Load_Data(self):
            excel_file_mhs = pd.read_csv(r'C:\Users\USER\Desktop\project_))\September 2019.csv', encoding='cp1252')
            contr_select = excel_file_mhs.loc[excel_file_mhs['Country Name']== self.entry_country.get()]
            loca_select = contr_select.loc[contr_select['Location Name']== self.entry_location.get()]
            final_select = loca_select.loc[loca_select['Survey Date'] == self.entry_survey.get()]
            return final_select
Rents.py - In that file i want to use the function Load_Data()

import pandas as pd
from tkinter import *
from main import *
from tkinter.ttk import *

class Rents_tab(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        
        
        entry_label_country = Label(self, text="Please provide country: ")
        entry_label_country.grid(row=1, column=1)
        entry_country = Entry(self, width=30)
        entry_country.grid(row=1, column=2)
        entry_country.insert(0, "Poland") #Default displayed text
        
        entry_label_location = Label(self, text="Please provide location: ")
        entry_label_location.grid(row=2, column=1)
        entry_location = Entry(self, width=30)
        entry_location.grid(row=2, column=2)
        entry_location.insert(0, "Warsaw") #Default displayed text

        entry_label_survey = Label(self, text="Survey Date: ")
        entry_label_survey.grid(row=3, column=1)
        entry_survey = Entry(self, width=30)
        entry_survey.grid(row=3, column=2)
        entry_survey.insert(0, "2019") #Default displayed text
        
        # #Buttons
        test_button = Button(self, text="Show Data", command=Application().Load_Data)
        test_button.grid(row=3, column=3)
Could you please guide me, what i am doing wrong?

Using command=Application().Load_Data) gives me output
Error:
TypeError: __init__() missing 1 required positional argument: 'parent'
Using command=Application(self).Load_Data)
or
Using command=Application(parent).Load_Data) gives me output
Error:
AttributeError: 'Rents_tab' object has no attribute 'geometry'
Reply


Messages In This Thread
Use function from other class (Tkinter) - by zarize - Aug-15-2020, 09:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 4,863 Dec-03-2023, 01:50 PM
Last Post: deanhystad
Lightbulb Using Tkinter With Concurrent.Futures / ThreadPoolExecutor Class AaronCatolico1 1 1,418 Dec-14-2022, 08:01 PM
Last Post: deanhystad
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 2,973 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  Tkinter won't run my simple function AthertonH 6 3,743 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,737 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,419 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,920 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,603 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  tkinter get function finndude 2 2,891 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,238 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020