簡體   English   中英

在 tkinter python 的時間限制內停止時鍾

[英]stop the clock within the time limit in tkinter python

我想在時限內停止時鍾,這里我給了 5 秒但時鍾沒有停止,請幫忙。

將 tkinter 作為 tk 從 tkinter.constants 導入 *

def start():全局時、分、秒

if hours == 4:
    return
seconds -= 1


if seconds == 00:
    minutes -= 1
    seconds = 60

if minutes == 00 and seconds == 00:
    hours = hours+1

clock.config(text=f'{hours:02}:{minutes:02}:{seconds:02}')

root.after(1000, start)

root=tk.Tk() clock = tk.Label(root, font=("粗體",20), anchor=CENTER, text="00:00:00")

clock.place(relx=0.5,rely=0.5,anchor=CENTER)

小時、分鍾、秒 = 0,0,5

開始()

根.mainloop()

只需添加變量 stop ,如果stop == True則時鍾將停止

解決你的問題

import tkinter as tk
from tkinter.constants import *

def start():
    global hours, minutes, seconds, stop

    if hours == 4:
        return
    if stop == False:
        seconds -= 1
        
    if seconds == 00:
        stop = True
    
    elif seconds == 00 and minutes != 00 and hours != 00:
        minutes -= 1
        seconds = 60

    elif minutes == 00 and seconds == 00:
        hours = hours+1

    clock.config(text=f'{hours:02}:{minutes:02}:{seconds:02}')

    root.after(1000, start)

root=tk.Tk()
clock = tk.Label(root, font=("bold",20), anchor=CENTER, text="00:00:00")

clock.place(relx=0.5, rely=0.5, anchor=CENTER)

hours,minutes,seconds,stop = 0,0,5, False

start()

root.mainloop()

一個完整的時鍾遞減計數器 BONUS

import tkinter as tk
from tkinter.constants import *

def start():
    global hours, minutes, seconds, stop

    if hours == 4:
        return
    if stop == False:
        seconds -= 1

    if seconds == 00 and minutes == 00 and hours == 00:
        stop = True
    
    elif seconds == -1 and minutes != 00:
        minutes -= 1
        seconds = 59

    elif hours != 00 and minutes == 00 and seconds == -1:
        hours -= 1
        minutes = 59
        seconds = 59


    clock.config(text=f'{hours:02}:{minutes:02}:{seconds:02}')

    root.after(1000, start)

root=tk.Tk()
clock = tk.Label(root, font=("bold",20), anchor=CENTER, text="00:00:00")

clock.place(relx=0.5, rely=0.5, anchor=CENTER)

hours,minutes,seconds,stop = 0,0,5, False

start()

root.mainloop()

沒有那么多if條件的解決方案。 我使用timestamp ,但我認為時區也有一些問題。 也許有人可以解決我的偏移量。

我的 tkinter TIMER應用程序,帶鈴鐺:

import tkinter as tk
import calendar
from datetime import datetime
import time
import sys 


class Countdown(tk.Frame):
    def __init__(self,master):
        tk.Frame.__init__(self, master)
        global target
        self.master = master
        self.clock = tk.Label(text="00:00:00", fg="Green", font=("bold",40))
        self.clock.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
        timestamp = calendar.timegm(time.localtime())
        print("Timestamp",timestamp)
        print("Timestamp_future",timestamp + t_plus)
        target = timestamp + t_plus
        self.update_clock()
       
    def update_clock(self):     
        current = calendar.timegm(time.localtime()) -23*3600
        countdown = target - current 
        self.down_time = datetime.fromtimestamp(countdown).strftime("%H:%M:%S")       
        self.clock.configure(text=self.down_time)
        if countdown/3600 != 23:
            self.after(1000, self.update_clock)
        else:
            print('\a', end='', flush=True) 

            
root = tk.Tk()
global t_plus

######### SET YOUR TIME FOR THE COUNTDOWN ############
your_timer_set = '00:00:05' # hours, minutes, seconds
######################################################
countdown = time.strptime(your_timer_set, '%H:%M:%S')
hours = int(countdown.tm_hour)
minutes = int(countdown.tm_min)
seconds = int(countdown.tm_sec)
t_plus = ((hours*3600)+(minutes*60)+seconds)
print("SET_TIME",t_plus)

app=Countdown(root)
root.title("Countdown")
root.geometry("400x400+400+150")

root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM