Calling Functions from a Tkinter Frame to Another

Tkinter call function in another class

Since you're instantiating all your classes in a loop, the easiest way to add references between them is probably to do it after that loop. Remove those references from inside the class, as they won't be valid until after instantiation. Note how button3 is no longer created or packed in StartPage, but rather in SampleApp.

for F in (StartPage, PageOne, PageTwo):
...

self.frames['StartPage'].pageone = self.frames['PageOne']
self.frames['StartPage'].button3 = tk.Button(
text='Calling msg in another class',
command=self.frames['StartPage'].pageone.msg) #<-- my mod
self.frames['StartPage'].button3.pack() #<-- my mod

Python Tkinter GUI Frame: How to call a class method from inside a function of another class?

show_frame is a method on the controller. You simply need to save a reference to the controller, and call it from anywhere. This is the entire purpose of the controller class - to control access to the other windows.

The first step is to modify your classes to save a reference to the controller:

class Login(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
...

class WelcomePage(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
...

Now, you can call show_frame wherever you want:

if actNum == act_num and pinNum == pin_num:
...
self.controller.show_frame(WelcomePage)
...

For more information on the controller see this answer: https://stackoverflow.com/a/32865334/7432

Calling functions from another class in Tkinter

We have no idea what XY_Tk contains so don't know what it is supposed to do I just answered a similar question at Adding or deleting tkinter widgets from within other modules which might help.

Changing a tkinter frame from inside a method/function other than __init__

I have found a fix

class PageOne (tk.Frame):
def __init__ (self, parent, controller):
tk.Frame.__init__(self, parent)
lbl = tk.Label(self, text="Page 1")
lbl.pack()

button = tk.Button(self, text="switch", command=lambda: self.launch(controller)
button.pack()

def launch (self, cont):
cont.showFrame(PageTwo)

Now I've made a function (launch) in the PageOne class which takes self and cont (the controller) as the parameters. You will then pass the controller from init into the launch function when you press the button.

Hope this helps some on. 'Cause personally I've been stumped on this one for months.

UPDATE

After months of this answer being up, i have now found a new way, rather than always passing the controller as a parameter to each function you with to use in each page class, in the initiation of the class (__ init __), you can declare a variable to the object that holds the controller, heres an example:

class PageTwo(tk.Frame):
def __init__ (self, parent, controller):
self.controller = controller

def doSomething (self):
self.controller.doSomething()

by referencing the controller you can run any class within the controller class, you can also get variable data and change it.

Changing a Tkinter frame from a different frame

The error is given in the stack trace. The 'testText' variable is not currently an instance variable of the PageOne object. You should set self.testText = Text(self, height=2, width=30)



Related Topics



Leave a reply



Submit