一、问题
视频看了好几遍,有太多太多太多不明白的地方:我将问题和我的理解一一描述,这样老师可以知道我到底为什么不明白,可以更好地讲解。
代码如下:
#encoding=utf-8
"""测试一个经典的GUI程序的写法,使用面向对象的方式"""
from tkinter import *
from tkinter import messagebox
class Applictoon(Frame):
"""一个经典的GUI程序的类的写法"""
def __init__(self, master = None):
super().__init__(master) # super()代表的是父类的定义,而不是父类对象
self.master = master
self.pack()
self.createWidget()
def createWidget(self):
"""创建组件"""
self.bnt01 = Button(self)
self.bnt01['text'] = "点我送花"
self.bnt01.pack()
self.bnt01['command'] = self.songhua
# 创建一个退出按钮
self.btnQuit = Button(self, text = '退出', command = root.destroy)
self.btnQuit.pack()
def songhua(self):
messagebox.showinfo('送花', '送你99朵玫瑰花')
if __name__ == '__main__':
root = Tk()
root.geometry('500x300+200+300')
root.title("一个经典的GUI程序类的测试")
app = Applictoon(master = root)
root.mainloop()
问题1:
已经定义了root这个主窗口,为什么还要在定义一个Application?首先我的理解是:类似于html中的div,root只是一个显示框,类似于html的body在这个body里写内容。application就是它里面的大的框架,来更方便的管理里面的按钮之类的。如下图

问题2:定义application为什么要继承Frame呢?是不是将application定义为一个容器,类似于html中最外层的div
问题3:这里将app和root绑定是为了什么呢?作用什么?我的理解和上面一样,是不是就是为了让app这个主框架在root这个显示框框内显示
super().__init__(master) # super()代表的是父类的定义,而不是父类对象
self.master = master
问题4:command是什么意思?是不是点击这个按钮为一次事件执行一个任务,这个任务就是关闭界面?那么command = root.destory,为什么是root.destroy,不应该是self.destroy呢?我理解的是,如果是self.destroy,则关闭app这个主框架,而root这个显示框框还会继续显示,而root.destory则是直接把root给关掉了。

问题5:
self.bnt01['text'] = "点我送花"
这里是不是就是这个按钮叫什么名字:这个名字是text格式
self.bnt01['command'] = self.songhua
这个是不是就是点击这个按钮,视为一次事件,执行送花这个函数