停止 Tkinter 窗口而不关闭它

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

我编写了一个Python脚本,它显示了寻路算法演变的动画,我想知道一旦达到特定条件如何停止主循环。

我的基本想法(没有任何实际代码)是:

import Tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=800, height=800, background="black")
canvas.pack()

def initialise():
    <some code to initialise everything on the canvas>

def move():
    <some code for move()>

root.after(1000,move)
root.mainloop()

我希望能够测试 move() 函数中的一个条件,该条件允许我停止 Tkinter 主循环,但仍保持窗口打开,以便您可以看到它,然后能够在之后执行其他操作(不包括窗口,如果它不可更改并不重要,只要它在用户关闭窗口之前可见即可)

基本上与此类似:

while true:   # this represents the Tkinter mainloop
    <do something>
    if <final condition>:
        break

<some other operations on data>  # this happens after mainloop stops
python tkinter
2个回答
1
投票

如果不破坏窗户,你就无法停止

mainloop
。这就是 Tkinter 的基本性质。

您可以做的就是停止动画。您可以完全按照您的建议进行操作:在您的

move
方法中添加标志或最终条件。

def move():
    if some_condition:
        return
    ...
    root.after(1000, move)

0
投票

您可以使用

root.quit()
代替
root.destroy()

类似这样的:

while True:   # this represents the Tkinter mainloop
    <do something>
    if <final condition>:
        root.quit()

<some other operations on data>   # this happens after mainloop stops
© www.soinside.com 2019 - 2024. All rights reserved.