简体   繁体   English

在Tkinter中打开新窗口将冻结程序(python 3.6)

[英]Opening new window in Tkinter freezes the program (python 3.6)

I have something like this: 我有这样的事情:

first.py 第一.py

from tkinter import *

def new_window(event):
    root.destroy()
    import second

root = Tk()
b = Button(root, text='New window')
b.pack()
b.bind('<Button-1>', new_window)
root.mainloop()

second.py 第二个

from tkinter import *
root = Tk()
root.mainloop()

But when I open the second window, the first one is destroyed (I hope so), but the second one is frozen (it is shown, but there is no close-button on the top and I only see the launch-icon). 但是,当我打开第二个窗口时,第一个窗口被破坏了(我希望如此),但是第二个窗口却被冻结了(显示了出来,但是顶部没有关闭按钮,我只看到了启动图标)。 Why is it so? 为什么会这样呢? Don't I kill the first loop? 我不杀死第一个循环吗?

The problem is likely because import second never returns since the last thing it does is call root.mainloop() . 之所以可能出现此问题,是因为import second永远不会返回,因为它所做的最后一件事情是调用root.mainloop() Since it never returns, the callback in the first window never finishes. 由于它永远不会返回,因此第一个窗口中的回调永远不会完成。 And since it never finishes, it's not able to process any other events. 而且由于它永远不会完成,因此无法处理任何其他事件。

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

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