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

问题描述 投票:0回答:4

我有一个

while
循环,我想在 Tkinter 窗口打开时运行它,但当
while
循环运行时,Tkinter 窗口甚至不会打开。

这是一个问题,因为我的

while
循环是无限循环。

我基本上想创建一个程序,在通过

while
循环更新按钮来选择先前的选择后为用户提供新的选择,但是每当我使用
while
循环时,Tkinter 都不会打开窗口,直到我结束循环。

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()

此外,还会显示错误,因为 tkinter 不断执行此循环,直到 list1 或 list2 中没有任何项目为止。

我想知道是否有办法在 while 循环进行时运行 Tkinter 窗口

a1
a2
是随机生成的数字。)

python python-3.x user-interface tkinter while-loop
4个回答
1
投票

mainloop()
是窗口不断显示的主要原因。当
while loop
运行时,
mainloop()
不会被执行,直到
while loop
结束。因为在你的情况下, while 循环永远不会结束,所以包括
mainloop()
的代码一直等待轮到执行。

要解决这个问题,您必须将所有想要在窗口中显示的小部件与

mainloop()
一起放入
while loop

像这样:

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()

0
投票

您应该将

root.mainloop
放入循环中,否则它永远不会执行。如果 mainloop() 不执行,窗口将不会保持打开状态。

而且:您需要调用函数,定义它们是不够的。 因此,循环中不应仅包含

def choice1()
def choice2()
,您还需要循环中包含
choice1()
choice2()
,否则它将不会执行这些命令。

还有一件事:您需要 pack() 按钮,因此添加行

button1.pack()
button2.pack()
。这些按钮也需要位于循环之前,这意味着您的
def choice1()
def choice2()
也需要位于循环之前。 (否则按钮永远不会显示)


0
投票

tkinter 在自己的循环中运行,每个按钮/小部件/元素都可以绑定到更高级的功能。从您的脚本来看,您已将函数内置到循环中,如果将它们放在循环之外,则可以增强它们的可用性。

您的代码:

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()

建议代码:

root = Tk()

i=0

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

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

while i==0:

 # put here, what this extra loop will do.#

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


0
投票

我有一个 while 循环,我想在 Tkinter 窗口打开时运行它 打开,但当 while 循环时 Tkinter 窗口甚至不会打开 跑步。

您缺少

grid()
place()
pack()

Button
小部件和
mainloop()
放入
while/else
块中。

片段:

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).pack()
    button2 = tk.Button(root, text='list2.headline', command =choice2).pack()
    root.mainloop()

截图:

© www.soinside.com 2019 - 2024. All rights reserved.