"""使用面向对象测试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):
"""创建新的组件"""
# 测试Label对象
self.lab01 = Label(self, text='lxh', width=10, height=2, bg='pink', fg='white')
self.lab01.pack()
# 显示图像 tkinter只支持gif
global phooto # 把photo声明称为全局变量,避免销毁图像
photo = PhotoImage(file='')
self.lab02 = Label(self, image=photo)
self.lab02.pack()
# 显示多行文本
self.label03 = Label(self, text="北京李欣辉\n百战程序员\n小李好帅,就是做饭不行",
borderwidth=5, relief="groove", justify="left")
self.label03.pack()
# 按键对象
self.btn01 = Button(self)
self.btn01["text"] = "点击送一朵小发发"
self.btn01.pack()
self.btn01["command"] = self.songhua
# 退出按键
self.btnQuit = Button(self, text="Quit", command=root.destroy)
self.btnQuit.pack()
def songhua(self):
messagebox.showinfo("送一朵小发发~")
if __name__ == "__main__":
root = Tk()
root.geometry("500x400+500+200")
root.title("一个经典的GUI程序类的测试")
app = Application(master=root)
root.mainloop()
老师,点击送花的按钮会报错是为什么?上一节课的代码我没动,就是加了一下Label标签就报错了