1、定義class類,繼承Frame
2、在類里面使用init
3、建立一個(gè)createWidget
4、然后在Widget里面創(chuàng)建button\label
5、在button里面建立綁定事件;
6、還定義事件;
7、Label,可以顯示普通文字
8、Label,也可以顯示圖片,目前據(jù)說Tk只支持gif格式,我早上使用了jpg還會報(bào)錯(cuò)。
9、root.destroy,知道消毀程序;
from tkinter import *
from tkinter import messagebox
class Application(Frame):
"""一個(gè)經(jīng)典的GUI程序類的寫法"""
def __init__(self,master=None):
super().__init__(master) # super()代表的是父類的定義 ,而不是父類的對像
self.master = master
self.pack()
self.createWidget()
def createWidget(self):
#顯示圖像
global photo #定義為全局變量
photo = PhotoImage(file="img/aa.gif")
self.label03 =Label(self,image=photo)
self.label03.pack()
self.label01 = Label(self)
self.label01 = Label(self,text="抗戰(zhàn)疫情",width=20,height=2,bg="black",fg="white")
self.label01.pack()
"""創(chuàng)建組件"""
self.btn01 = Button(self,text="點(diǎn)擊送花")
self.btn01["command"] = self.songhua
self.btn01.pack()
self.btnQuit=Button(self,text="退出",command=root.destroy)
self.btnQuit.pack()
def songhua(self):
messagebox.showinfo("送花","送你99朵花")
if __name__=='__main__':
root = Tk()
root.geometry("400x400+200+300")
root.title("一個(gè)經(jīng)典的GUI程序類測試")
app = Application(master=root)
root.mainloop()

image.png