# 生成可执行文件
# pyinstaller main.py --onefile --noconsole --noupx
import time
import datetime
import winsound
import tkinter as tk
wtime = 50
rtime = 10
time_end = datetime.datetime.now() + datetime.timedelta(minutes=wtime)
time_zero = datetime.timedelta(seconds=0)
def beep():
global time_end
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
window.geometry('%dx%d+%d+%d' % (w, h, x, int((hs - h) / 2)))
clock.config(text=u"休息时间")
time_end = datetime.datetime.now() + datetime.timedelta(minutes=rtime)
clock.after(1000, tick)
def restart():
global time_end
time_end = datetime.datetime.now() + datetime.timedelta(minutes=wtime)
window.geometry('%dx%d+%d+%d' % (w, h, x, y))
clock.config(command=lambda: root.destroy())
tick()
def pause():
time.sleep(1)
clock.config(text=u"计时开始")
flag = 1
def tick():
global time_end, flag
deltatime = time_end - datetime.datetime.now()
if deltatime < time_zero:
if flag == 1:
flag = 0
clock.config(text=u"计时结束")
clock.config(command=restart)
clock.after(100, beep)
else:
flag = 1
clock.config(text=u"休息结束")
clock.after(100, pause)
else:
deltatime = str(deltatime).split('.')[0]
clock.config(text=deltatime)
# calls itself every 1s
# to update the time display as needed
# after为利用tk自身的事件循环机制进行延时调用
clock.after(1000, tick)
root = tk.Tk() # create a Tk root window
# root.overrideredirect(True)
# -----------------隐藏标题边框 始-------------------- #
root.attributes('-alpha', 0.0) #For icon
#root.lower()
root.iconify()
window = tk.Toplevel(root)
# -----------------设定窗口位置 始-------------------- #
w = 140 # width for the Tk root
h = 45 # height for the Tk root
# get screen width and height
ws = root.winfo_screenwidth() # 屏幕尺寸 宽 x
hs = root.winfo_screenheight() # height of the screen 高 y
# calculate x and y coordinates for the Tk root window
# x = ws - w - 100
# y = hs - h - 60
x = int((ws - w)/ 2)
y = 1
# set the dimensions of the screen and where it is placed
# root.geometry('%dx%d+%d+%d' % (w, h, x, y))
window.geometry('%dx%d+%d+%d' % (w, h, x, y))
# -----------------设定窗口位置 终-------------------- #
window.overrideredirect(1) #Remove border
window.attributes('-topmost', 1)
window.attributes('-alpha', 0.5)
#Whatever buttons, etc
# close = tk.Button(window, text = "Close Window", command = lambda: root.destroy())
# -----------------隐藏标题边框 终-------------------- #
# ----------------- 主要控件设置与主循环 ------------- #
# root.wm_attributes("-topmost", 1) # 窗口置顶
# clock = tk.Label(window, font=('times', 20, 'bold'), bg='green')
clock = tk.Button(window,
font=('times', 20, 'bold'),
bg='#00BFFF',
command=lambda: root.destroy(),
bd=0,
# command = lambda: root.destroy()
)
clock.pack(fill='both', expand=True)
tick()
window.mainloop()
python 番茄计时 50+10
最新推荐文章于 2022-01-21 19:37:45 发布