Python tkinter module has three methods of class inheritance: pythontkinter
This article describes three methods of class inheritance in the Python tkinter module. We will share this with you for your reference. The details are as follows:
There are three methods to inherit tkinter class.
Note that these inherited running methods
1. inherit objects
1. Lay tk. Frame to the parent:
Note:
self.rootframe = tk.Frame(parent)
tk.Label(self.rootframe)
Import tkinter as tkclass MyApp (object): def _ init _ (self, parent): self. rootframe = tk. frame (parent) self. rootframe. pack () self. setupUI () def setupUI (self): tk. label (self. rootframe, text = 'tag '). pack () if _ name _ = '_ main _': root = tk. tk () MyApp (root) # Pay attention to this sentence as root. mainloop ()
2. directly use root
Note:
self.root = parent
tk.Label(self.root)
Import tkinter as tkclass MyApp (object): def _ init _ (self, parent, ** kwargs): self. root = parent self. root. config (** kwargs) self. setupUI () def setupUI (self): tk. label (self. root, text = 'tag '). pack () if _ name _ = '_ main _': root = tk. tk () app = test (root) root. mainloop ()
2. inherit tk. Tk
Import tkinter as tkclass MyApp (tk. tk): def _ init _ (self): super (). _ init _ () self. setupUI () def setupUI (self): tk. label (self, text = 'tag '). pack () if _ name _ = '_ main _': MyApp (). mainloop ()
3. inherit tk. Frame
There are two situations:
1. parent
Import tkinter as tkclass MyApp (tk. frame): def _ init _ (self, parent = None): super (). _ init _ (parent) self. pack () self. setupUI () def setupUI (self): tk. label (self, text = 'tag '). pack () if _ name _ = '_ main _': MyApp (tk. tk ()). mainloop () # MyApp (). mainloop () # You can also
Note:self.pack()
2. No parent
Import tkinter as tkclass MyApp (tk. frame): def _ init _ (self): super (). _ init _ () self. pack () self. setupUI () def setupUI (self): tk. label (self, text = 'tag '). pack () if _ name _ = '_ main _': MyApp (). mainloop ()