【问题标题】:Python - Why is my Tkinter window not closing on self.destroy?Python - 为什么我的 Tkinter 窗口没有在 self.destroy 上关闭?
【发布时间】:2016-10-15 21:16:56
【问题描述】:

我目前正在创建一个小程序,它将 Tkinter 输入框插入到 Google 日历中(当然,在所有不同类型的检查之后)。那部分不是问题。 由于我同时在运行一个终端,我不想在 Tkinter 窗口打开期间“保持”。

当我使用

关闭窗口时
def quit(self):
    self.thread.stop()
    self.destroy()

窗口的所有部分都消失了,但窗口仍然在屏幕上。

class window(Frame):

def __init__(self, parent, thread):
    Frame.__init__(self, parent)   

    self.parent = parent
    self.w = 600
    self.h = 400

    self.initUI()
    self.center()
    self.thread = thread

我使用这个函数来创建类:

def main(thread):

root = Tk()
root.resizable(width=False, height=False)
app = window(root, thread)
root.mainloop()

myThread 类。

class myThread(threading.Thread):
def __init__(self,threadType):
    threading.Thread.__init__(self)
    self.threadType = threadType
    self.event = threading.Event()
def stop(self):
    self.event.set()
def run(self):
    if self.threadType == "new_app":
        newappoint.main(self)
    elif self.threadType == "main":
        main()

谁能告诉我我是否错过了可以使窗户正常关闭的东西。 调用self.quit()后线程关闭

【问题讨论】:

  • tkinter 和线程不能很好地混合,例如参见 stackoverflow.com/questions/10556479/…
  • @shivsn 成功了,谢谢 :)
  • @J.J.Hakala 所以基本上最好只线程化我原来的主函数然后保持if __name__ == __main__:循环寻找一个变量来改变和启动Tkinter?

标签: python multithreading tkinter


【解决方案1】:

而不是self.destroy() 这样做self.parent.destroy()

据我了解,我们将tk 对象传递给window 类,并在类中用作parent。因此,我们使用parent 创建小部件,而不是使用root

【讨论】:

  • 你能解释一下它为什么有用吗?
  • 谢谢。您应该将此解释添加到答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
相关资源
最近更新 更多