【问题标题】:How to loop through a list in a label using Tkinter?如何使用 Tkinter 遍历标签中的列表?
【发布时间】:2021-02-16 10:39:07
【问题描述】:

我正在尝试创建一个图形用户界面,它只是在间隔一段时间后一个接一个地打印消息。

list_of_tweets = [1, 2, 3, 4, 5]


for i in list_of_tweets:
    print(i)
    time.sleep(10)

window = Tk()
window.geometry("800x400")

window.title("Twitterscreen")

variable=StringVar()

def update_label():
    while True:
        for i in list_of_tweets:
            print(i)
            time.sleep(10)
        variable.set(str(i))
        window.update()

label = Label(master=window,
              background='white',
              foreground='black',
              font=('Helvetica', 20),
              width=14,
              height=3)
label.config(width=200, height=300)
label.pack()

mainloop()

到目前为止,这是我的代码。当我运行它时,结果只是一个白屏。同样,我希望标签一次打印出 list_of_tweets 一个,间隔 10 秒。

【问题讨论】:

  • 我应该补充一下,我对 Tkinter 完全陌生,所以请原谅任何愚蠢的错误。
  • 使用 time.sleep() 会使 GUI 无响应
  • 有什么办法可以在两者之间暂停一下吗?
  • window.after() 是这样做的一种方式。但我们无法运行此代码,因此我们不知道 public_tweetstweet 是什么
  • 您想知道为什么会显示一个巨大的白色屏幕吗?它是您的标签背景和您配置的大小。只需减小大小并从主块调用函数

标签: python loops user-interface tkinter label


【解决方案1】:

试试这个标签:

display_text = StringVar()
label=Label(window, textvariable=display_text)


text='Your message'
display_text.set(text)

【讨论】:

    【解决方案2】:

    首先将您的标签变成:

    label = Label(master=window,
                  background='white',
                  foreground='black',
                  font=('Helvetica', 20),
                  width=200,
                  height=300)
    

    现在我在这里使用的想法是索引列表直到它到达列表中的最后一项,而不是使用for 循环。所以把你的函数改成:

    count = 0 #index number 
    def update_label():
        global count
        label.config(text=list_of_tweets[count]) #show the current indexed item
        count += 1 #increase the index number 
        rep = window.after(1000,update_label) #repeat this process every 1 second
        
        if count >= len(list_of_tweets): #if the index number greater than or equal to length of list
            window.after_cancel(rep) #stop repeating 
    

    所以最终的代码是:

    from tkinter import *
    
    list_of_tweets = [1, 2, 3, 4, 5]
    
    window = Tk()
    window.geometry("800x400")
    
    window.title("Twitterscreen")
    
    variable = StringVar() #no use here, dont know why you would use it here
    
    count = 0 
    def update_label():
        global count
    
        label.config(text=list_of_tweets[count])
        count += 1
        rep = window.after(1000,update_label)
        
        if count >= len(list_of_tweets):
            window.after_cancel(rep)
        
    label = Label(master=window,
                  background='white',
                  foreground='black',
                  font=('Helvetica', 20),
                  width=200,
                  height=300) # no need to configure it separately when you can declare it directly
    
    label.pack()
    update_label() # call the function initially
    
    mainloop()
    

    为什么它在您的示例中不起作用?首先,您将 StringVar() 的值设置为文本,正确,但 StringVar() 根本与标签无关。如果使用for 循环,您将需要time.sleep(),但这会冻结GUI,即使您使用window.update() 窗口也会更新,但GUI 窗口将无响应。

    after(ms,func) 方法主要接受两个参数:

    • ms - 运行函数的时间(毫秒)
    • func - 在给定 ms 完成后运行的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-16
      • 2023-01-31
      • 2011-01-19
      • 2011-08-14
      • 1970-01-01
      • 2015-05-14
      • 2013-08-28
      • 2016-10-14
      相关资源
      最近更新 更多