简体   繁体   English

如何在 tkinter window 打开时运行 while 循环?

[英]How do I run a while loop in tkinter window while it is open?

I have a while loop that I want to run while the Tkinter window is open but the Tkinter window doesn't even open when the while loop is running.我有一个while循环,我想在 Tkinter window 处于打开状态时运行,但 Tkinter Z05B8C74CBD976FBF2DEZ4 运行while甚至没有打开。

This is a problem since my while loop is an infinite loop.这是一个问题,因为我的while循环是一个无限循环。

I basically want to create a programme that provides the users with new choices after a previous choice is selected by updating the buttons through a while loop, but whenever I use a while loop Tkinter doesn't open the window until I end the loop.我基本上想创建一个程序,在通过while循环更新按钮来选择先前的选择后为用户提供新的选择,但是每当我使用while循环时,Tkinter直到我结束循环才会打开window。

root = Tk()
i=0
while i==0:
    def choice1():
        list1[a1].implement()
        list1.remove(list1[a1])
    def choice2():
        list2[a2].implement()
        list2.remove(list2[a2])

button1 = tk.Button(root, text=list1.headline, command=choice1)
button2 = tk.Button(root, text=list2.headline, command =choice2)
root.mainloop()

Also, an error shows up because tkinter keeps executing this loop until there are no items in list1 or list2 left.此外,还会出现错误,因为 tkinter 一直执行此循环,直到 list1 或 list2 中没有任何项目。

What I want to know is if there is a way to run Tkinter window while the while loop is going on我想知道的是是否有办法在while循环进行时运行 Tkinter window

( a1 and a2 are randomly generated numbers.) a1a2是随机生成的数字。)

You should probably put root.mainloop in the loop, otherwise it will never execute.您可能应该将root.mainloop放在循环中,否则它将永远不会执行。 If mainloop() doesn't execute, the window won't stay open.如果 mainloop() 不执行,window 将不会保持打开状态。

And also: you need to call the functions, defining them isn't enough.而且:你需要调用函数,定义它们是不够的。 So instead of in the loop only having the def choice1() and def choice2() you need to also have choice1() and choice2() in the loop, otherwise it won't execute those commands.因此,除了在循环中只有def choice1()def choice2() ,您还需要在循环中拥有choice1()choice2() ,否则它不会执行这些命令。

And one more thing: you need to pack() the buttons, so add the lines button1.pack() and button2.pack() .还有一件事:你需要 pack() 按钮,所以添加行button1.pack()button2.pack() The buttons also need to be before the loop, which means your def choice1() and def choice2() need to be before the loop also.按钮也需要在循环之前,这意味着你的def choice1()def choice2()也需要在循环之前。 (The buttons will never show up otherwise) (否则按钮将永远不会出现)

The mainloop() is the main reason for the window to be displayed continuously. mainloop()是window连续显示的主要原因。 When the while loop is running, the mainloop() does not get executed until the while loop ends.while loop运行时, mainloop()直到while loop结束才被执行。 And because in your case the while loop never ends, the code including the mainloop() keeps waiting for its turn to be executed.而且因为在您的情况下,while 循环永远不会结束,所以包含mainloop()的代码会一直等待轮到它执行。

To overcome this issue you will have to put all the widgets you want to be displayed in the window along with the mainloop() inside the while loop为了克服这个问题,您必须将要显示的所有小部件与while loop中的mainloop()一起放在 window 中

Like this:像这样:

import tkinter as tk

root = tk.Tk()

i = 0

while i == 0:

    def choice1():
        list1[a1].implement()
        list1.remove(list1[a1])

    def choice2():
        list2[a2].implement()
        list2.remove(list2[a2])


    button1 = tk.Button(root, text=list1.headline, command=choice1)
    button2 = tk.Button(root, text=list2.headline, command=choice2)

    root.mainloop()

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

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