Tkinter 在 while 循环中的 widget.configure [英] Tkinter's widget.configure inside a while loop

查看:45
本文介绍了Tkinter 在 while 循环中的 widget.configure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据已完成的计算更改 Tkinter Label 的文本.我使用的是简单的 label.configure(text="something new").问题是我需要在 while 循环的每次迭代中都这样做.该过程只是等到循环完成,然后显示最后的结果.我一直需要它们.

I need to change text of Tkinter Label based on done calculations. I am using simple label.configure(text="something new"). The problem is that I need to do so in every iteration of while loop. The process just waits till the loop is done and then shows the last result. I need them continuously.

def new_frequency_1000times():
  k=1
  while k>1000:
   #steps to determine new frequency f
   freq_out.configure(text=str(f))
   k=k+1

master=Tk()
freq_out = Label(master)
freq_out.grid(row=0, column=1)
button_freq=Button(okno, command=new_frequency_1000times, text="Get new f")
button_freq.grid(row=0, column=0)

知道如何在循环内强制执行评估"吗?

Any idea how to force the "evaluation" inside a loop?

推荐答案

你的问题是你有一个 while 循环在主偶数线程中运行.因此,它会阻塞直到 while 循环完成.使用 afterthreading.

Your problem is that you have a while loop running in the main even thread. So, it's blocking until the while loop is completed. Use after or threading.

这是一个小例子:

import tkinter as tk

def new_frequency_1000times(k=0):

    if k < 1000:
        freq_out.configure(text=str(k))
        #1000 ms = 1 seconds, adjust to taste.
        root.after(10, lambda: new_frequency_1000times(k+1))

root=tk.Tk()
freq_out = tk.Label(root)
freq_out.grid(row=0, column=1)
button_freq=tk.Button(root, command=new_frequency_1000times, text="Get new f")
button_freq.grid(row=0, column=0)
root.mainloop()

这篇关于Tkinter 在 while 循环中的 widget.configure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆