日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Python利用tkinter實現(xiàn)一個簡易番茄鐘的示例代碼_python

作者:X_chaotu ? 更新時間: 2022-12-29 編程語言

之前搗鼓樹莓派時,要求做一個番茄鐘,但最后就只是搞成一個與樹莓派沒啥關(guān)系的py程序,雖然簡陋,但就此記錄一下自學的成果。

程序?qū)崿F(xiàn)番茄工作法:25分鐘工作,5分鐘休息

完成一次番茄工作時間,就記一個番茄

(不把休息時間算在里面,有時候自己都不想休息,好吧,是我不知道怎么把番茄工作時間和休息時間聯(lián)系在一塊來記錄番茄個數(shù))

這個程序倒計時顯示的是從24:59開始,是因為按的時候算是1秒?

運行界面如下:

自己感覺這個界面還行,樸素中帶著點高級感

代碼參考了一些大佬寫的番茄鐘程序,特別是那個倒計時的實現(xiàn),很神奇.

代碼如下:

import tkinter
from tkinter import messagebox
import time
 
#創(chuàng)建番茄鐘窗口
root = tkinter.Tk()
root.title('番茄鐘小程序')
root.geometry('300x300')
root.configure(bg='Tomato')
 
#創(chuàng)建計數(shù)的
count = 0
 
#創(chuàng)建完成計時后的彈窗
def mymsg():
    tkinter.messagebox.showinfo("提示", "恭喜完成一個番茄鐘??!記得休息一下")
def mymsg2():
    tkinter.messagebox.showinfo("提示", "休息完畢!")
 
 
#創(chuàng)建番茄計時函數(shù)
# strptime()函數(shù)將字符串轉(zhuǎn)換為datetime
def tomato_clock():
    remain_time = 1500
    bb = time.strftime('/  %M:%S', time.gmtime(remain_time))
    lb2.configure(text=bb)
    lb3.configure(text='剩余時間/總時間')
    for i in range(1500):
        remain_time -= 1
        aa = time.strftime('%M:%S', time.gmtime(remain_time))
        lb.configure(text=aa)
        root.update()
        time.sleep(1)
        if remain_time == 0:
            tomato_count()
            mymsg()
 
#創(chuàng)建計數(shù)的函數(shù)
def tomato_count():
    global count
    count += 1
    lb4.configure(text=count)
 
#創(chuàng)建休息時間函數(shù)
def relax():
    remain_time = 300
    bbb = time.strftime('/  %M:%S', time.gmtime(remain_time))
    lb2.configure(text=bbb)
    lb3.configure(text='剩余時間/總時間')
    for i in range(300):
        remain_time -= 1
        aaa = time.strftime('%M:%S', time.gmtime(remain_time))
        lb.configure(text=aaa)
        root.update()
        time.sleep(1)
        if remain_time == 0:
            mymsg2()
 
#創(chuàng)建各種標簽
 
#番茄動態(tài)計時
lb = tkinter.Label(root, text=' ', bg='Tomato', fg='white', font='Verdana 16 bold', width=7, height=1)
lb.place(x=50, y=100)
 
#番茄固定時間
lb2 = tkinter.Label(root, text=' ', bg='Tomato', fg='white', font='Verdana 16 bold', width=8, height=1)
lb2.place(x=138, y=100)
 
#剩余時間/總時間
lb3 = tkinter.Label(root, text=' ', bg='Tomato', fg='white', font='Verdana 16 bold', width=14, height=2)
lb3.place(x=50, y=44)
 
#番茄個數(shù)顯示
lb4 = tkinter.Label(root, text='0', bg='Tomato', fg='white', font='Verdana 16 bold', width=7, height=1)
lb4.place(x=25, y=20)
 
#左上角的 番茄:
lb5 = tkinter.Label(root, text='番茄:', bg='Tomato', fg='white', font='Verdana 16 bold', width=4, height=1)
lb5.place(x=5, y=20)
 
 
#按鈕
#開啟一個番茄
Button1 = tkinter.Button(root, text='開啟一個番茄', bg='orange', fg='black', font='Verdana 13 bold', width=15, height=1, command=tomato_clock)
Button1.place(x=70, y=150)
 
#休息一下
Button2 = tkinter.Button(root, text='休息一下', bg='cornflowerblue', fg='black', font='Verdana 13 bold', width=15, height=1, command=relax)
Button2.place(x=70, y=200)
 
#循環(huán)
root.mainloop()

原文鏈接:https://blog.csdn.net/X_chaotu/article/details/127328735

欄目分類
最近更新