我創建了這個 GUI 通知程式,我必須在 task_box 中給它一個任務,在 time_box 中給它所需的時間,它會倒計時到 0,并會通知我在開始時給它的任務。但是有一個我似乎無法弄清楚的錯誤(即使經過大量研究),我無法設定我想要的時間,因為計數是倒計時函式中的一個引數,它不接受該值time_box.get() 作為引數,我只能編輯代碼(比如在countdown(count=10)函式中設定時間)
代碼:
import time
from tkinter import *
from plyer import notification
from tkinter import messagebox
def space():
space = Label(text="", bg="darkgreen")
space.grid()
def countdown(count=10):
if task_box.get() != "" and time_box.get() != "" and time_box.get().isdigit():
# count = 60 * int(time_box.get())
count_label["text"] = count
if count > 0:
root.after(1000, countdown, count-1)
if count == 0:
set_notifier()
else:
messagebox.showwarning(title="Error", message="Please set task and / or time")
def set_notifier():
notification.notify(
title = f"{task_box.get()}",
message = "Don't be stupid, just do what I say!",
timeout=3
)
root = Tk()
# root.iconbitmap("yt.ico")
root.title("Notifier")
root.geometry("400x400")
root.columnconfigure(0, weight=1)
root.config(bg="darkgreen")
space()
space()
space()
task_label = Label(root, text="Enter task", bg="darkgreen", fg="white", font=("jost", 9, "bold"))
task_label.grid()
EntryVar = StringVar()
task_box = Entry(root, width=27, textvariable=EntryVar)
task_box.grid()
space()
time_label = Label(root, text="Set time (minutes)", bg="darkgreen", fg="white", font=("jost", 9, "bold"))
time_label.grid()
EntryVar = StringVar()
time_box = Entry(root, width=27, textvariable=EntryVar)
time_box.grid()
space()
space()
set_btn = Button(width=23, height=1, bg="darkgreen", fg="white", text="Set", font=("jost", 11, "bold"), command=countdown)
set_btn.grid()
space()
count_label = Label(root, text="", bg="darkgreen", fg="white")
count_label.grid()
space()
space()
root.bind('<Return>', set_notifier)
root.mainloop()
uj5u.com熱心網友回復:
command= 期望沒有引數的函式名稱,但您始終可以創建其他沒有引數的函式,但它會使用引數運行您的函式:
def start_countdown():
countdown( int(time_box.get()) )
Button(..., command=start_countdown )
您也可以lambda為此使用
start_countdown = lambda : countdown( int(time_box.get()) )
Button(..., command=start_countdown )
或更簡單的
Button(..., command=lambda:countdown(int(time_box.get())) )
順便提一句:
time_box.get() 給出字串,所以你必須記住轉換為整數。
但是用戶可能會設定文本hello而不是數字,然后它可能會引發錯誤 - 所以更好地使用try/except- 這需要正常的功能。
def start_countdown():
try:
countdown( int(time_box.get()) )
except ValueError as ex:
print('ex:', ex)
messagebox.showwarning(title="Error", message="Wrong time value")
Button(..., command=start_countdown )
您也可以使用start_countdown()來檢查,task_box.get()這樣您就不必在countdown().
檢查中的entry值會countdown()產生其他問題。每 1 秒它從中獲取值Entry并檢查它 - 因此,如果您在倒計時時更改值,則它可能會messagebox以"Please set task and / or time".
uj5u.com熱心網友回復:
您的問題是由使用 Button 呼叫引起的,countdown(count=10)因為 button 命令不帶引數并且 count=10 是 kwd 而不是 arg。
解決方案
countdown(count)間接呼叫counter()
def countdown(count):
count_label["text"] = count
if count > 0:
root.after(1000, countdown, count-1)
else:
set_notifier()
def counter():
if task_box.get() != "":
count = time_box.get()
if count != "" and count.isdigit() and int(count ) > 0:
count = 60 * int(time_box.get())
countdown(count)
else:
messagebox.showwarning(title = "Error", message = "Please set time time")
time_box.focus_set()
else:
messagebox.showwarning(title = "Error", message = "Please set task event")
task_box.focus_set()
也像這樣更改 set_btn 命令。
set_btn = Button(
width = 23, height = 1, bg = "darkgreen", fg = "white", text = "Set",
font = ("jost", 11, "bold"), command = counter)
set_btn.grid()
task_box.focus_set()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/365157.html
下一篇:訊息框名稱未定義
