Posted in Python onAugust 08, 2017
本文实例讲述了Python tkinter模块中类继承的三种方式。分享给大家供大家参考,具体如下:
tkinter class继承有三种方式。
提醒注意这几种继承的运行方式
一、继承 object
1.铺tk.Frame给parent:
说明:
self.rootframe = tk.Frame(parent)
tk.Label(self.rootframe)
import tkinter as tk class MyApp(object): def __init__(self, parent): self.rootframe = tk.Frame(parent) self.rootframe.pack() self.setupUI() def setupUI(self): tk.Label(self.rootframe, text='标签').pack() if __name__ == '__main__': root = tk.Tk() MyApp(root) # 注意这句 root.mainloop()
2.直接使用root
说明:
self.root = parent
tk.Label(self.root)
import tkinter as tk class MyApp(object): def __init__(self, parent, **kwargs): self.root = parent self.root.config(**kwargs) self.setupUI() def setupUI(self): tk.Label(self.root, text = '标签').pack() if __name__ == '__main__': root = tk.Tk() app = test(root) root.mainloop()
二、继承 tk.Tk
import tkinter as tk class MyApp(tk.Tk): def __init__(self): super().__init__() self.setupUI() def setupUI(self): tk.Label(self, text='标签').pack() if __name__ == '__main__': MyApp().mainloop()
三、继承 tk.Frame
分两种情况
1.有parent
import tkinter as tk class MyApp(tk.Frame): def __init__(self, parent=None): super().__init__(parent) self.pack() self.setupUI() def setupUI(self): tk.Label(self, text='标签').pack() if __name__ == '__main__': MyApp(tk.Tk()).mainloop() #MyApp().mainloop() # 也可以这样
注意: self.pack()
2.没有parent
import tkinter as tk class MyApp(tk.Frame): def __init__(self): super().__init__() self.pack() self.setupUI() def setupUI(self): tk.Label(self, text='标签').pack() if __name__ == '__main__': MyApp().mainloop()
希望本文所述对大家Python程序设计有所帮助。
Python tkinter模块中类继承的三种方式分析
- Author -
罗兵声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@