from tkinter import *
from tkinter import messagebox
class Application(Frame):
def __init__(self,master=None):
super().__init__(master)
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=15,height=1,
bg="red",fg="blue",font=("宋体","30"))
self.label02.pack()
#显示图像
global photo #因为photo为局部变量 必须声明为全局变量,否则执行完语句,图像自动销毁,不会显示
photo=PhotoImage(file="image/123.gif")#先创建一个photo容器
self.label03=Label(self,image=photo)
self.label03.pack()
#显示多行文本
self.label04=Label(self,text="姬昊余\n吕倩倩",borderwidth="10",relief="groove"
,justify="left")
self.label04.pack()
#justify文本对齐方式 relief边框样式 borderwidth边框宽度
#创建一个送花按钮
self.btn01=Button()
self.btn01["text"]="点击送花"
self.btn01["command"]=self.songhua
#songhua后边加括号就成了自动执行 不加括号为触发事件
self.btn01.pack()
def songhua(self):
messagebox.showinfo("送花","送你一朵小花花")
root=Tk()
root.geometry("400x200+200+300")
root.title("一个经典的GUI")
app=Application(root)
root.mainloop()
老师 在这个createWidget方法中创建组件的时候 为什么前面都要加一个self. 为什么不可以直接创建 这样做有什么作用