当代码中有 'mainloop()' 时,如何实际执行 'while True' 循环代码?

How can I actually execute a 'while True' loop code while there is a 'mainloop()' in the code?

如果我的问题无法理解,我很抱歉。 无论如何,我有这个代码:

from tkinter import *
root = Tk()
root.title("My Window")

# Adding a text widget thing.
text = Text(root, foreground='black', background='white', font=("Consolas", 16))
text.pack(expand=1, fill=BOTH)

# a while true loop.
while True:
    text.delete(1.0, END)
    text.insert(END, "This is a text in a loop")

# Calling the Python tkinter's mainloop.
root.mainloop()

每次我尝试 运行 上面的代码时,它不会显示 Window,相反,它 运行 是 while True 循环中的代码.

所以主要问题是,有没有办法让 Python 显示 window(我的意思是执行代码 root.mainloop(),然后执行while True 循环代码?

任何帮助或答案将不胜感激!
谢谢!

你应该尽量避免这种情况,但你可以使用递归循环。

创建一个名为“myloop”的函数并像这样重复它,然后在主循环之前调用它:

def myloop():
   <code>
   root.after(1, myloop) #repeats the function again 1 1000th of a second after it finishes


#----- down by where you're calling mainloop-----


root.after(1, myloop)
root.mainloop()