所以我有一個基本的警報 python GUI,沒有什么花哨的東西仍然需要大量的作業。所以目前我的代碼作業如下:用戶點擊“創建新警報”,它打開一個帶有 3 個選項選單小部件的視窗,并選擇他希望警報響起的時間,然后一旦點擊設定的警報,就會運行一個回圈每次重復回圈時倒計時一秒,并將時間列印在控制臺中,而不是當他們設定的時間等于警報響起的當前時間時。
現在我的問題是我希望控制臺中的倒計時顯示在 GUI 上。我試圖簡單地制作一個標簽并使用 .get 來獲取當前值并顯示它們,但是一旦回圈開始,它就不會執行任何其他代碼。如果我有任何其他建議,我會全神貫注。我想要的只是一個倒計時,顯示警報響起的時間,我目前在試錯計劃中的想法是顯示控制臺。
這是我的代碼:
# Import Required Library
from tkinter import *
import datetime
import time
import winsound
from threading import *
from tkinter import messagebox
def submit():
def alarm(Curent):
# Infinite Loop
while True:
# Set Alarm
set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
# Wait for one seconds
time.sleep(1)
# Get current time
current_time = datetime.datetime.now().strftime("%H:%M:%S")
print(current_time,set_alarm_time)
Curent = current_time
set = set_alarm_time
# Check whether set alarm is equal to current time or not
if current_time == set_alarm_time:
print("Time to Wake up")
# Playing sound
winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
messagebox.showinfo(title="ALARM", message="Alarm is going off, its time to wake up!")
break
root = Tk()
root.geometry("400x300")
root.config(bg="#447c84")
root.title('MathAlarm')
# Add Labels, Frame, Button, Optionmenus
Label(root,text="Alarm Clock",font=("Helvetica 20 bold"),fg="Black").pack(pady=10)
Label(root,text="Set Time",font=("Helvetica 15 bold")).pack()
frame = Frame(root)
frame.pack()
hour = StringVar(root)
hours = ('00', '01', '02', '03', '04', '05', '06', '07',
'08', '09', '10', '11', '12', '13', '14', '15',
'16', '17', '18', '19', '20', '21', '22', '23', '24'
)
hour.set(hours[0])
hrs = OptionMenu(frame, hour, *hours)
hrs.pack(side=LEFT)
minute = StringVar(root)
minutes = ('00', '01', '02', '03', '04', '05', '06', '07',
'08', '09', '10', '11', '12', '13', '14', '15',
'16', '17', '18', '19', '20', '21', '22', '23',
'24', '25', '26', '27', '28', '29', '30', '31',
'32', '33', '34', '35', '36', '37', '38', '39',
'40', '41', '42', '43', '44', '45', '46', '47',
'48', '49', '50', '51', '52', '53', '54', '55',
'56', '57', '58', '59', '60')
minute.set(minutes[0])
mins = OptionMenu(frame, minute, *minutes)
mins.pack(side=LEFT)
second = StringVar(root)
seconds = ('00', '01', '02', '03', '04', '05', '06', '07',
'08', '09', '10', '11', '12', '13', '14', '15',
'16', '17', '18', '19', '20', '21', '22', '23',
'24', '25', '26', '27', '28', '29', '30', '31',
'32', '33', '34', '35', '36', '37', '38', '39',
'40', '41', '42', '43', '44', '45', '46', '47',
'48', '49', '50', '51', '52', '53', '54', '55',
'56', '57', '58', '59', '60')
second.set(seconds[0])
secs = OptionMenu(frame, second, *seconds)
secs.pack(side=LEFT)
Button(root,text="Set Alarm",font=("Helvetica 15"),command=alarm).pack(pady=20)
Button(root,text="Exit",font=("Helvetica 15"), command=lambda:root.destroy()).pack(pady=20)
root = Tk()
root.title('MathAlarm')
root.geometry('347x400')
root.config(bg="#447c84")
welcomelabel = Label(root, text="Welcome to Math Alarm", font=("Times", "24", "bold"))
welcomelabel.pack()
ext = Button(root, text="Exit", padx=20, pady=10, relief=SOLID, font=("Times", "14", "bold"), command=lambda:root.destroy())
reg = Button(root, text="Create new Alarm", padx=20, pady=10, relief=SOLID, font=("Times", "14", "bold"), command=submit)
ext.pack()
reg.pack()
# Execute Tkinter
root.mainloop()`
uj5u.com熱心網友回復:
好吧,我嘗試了一種不同的方法,它根本不使用自定義執行緒,而是使用 tkinter 自己的主回圈,它也有自己的缺點和優點。我對您的代碼進行了以下更改以使其正常作業
- 小時、分鐘和秒的全域變數
- 輸出標簽的全域變數
- 將您的鬧鐘功能與提交功能分開
- 從按鈕命令中洗掉警報功能
tkinter 中有一個選項可以定期在 tkinter 的主回圈中運行一個函式,這樣您就不會阻止
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/356586.html
