首页 > 解决方案 > 必须为多个窗口重复按下 Tkinter 的“退出”按钮

问题描述

我正在开发一个显示 matplotlib 图形的 Tkinter GUI。我使用变量“window”来初始化 Tk 解释器;而不是“根”/“主”= Tk()。GUI 由 MyWindow 类格式化。

class MyWindow:
    def __init__(self, win):

三个 GUI 'visual' 按钮显示 matplotlib 图形。mpl 图形的函数位于一个名为“charts”的模块中。在下面的示例中,“概览”是一个可视按钮。

        self.btn5=Button(win, text='Overview')
        self.b5=Button(win, text='Overview', width='6', height='2', command=self.overview)
        self.b5.place(x=50, y=160)

    def overview(self):
        from charts import overview
        overview()

“退出”按钮退出可视窗口和 GUI 本身。退出按钮代码:

        self.btn6=Button(win, text='Quit')
        self.b6=Button(win, text='Quit', width='6', height='2', command=window.quit)
        self.b6.place(x=50, y=230)

    def quit(self):
        self.win.destroy()

除“退出”按钮外,所有功能都按预期工作。我必须单击“退出”的次数与 GUI 打开的窗口数一样多才能使其工作。

即我启动 GUI 并打开“概览”视觉;我需要单击“退出”两次以关闭“概览”和 GUI 本身。

我已经尝试将退出按钮“命令”和退出功能调整为带有前缀 self、win 和 window(并且没有前缀)的 destroy() / quit() 的所有组合。

标签: pythontkinter

解决方案


尝试使用Python调用它的内置函数quit()只会退出程序,杀死它的进程。

def quit(self):
    quit()

推荐阅读