python 番茄计时 50+10

参考这篇文章,修改了位置和时间

exe文件在这

# 生成可执行文件
# 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值