我想爲一個任務做一個簡單的老虎機python程序,我無法獲得老虎機圖像更新。我想要發生的事情是讓用戶點擊按鈕,讓三個標籤每隔0.1秒動態更新一張不同的圖片,持續2秒。但是,發生了什麼事是我的randint正在爲數組生成隨機索引號,但標籤只在最後一個randint實例上顯示一個新圖像。這裏是我的代碼:如何動態更新tkinter標籤小部件中的圖像?
def update_image():
global images
time.sleep(0.1)
y = images[randint(0, 3)]
slot1.delete()
slot1.configure(image = y)
print(y)
def slot_machine():
x = 0
while x != 10:
slot1.after(0,update_image)
x = x + 1
你爲什麼叫'slot1.after(0,...)'然後立即在'update_image'中睡十分之一秒?爲什麼不直接調用'slot1.after(100,...)'?一般來說,你不應該在主GUI線程中調用'sleep'。 –