'''三种Options的方式'''
Label(self,text="抗战疫情",width=20,height=2,bg="black",fg="white")
以上这种其实是对参数中,**kw的处理;
self.btn01["bg"]="red"
self.btn01.config(fg="red",bg="blue")
以上这种是cnf={}使用
以上几种传参后,都是调用同一个方法进行合并(可以查看源码):
if kw:
cnf = _cnfmerge((cnf, kw))
总共有这么多Options
Construct a label widget with the parent MASTER.
STANDARD OPTIONS
activebackground, activeforeground, anchor,
background, bitmap, borderwidth, cursor,
disabledforeground, font, foreground,
highlightbackground, highlightcolor,
highlightthickness, image, justify,
padx, pady, relief, takefocus, text,
textvariable, underline, wraplength
WIDGET-SPECIFIC OPTIONS
height, state, width
以下是今天的代码:
from tkinter import *
from tkinter import messagebox
'''三种Options的方式'''
#Label(self,text="抗战疫情",width=20,height=2,bg="black",fg="white")
#self.btn01["command"] = self.songhua
#self.btn01.config(fg="red",bg="blue")
#总共有这么多Options
"""Construct a label widget with the parent MASTER.
STANDARD OPTIONS
activebackground, activeforeground, anchor,
background, bitmap, borderwidth, cursor,
disabledforeground, font, foreground,
highlightbackground, highlightcolor,
highlightthickness, image, justify,
padx, pady, relief, takefocus, text,
textvariable, underline, wraplength
WIDGET-SPECIFIC OPTIONS
height, state, width
"""
#认识传参的办法,可以有cnf={}, **kw
#cnf={},这种方式就是可以将
class Application(Frame):
"""一个经典的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="字体颜色",width=20,height=2,bg="black",fg="white")
self.label01.pack()
#设成全局变量
global v1
v1= StringVar()
self.entry01 = Entry(self,textvariable=v1)
self.entry01.pack()
v1.set("black")
"""创建组件"""
self.btn01 = Button(self,text="点击送花,变色")
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朵花")
self.btn01.config(fg=v1.get(),bg="blue")
if __name__=='__main__':
root = Tk()
root.geometry("400x400+200+300")
root.title("一个经典的GUI程序类测试")
app = Application(master=root)
root.mainloop()
以下是今天代码的效果:
image.png
然后“点击后”
image.png