This is my first question, I am new to python and this site.
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. 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. 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()