import datetime
from time import sleep
from tkinter import *
from tkinter import messagebox
from time import sleep
from PIL import Image
root = Tk()
root.wm_attributes('-topmost',1)
root.title("计时器")
root.geometry('300x150') #窗口大小
l1 = Label(root, text="设置时间(分钟)")
l1.pack() #这里的side可以赋值为LEFT RTGHT TOP BOTTOM
xls_text = StringVar()
xls = Entry(root, textvariable = xls_text)
xls_text.set("")
xls.pack()
l2 = Label(root, text="设置提醒内容(非必填)")
l2.pack() #这里的side可以赋值为LEFT RTGHT TOP BOTTOM
Reminder_text = StringVar()
Reminder = Entry(root, textvariable = Reminder_text)
Reminder_text.set("")
Reminder.pack()
def on_check():
NowTime = datetime.datetime.now()
TimeEnd = xls_text.get()
TimeEnd = int(TimeEnd) * 60
Reminder = Reminder_text.get()
if TimeEnd < 1:
print(TimeEnd)
messagebox.showinfo(title='计时提醒', message="时间必须是大于0的正整数")
else:
if Reminder == "":
Reminder = "你的提醒时间到了"
NowTime = str(NowTime)
NowTime = NowTime[0:19]
print("计时开始时间:",NowTime,"现在你可以最小化计时器了,计时结束前关闭计时可能会出现无响应","n提醒窗口可能会因优先级不能置顶显示,不建议再全屏应用中使用")
sleep (TimeEnd)
messagebox.showinfo(title='计时提醒', message=Reminder)
Button(root, text="开始倒计时", command = on_check).pack()
root.mainloop()
运行结果如下:(输入框内容自行设置)