Python實現簡易有效學習時鐘

基於tkinter,python自帶的圖形界面框架

優點:防止拖延,有效學習,輕鬆重複使用!!

# 生成可執行文件
# pyinstaller countdown.py --onefile --noconsole --noupx
import time
import datetime
import winsound
import tkinter as tk

time_end = datetime.datetime.now() + datetime.timedelta(minutes=40)
time_zero = datetime.timedelta(seconds=0)


def beep():
    global time_end
    for i in range(2):
        winsound.Beep(440, 250)  # frequency, duration
        time.sleep(0.25)         # in seconds (0.25 is 250ms)
        winsound.Beep(600, 250)
        time.sleep(0.25)
    winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
    clock.config(text=u"開始休息")
    time_end = datetime.datetime.now() + datetime.timedelta(minutes=10)
    clock.after(1000, tick)


def restart():
    global time_end
    time_end = datetime.datetime.now() + datetime.timedelta(minutes=40)
    clock.config(command=lambda: root.destroy())
    tick()


def pause():
    time.sleep(2)
    clock.config(text=u"再戰一回", bg='#dfd')


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"成功番茄", bg='red')
            clock.config(command=restart)
            clock.after(500, beep)
        else:
            flag = 1
            clock.config(text=u"休息完畢")
            clock.after(500, 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 = 50 # 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 - 5
y = hs - h - 95
# 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='#dfd',
                  command=lambda: root.destroy(),
                  # command = lambda: root.destroy()
)
clock.pack(fill='both', expand=True)
tick()
window.mainloop()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章