簡體   English   中英

python Tkinter將標簽綁定到循環事件

[英]python Tkinter Bind label to loop event

嗨我試圖在tkinter中獲取一個標簽,一次顯示每個項目,因為它循環通過列表。 我的代碼只顯示一旦循環完成后標簽中的最后一個元素。 Shell工作正常。 謝謝

 from Tkinter import*
 import tkMessageBox
 import time
 top = Tk()
 top.geometry("500x500+100+200")

 w =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,1620]
 def bello():
     for pp in w :
         print(pp)
         myLabel = Label(top, text=pp).grid(row=6, column=0, sticky='e')
         time.sleep(1)

 B1 = Button(top, text = "Say Hello", command = bello)
 B1.place(x=50, y=50)
 top.mainloop()

每次顯示循環中的新值時,您都需要使用update()方法更新GUI:

from Tkinter import *
import time


top = Tk()
top.geometry("500x500+100+200")

w = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1620]


def bello():
    for pp in w:
        print(pp)
        Label(top, text=pp).grid(row=6, column=0, sticky='e')

        time.sleep(1)
        top.update() #refreshes top before looping to the next element


B1 = Button(top, text="Say Hello", command=bello)
B1.place(x=50, y=50)
top.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM