from tkinter import *
class App(Frame): # Frame类
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.subFrm = None
# 放置一个Label
w = Label(self, text='我是主窗体!')
w.pack()
def createSubfrm():
if not self.subFrm:
self.subFrm = SubFrm(self)
self.subFrm.setLabelText() # 计数测试
def showSubfrm():
if self.subFrm:
self.subFrm.deiconify()
self.subFrm.setLabelText() # 计数测试
def hideSubfrm():
if self.subFrm:
self.subFrm.withdraw()
def miniSubfrm():
if self.subFrm:
self.subFrm.iconify()
def destroySubfrm():
if self.subFrm:
self.subFrm.destroy()
self.subFrm = None
b1 = Button(master, text='创建', command=createSubfrm)
b1.pack()
b2 = Button(master, text='显示', command=showSubfrm)
b2.pack()
b2 = Button(master, text='不显示', command=hideSubfrm)
b2.pack()
b3 = Button(master, text='最小化', command=miniSubfrm)
b3.pack()
b4 = Button(master, text='关闭', command=destroySubfrm)
b4.pack()
class SubFrm(Toplevel): # Toplevel类
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.attributes('-topmost',True)
self.labelText = StringVar()
self.labelText.set('1')
# 放置Label
w1 = Label(self, text='我是子窗体!')
w1.pack()
w2 = Label(self, textvariable=self.labelText)
w2.pack()
def setLabelText(self):
self.labelText.set(str(int(self.labelText.get())+1))
if __name__=='__main__':
root = Tk()
app = App(master=root)
app.mainloop()
|