from tkinter import *
#设置Button的command调用的函数,var调用set()方法,设置显示内容
def callback():
var.set('Hello,nice to meet you!~')
root = Tk()
frame1 = Frame(root) #存放标签1的框架
frame2 = Frame(root) #存放按钮的框架
var = StringVar() #这个是动态存放字符串的函数,不知道这么理解对不对
var.set('请向大家打个招呼吧,\n说完再去忙别的吧。')
the_Label1 = Label(frame1,textvariable = var,#textvariable 是动态文本对象,text是静态的
justify = LEFT) #justify 是多行对齐属性
the_Label1.pack(side = LEFT)
the_Button = Button(frame2,text = '点我一下!',
fg = 'white',bg = 'black' ,
command = callback) #command 关键的,调用callback函数
the_Button.pack()
#摆放两个框架的位置
frame1.pack(padx = 10,pady = 10)
frame2.pack(padx = 10,pady = 10)
mainloop()
小白学tkinter(按钮Button)
最新推荐文章于 2024-07-20 03:56:26 发布