【问题标题】:Python textbox only updates after loop is completePython文本框仅在循环完成后更新
【发布时间】:2021-09-28 07:11:51
【问题描述】:

我有一个很长的自动化程序,它使用很长的 WHILE 循环。随着过程的进行,我希望能够更新文本框中的文本。但是,根据我所做的研究,这似乎是不可能的,并且当 WHILE 循环完成时,所有结果都会立即“转储”。这对我没用。

如果文本框可以随着 SHELL 的更新而更新,我会很高兴,因为它与实际过程同步。

我制作了一个简单的 TEST 文件,看看我是否可以让它工作。以下是 TEST 文件的代码:

from tkinter import *
import time
import tkinter as tk
import tkinter.font as tkFont  

root=Tk()
myFont = tkFont.Font(family = 'Helvetica', size = 18, weight = 'bold')
text_cell_bg="cyan"        #TEXT CELL BACKGROUND COLOR
text_cell_fg="black"       #TEXT CELL TEXT COLOR
text_cell_height=2         #TEXT CELL HEIGHT
text_cell_width=30         #TEXT CELL WIDTH
button_bg="blue"           #BUTTON CELL BACKGROUND COLOR
button_fg="white"          #BUTTON CELL TEXT COLOR
button_height=2            #BUTTON CELL HEIGTH
button_width=10            #BUTTON CELL WIDTH

textbox=Text(root)
textbox.insert(END, 'Default Text\n\n')


def count_print ():
    count = 0
    letter = "A"
    while count < 5:
        print("Count = ",count,".  Letter = ",letter,".")
        textbox_value = "Count = {}.  Letter = {}.\n".format(count,letter)
        textbox.insert(1.0, textbox_value)
        count += 1
        time.sleep(1)

textbox.pack()

button1=tk.Button(root, text='output', command=count_print, font = myFont,
                        height=button_height,
                        width=button_width,
                        bg = button_bg,
                        fg = button_fg)
button1.pack()

root.mainloop()

【问题讨论】:

  • 在使用tkinter 时不应使用time.sleep。最好使用tkinter 循环。例如看here
  • 如果您在此站点上搜索[tkinter] sleep,您会发现超过 1000 个问题和答案。您是否尝试过查看现有问题?
  • 为什么我应该避免在 tkinter 中使用 time.sleep?我没有遇到任何问题。

标签: python loops tkinter while-loop textbox


【解决方案1】:

您可以使用 Tkinter 从函数本身内部调用您的函数。 Tkinter 对此有一个特殊的功能。

from tkinter import *
import time
import tkinter as tk
import tkinter.font as tk_font

root = Tk()
myFont = tk_font.Font(family='Helvetica', size=18, weight='bold')
text_cell_bg = "cyan"  # TEXT CELL BACKGROUND COLOR
text_cell_fg = "black"  # TEXT CELL TEXT COLOR
text_cell_height = 2  # TEXT CELL HEIGHT
text_cell_width = 30  # TEXT CELL WIDTH
button_bg = "blue"  # BUTTON CELL BACKGROUND COLOR
button_fg = "white"  # BUTTON CELL TEXT COLOR
button_height = 2  # BUTTON CELL HEIGHT
button_width = 10  # BUTTON CELL WIDTH

textbox = Text(root)
textbox.insert(END, 'Default Text\n\n')


def count_print(n_times=0): # tell your function how many times you have called it
(on the first call this will be 0)
    n = n_times + 1
    letter = "A"
    if n <= 5:
        print("Count = ", n, ".  Letter = ", letter, ".")# you can change the n var on these lines to n_times if you want 0-4 rather than printing 1-5.
        textbox_value = "Count = {}.  Letter = {}.\n".format(n, letter)
        textbox.insert(1.0, textbox_value)
        root.after(1000, count_print, n) # this number replaces and time.sleep  it's set to 1 second right now it also call the count_print function passing n as a variable


textbox.pack()

button1 = tk.Button(root, text='output', command=count_print, font=myFont,
                    height=button_height,
                    width=button_width,
                    bg=button_bg,
                    fg=button_fg)
button1.pack()

root.mainloop()

我希望这会有所帮助。另外,我不会在我的项目中使用 Tkinter,而且我也绝对不是很擅长使用它,所以我对 root 的了解要归功于我。在得到这个答案之后, Make Tkinter force a "refresh" a text widget in mid-command?

【讨论】:

  • n0 时,OP 的代码会打印出来,但你的代码不会。还有使用nn_times 的意义何在。您可以删除其中一个变量。
  • @TheLizzard 我明白你在说什么,这完全有道理,这就是为什么我在 if 语句的第一行留下评论,如果你改变变量,它将打印 0-4而不是 1-5,我还保留了额外的变量,以便更容易更改为其他数字集或保持原样。我希望这能对此有所澄清。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 2018-07-04
  • 2020-02-06
  • 1970-01-01
  • 2011-11-03
  • 2020-09-18
  • 1970-01-01
相关资源
最近更新 更多