学习记录:python tkinter按钮触发显示时间

#导入tkinter库,并设置别名为tk
import tkinter as tk
# 创建Tk对象,Tk代表窗口
root =tk.Tk()
# 设置窗口标题
root.title('demo')
# 创建Label对象,第一个参数指定该Label放入root
 
var=tk.StringVar()
w =tk.Label(root, text=" ",textvariable=var,fg='blue',font=("微软雅黑",25))
var.set("time")
# 调用pack进行布局
w.pack()

#导入时间库
import time
def gettime():
    var.set(time.strftime("%H:%M:%S"))  # 获取当前时间
    root.after(1000, gettime)  # 每隔1s调用函数 gettime 自身获取时间

#测试用途
goBtn = tk.Button(text="测试",command=gettime,fg='blue',font=("微软雅黑",25))
goBtn.pack()
 
# 启动主窗口的消息循环
root.mainloop()

猜你喜欢

转载自blog.csdn.net/qq_26086231/article/details/113181600