简体   繁体   English

在python中使用tkinter从顶级窗口关闭主窗口

[英]Closing main window from toplevel window with tkinter in python

This is my first question, I am new to python and this site. 这是我的第一个问题,我是python和此站点的新手。

I am designing an app to interact with a database. 我正在设计一个与数据库交互的应用程序。 I added a "close" button that I would like to open a new window asking "close the program?" 我添加了一个“关闭”按钮,我想打开一个新窗口询问“关闭程序?”。 and 2 buttons: yes and no. 和2个按钮:是和否。 When you click no, the new window closes. 当您单击否时,新窗口关闭。 When you click yes both windows close. 当您单击是时,两个窗口都关闭。

I got my code working but I am quite sure there is a better or smarter way of doing it. 我的代码可以正常工作,但我可以肯定有更好或更聪明的方式可以做到这一点。

To make it work I had to write "root.destroy()" in the "close_window" method but I am pretty sure there is a smarter way of getting the same result with something like "self.master.destroy()" that uses all the power of python. 为了使其正常工作,我必须在“ close_window”方法中编写“ root.destroy()”,但是我敢肯定,可以通过“ self.master.destroy()”之类的方法来获得相同结果的更聪明的方法。 python的所有功能。 I show a simplified version of the code below. 我在下面显示了代码的简化版本。

Thanks in advance. 提前致谢。

Alfonso 阿方索

from tkinter import *

class Window():

    def __init__(self, main):
        self.main = main

        self.b5=Button(self.main, text="Action 1", width=12)
        self.b5.grid(row=0, column=1)

        self.b5=Button(self.main, text="Action 2", width=12)
        self.b5.grid(row=0, column=2)

        self.b6=Button(self.main, text="Close", width=12, command=self.new_window)
        self.b6.grid(row=0, column=3)

    def new_window(self):
        self.newWindow = Toplevel(self.main)
        self.app = Demo2(self.newWindow)

    def close_window(self):
        root.destroy()


class Demo2:
    def __init__(self, master):
        self.master = master
        self.frame = Frame(self.master)

        self.l1=Label(self.frame, text="Close the program?")
        self.l1.grid(row=0, column=0, columnspan=2)

        self.b1=Button(self.frame, text="Yes", command=self.yes_com)
        self.b1.grid(row=1, column=0)

        self.b1=Button(self.frame, text="No", command=self.no_com)
        self.b1.grid(row=1, column=1)

        self.frame.pack()

    def yes_com(self):
        self.master.destroy()
        Window.close_window(self)

    def no_com(self):
        self.master.destroy()


def main():
    global root
    root = Tk()
    app = Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()

You could simply use the standard dialogs provided by the messagebox module. 您可以简单地使用messagebox模块提供的标准对话框。

Specifically, it provides the askyesno dialog that takes care of the opening/closing of the new window and returns True if the user clicks Yes and False if the user clicks No . 具体来说,它提供了askyesno对话框,采取新窗口的打开/关闭的照顾,并返回True如果用户点击YesFalse ,如果用户单击No Then, you could simply use an if -statement to close the window if the user says so by simply using self.main.destroy() (without the need to declare root as global ). 然后,你可以简单地使用if语句来来,如果用户通过简单地利用这样说关闭窗口self.main.destroy()无需申报root作为global )。

from tkinter import *
from tkinter import messagebox

class Window():

    def __init__(self, main):
        self.main = main

        self.b5=Button(self.main, text="Action 1", width=12)
        self.b5.grid(row=0, column=1)

        self.b5=Button(self.main, text="Action 2", width=12)
        self.b5.grid(row=0, column=2)

        self.b6=Button(self.main, text="Close", width=12, command=self.close_window)
        self.b6.grid(row=0, column=3)

    def close_window(self):
        if messagebox.askyesno('Close', 'Close the program?'):
            self.main.destroy()

def main():
    root = Tk()
    app = Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Notes 笔记

  1. The yes/no buttons in the standard dialog are localized , meaning that they are translated into the language used by your computer settings. 标准对话框中的是/否按钮是本地化的 ,这意味着它们已翻译为您的计算机设置所使用的语言。
  2. If you have a look around the messagebox module, you'll see that there are other standard dialogs. 如果您查看messagebox模块,将会看到还有其他标准对话框。 IMHO in this case I would use askyescancel , which is used in the exact same way, but seems more semantic. 恕我直言,在这种情况下,我将使用askyescancel ,它的使用方式完全相同,但似乎更具语义。

Pier Paolo has the right answer, but for the sake of science, here's the direct answer to your question: 皮埃尔·保罗(Pier Paolo)给出了正确的答案,但是为了科学起见,这是您问题的直接答案:

def yes_com(self):
    self.master.master.destroy() # <this class instance>.<toplevel instance>.<Tk instance>.destroy()

Or you could simply quit python: 或者您可以简单地退出python:

import sys

def yes_com(self):
    sys.exit()

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

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