python - How to access a method in one inherited tkinter class from another inherited tkinter class -
i've programmed using tkinter before, did long procedural gui class implemented other non gui classes i've created. time wanted using more oop making more modular.
i ran problem, i've searched answers , haven't found any, means it's either easy or i'm wrong. created inherited classes tk.labelframe , created gui widgets in them. have methods manipulate widgets in classes can't figure out how execute function in inherited class, partly because can't figure out how correctly instantiate object other class (which have tkinter ('parent') objects parameters).
would overloading constructors? i've seen @classmethods , *args, **kwargs haven't acted on them i'm not sure if that's right route either. there's debate best/correct way implement overloaded constructor in python. i'm stumped apropos i'm trying accomplish...
thanks
#python 2.7 on win7 import tkinter tk class testing(tk.labelframe): buttonwidth = 10 def __init__(self, parent): self.parent=parent #results = results(???) #<-- don't know how instantiate results object tk.labelframe.__init__(self, self.parent, text="test operations", padx=10, pady=10, ) self.taskbutton = tk.button( self, text="do task", width=self.buttonwidth, command=self.doatask, ) self.taskbutton.pack() def doatask(self): #want execute function in results.getresult() don't know how #results.getresults() #<--what want print("place holder") class results(tk.labelframe): def __init__(self, parent): self.parent = parent tk.labelframe.__init__(self, self.parent, text="visual results") self.resultlbl = tk.label(self, text="result") self.resultlbl.pack() def getresult(self): self.resultlbl.configure(bg='yellow') class application(tk.frame): def __init__(self, parent): self.parent = parent tk.frame.__init__(self, self.parent) self.testing = testing(self.parent) self.results = results(self.parent) self.testing.pack(fill=tk.x) self.results.pack(fill=tk.x) if __name__ == "__main__": root = tk.tk() root.title("modular gui app") application(root).pack() root.mainloop()
i'd recommend sticking instance variables, created each individual object, unlike class variables shared among of class's instantiations - prepend variable names self.
(e.g. self.results
). also, stick naming conventions don't have testing
class , testing
object of class.
you instantiate objects according __init__
. results
class has __init__
defined def __init__(self, parent):
, needs parent. if want have same parent testing
object created it, results = results(parent)
. however, don't want (see below).
a problem encountered after making above change application
class instantiated own results
object, , being displayed, not 1 created testing
object. refer object instead of creating new one. pass application
object each of these classes can refer each other. now, having said that, it's better have each class know little other classes possible, making change in 1 class doesn't require changes in other classes.
the following code make label yellow when click button.
import tkinter tk class testing(tk.labelframe): def __init__(self, parent, main): self.buttonwidth = 10 self.parent=parent self.main = main # save instantiating class tk.labelframe.__init__(self, self.parent, text="test operations", padx=10, pady=10 ) self.taskbutton = tk.button( self, text="do task", width=self.buttonwidth, command=self.doatask, ) self.taskbutton.pack() def doatask(self): #want execute function in results.getresult() don't know how self.main.results.getresult() #<--what can class results(tk.labelframe): def __init__(self, parent, main): self.parent = parent self.main = main # save instantiating class tk.labelframe.__init__(self, self.parent, text="visual results") self.resultlbl = tk.label(self, text="result") self.resultlbl.pack() def getresult(self): self.resultlbl.config(bg='yellow') class application(tk.frame): def __init__(self, parent): self.parent = parent tk.frame.__init__(self, self.parent) self.testing = testing(self.parent, self) self.results = results(self.parent, self) self.testing.pack(fill=tk.x) self.results.pack(fill=tk.x) if __name__ == "__main__": root = tk.tk() root.title("modular gui app") application(root).pack() root.mainloop()
Comments
Post a Comment