简体   繁体   English

Python Tkinter:在标签中循环

[英]Python tkinter : loop in Label

Hello I just wanted that the Label change/refresh during the loop, but it doesn't work 您好,我只是想在循环中更改/刷新标签,但它不起作用

This my code 这是我的代码

fen1 = Tk()

v = StringVar()
Label(fen1,textvariable=v).pack()

i=0

while(1):
    i=i+1
    v.set(i)
fen1.mainloop() 

Thanks 谢谢

here, try this: 在这里,试试这个:

from Tkinter import *
import time
root=Tk()

variable=StringVar()

def update_label():
    i=0
    while 1:
        i=i+1
        variable.set(str(i))
        root.update()

your_label=Label(root,textvariable=variable)
your_label.pack()
start_button=Button(root,text="start",command=update_label)
start_button.pack()
root.mainloop()

That should give you a good example. 那应该给你一个很好的例子。 However, it is important to note that during the while loop, you MUST call root.update() otherwise your GUI will freeze until the loop completes (in this case it never does) and never show your numbers. 但是,请务必注意,在while循环期间,您必须调用root.update()否则您的GUI会冻结,直到循环完成(在这种情况下,它永远不会执行)并且永远不会显示您的数字。

Also note that you can call update_label() from anywhere in your program. 还要注意,您可以从程序中的任何位置调用update_label() I just added it to the start button for example purposes. 我只是出于示例目的将其添加到“开始”按钮中。

What was wrong with your code was that you had set the while loop free-floating and most importantly before your GUI's mainloop. 代码的问题在于,您将while循环设置为自由浮动,最重要的是 GUI的mainloop 之前 When you do this, since this loop is infinate, it never allows Tkinter to start its mainloop() . 当您执行此操作时,由于此循环是无限的,因此它绝不允许Tkinter启动其mainloop() However, if you were to put the while loop after the mainloop, then that would never be executed until after you exit the GUI, this is because the mainloop is infinate until it is stopped (closing the GUI). 但是,如果你把while循环主循环之后 ,那么这将永远不会被执行,直到退出GUI ,这是因为,直到它停止(关闭GUI)的主循环是infinate。

So to fix this you simply put it in a function and call it later on during Tkinter 's mainloop. 因此,要解决此问题,您只需将其放入函数中,然后在Tkinter的mainloop中调用它即可。 You can do this various ways as well, for example, you can use .after() to perform a specific task after a certain amount of time, or make it the command of a button to be run when pressed, ect., ect. 您也可以通过多种方式执行此操作,例如,可以使用.after()在一定时间后执行特定任务,或者使其成为按下等操作按钮的命令。 .

However, The proper code you should use is the following, as you do not really want infinate loops in your code (other then you mainloop).: 但是,您应该使用以下适当的代码,因为您实际上并不希望代码中存在无限循环(而不是主循环)。

class App (object):
    def __init__(self):
        self.root=Tk()
        self.variable=StringVar()
        self.i=0
        self.your_label=Label(self.root,textvariable=self.variable)
    def grid(self):
        self.your_label.pack()
    def update_label(self):
        self.i=self.i+1
        self.variable.set(str(self.i))
        self.root.after(20,self.update_label)
    def run(self):
        self.grid()
        self.root.after(20,self.update_label)
        self.root.mainloop()

if __name__=='__main__':
    App().run()

Your code never gets to the mainloop . 您的代码永远不会进入mainloop In order to see something like this, you need to bind the update in a callback which gets called in the mainloop (indirectly, through events). 为了看到这样的东西,您需要将更新绑定到在主循环中被调用的回调中(通过事件间接地)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM