经典的面向对象写法:
"""测试一个经典的GUI程序的学法,使用面向对象的方式""" from tkinter import * from tkinter import messagebox class Application(Frame): """一个经典的GUI程序的类的写法""" def __init__(self, master=None): super().__init__(master) #super代表父类的定义,而不是父类的对象 self.master=master self.pack() self.createWidget() def createWidget(self): """创建组件""" self.btn01 = Button(self) self.btn01["text"] = "点击送花" self.btn01.pack() self.btn01["command"] = self.songhua self.btn02 = Button(self) self.btn02["text"] = "点击关注" self.btn02.pack() self.btn02["command"] = self.libm #创建一个退出按钮 self.btnQuit = Button(self,text="退出",command=root.destroy) self.btnQuit.pack() def songhua(self): messagebox.showinfo("送花","送你99朵玫瑰花") def libm(self): messagebox.showinfo("limb","李逼姆") if __name__ == '__main__': root = Tk() root.geometry("400x100+200+300") root.title("一个经典的GUI程序类的测试") app = Application(master=root) root.mainloop()
label用法
"""测试一个经典的GUI程序的学法,使用面向对象的方式""" from tkinter import * from tkinter import messagebox class Application(Frame): """一个经典的GUI程序的类的写法""" def __init__(self, master=None): super().__init__(master) #super代表父类的定义,而不是父类的对象 self.master=master self.pack() self.createWidget() def createWidget(self): """创建组件""" self.label01 = Label(self,text="百战程序员",width=10,height=2, bg="black",fg="white") self.label01.pack() self.label02 = Label(self,text="郑益天才",width=10,height=2, bg="blue",fg="white",font=("黑体",30)) self.label02.pack() self.label03 = Label(self,text="AAAA BBB CCC", borderwidth=5,relief="solid",justify="left") self.label03.pack() if __name__ == '__main__': root = Tk() root.geometry("400x200+200+300") root.title("一个经典的GUI程序类的测试") app = Application(master=root) root.mainloop()