Python tkinter实现全屏电子时钟

全屏电子时钟,接下来,我将用Python tkinter库实现全屏电子时钟。如有问题,望多多指正。

1.导入所需的库

本章,需要导入tkinter,time等模块,代码如下:

import tkinter as tk
import time

2.科技蓝界面开发与日期显示

(1)科技蓝界面制作

root = tk.Tk()
root.title("时间")
root.attributes('-fullscreen', True)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(f'{screen_width}x{screen_height}')
root.configure(bg="#0f4c81")
root.resizable(0,0)
root.title('Time')

(2)日期显示

#显示日期
date = time.strftime("%Y/%m/%d")
tk.Label(root,text = date,bg="#0f4c81",fg='#00FFFF',font=("微软雅黑",50)).place(x=100,y=100)

3.时间实时显示实现

# 获取时间的函数
def gettime():
    # 获取当前时间
    dstr.set(time.strftime("%H:%M:%S"))
    # 每隔 1s 调用一次 gettime()函数来获取时间
    root.after(1000, gettime)
# 生成动态字符串
dstr = tk.StringVar()
# 利用 textvariable 来实现文本变化
lb = tk.Label(root,textvariable=dstr,bg="#0f4c81",fg='#00FFFF',font=("微软雅黑",150))
lb.place(x=500,y=300)

4.退出按钮实现

def exit_root():
    root.destroy()
exit_btn = tk.Button(root,text="退出",command=exit_root)
exit_btn.place(x=int(screen_width)-100,y=int(screen_height)-100)

5.调用时间显示函数与界面运行

# 调用生成时间的函数
gettime()
# 显示窗口
root.mainloop()

6.完整源码分享

import tkinter as tk
import time

root = tk.Tk()
root.title("时间")
root.attributes('-fullscreen', True)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(f'{screen_width}x{screen_height}')
root.configure(bg="#0f4c81")
root.resizable(0,0)
root.title('Time')
#显示日期
date = time.strftime("%Y/%m/%d")
tk.Label(root,text = date,bg="#0f4c81",fg='#00FFFF',font=("微软雅黑",50)).place(x=100,y=100)
# 获取时间的函数
def gettime():
    # 获取当前时间
    dstr.set(time.strftime("%H:%M:%S"))
    # 每隔 1s 调用一次 gettime()函数来获取时间
    root.after(1000, gettime)
# 生成动态字符串
dstr = tk.StringVar()
# 利用 textvariable 来实现文本变化
lb = tk.Label(root,textvariable=dstr,bg="#0f4c81",fg='#00FFFF',font=("微软雅黑",150))
lb.place(x=500,y=300)

def exit_root():
    root.destroy()
exit_btn = tk.Button(root,text="退出",command=exit_root)
exit_btn.place(x=int(screen_width)-100,y=int(screen_height)-100)
# 调用生成时间的函数
gettime()
# 显示窗口
root.mainloop()
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值