tkinter - Python - Calling a function from another class -


i started learning python, read tutorial switching windows using tkinter. in tutorial, guy switched windows inside button in __init__ using lambda, want go through function before switching windows. function runs inside class windowa , needs call function inside class glapp. after runs function, if successful, it's supposed open windowb.

i error if try call show_frame inside function in windowa.

--controller not defined 

can please explain thought process behind getting call show_frame function inside windowa function, i'd appreciate bunch!

class glapp(tk.tk):      def __init__(self, *args, **kwargs):              self.frames = {}          f in (windowa, windowb):              frame = f(container, self)              self.frames[f] = frame              frame.grid(row=0, column=0, sticky="nsew")          def show_frame(self, cont):             frame = self.frames[cont]             frame.tkraise()   class windowa(tk.frame):      def __init__(self, parent, controller):          tk.frame.__init__(self,parent)          #window styling , function calling          self.attempt_login = tk.button(self,text="login",command= self.function)          def function(self):               try:                  trysomething               else:                  controller.show_frame(windowb) 

what tutorial doesn't tell you should save instance of controller can use in every function of page classes. see original code copied tutorial, see https://stackoverflow.com/a/7557028/7432

in you'll see 1 of first things each page class save controller. once can use self.controller anywhere in page class.

also, in code you're nesting function inside of __init__. there no reason in case. move function out 1 level, , write this:

class windowa(tk.frame):     def __init__(self, parent, controller):         tk.frame.__init__(self,parent)         self.controller = controller         ...      def function(self):          try:             trysomething          else:             self.controller.show_frame(windowb) 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo