Tkinter 忽略root.after
倒計時影片播放但它忽略延遲并root.after在倒計時結束之前執行所有操作
所有測驗都表明倒計時正在發生并且變數正在發生變化,它只是跳過了延遲
def StartTheSpam():
global TimeLable, ErrorLabel
#destoryes error labels and the countdown timer
try:
for child in root.winfo_children():
if child == TimeLable or child == ErrorLabel:
child.destroy()
except NameError:
pass
#needed for fail-safe 1
mouse = MouseController()
countdown_value = 5
counter = tk.StringVar()
counter.set(countdown_value)
TimeLable = tk.Label(frame, textvariable=counter, padx=10, pady=5, bg=Modes[mode.get()][3]['background'], fg=Modes[mode.get()][2]['text'])
TimeLable.pack()
#coundown
for countdown in range(1, countdown_value):
root.after(1000 * countdown, counter.set, countdown_value - countdown)
x = 100
y = 100
try:
with open(PreferencesStorage['FilePath'], 'r') as SpamText:
while PreferencesStorage['loop']:
for word in SpamText:
#fail safe 1
if x < 21 and y < 21:
break
TempX, TempY = mouse.position
x = int(TempX)
y = int(TempY)
#fail-safe 2
if keyboard.is_pressed('ctrl d'):
break
keyboard.write(word)
print(word)
#return to begining when at end, thats why its outside of the loop
SpamText.seek(0)
for word in SpamText:
keyboard.write(word)
except FileNotFoundError:
NoFile = tk.Label(frame, text = 'Please Select A File', padx=10, pady=5, fg=Modes[mode.get()][2]['text'], bg=Modes[mode.get()][3]['background'])
NoFile.pack()
uj5u.com熱心網友回復:
root.after不會造成延遲。意思是“請稍后讓主回圈呼叫這個函式”。您的代碼將非常快速地將 5 個計時器回呼請求排隊,然后繼續。回呼將在稍后發生。
當您為 GUI 編碼時,您必須開始考慮事件驅動編程。當您創建小部件或呼叫“pack”時,什么也沒有發生。所做的只是將訊息排隊等待稍后處理。在未來的某個時間點,當mainloop能夠執行時,您的訊息將被彈出并處理。只有這樣,您的可見螢屏才會改變。這需要改變編程方式。您設定螢屏,啟動主回圈,然后等待事件。你的事件回呼改變狀態并回傳,后臺的 Tk 將更新螢屏。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352489.html
