我目前正在創建一個程序,將不斷檢查按鍵,如果它是W或S它會改變速度。但是我有一個Tk沒有迴應的問題?任何人都會幫助歡呼。我的代碼是:Python - while循環更改變量
import tkinter as tk #Importing the GUI Library
import time #Importing Time#
def onKeyPress(event): #Defining my function of keypress, and using GUI Library to get the keypress.
time.sleep(1) #Telling it to wait one second otherwise it will crash.
text.insert('end', 'You pressed %s\n' % (event.char,)) #Telling the user if he pressed the key or not.
speed = 50 #Setting the speed to 50 default
while speed > 0:
if event.char == 'w': #if key pressed = w:
speed = speed + 1 #Change speed by 1
time.sleep(1)
if event.char == 's':
speed = speed - 1
time.sleep(1)
print(speed)
root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()
謝謝!正是我想要的。 –