hi guys,
I'm writing a graphical interface with Tkinter for some of my scripts and I would very like to know what do you think about my code because unfortunately I'm not an expert. I started to study Tkinter just recently.
my software should have a main window with many buttons, each of them associated to a specific menu (a new window). if you click one of them, the main window will close itself and a new window will open. if you close the latter, the main window will appear again, and from there you can choose another button to go ahead with a new window and so on. of course, if you close the main window, the software will be close definitivally.
to accomplish this goal, I wrote this code, it works but I wanted to know your opionions. from your side is it a good code? is it the right way to implement the behaviour that I just explained before?
I'm writing a graphical interface with Tkinter for some of my scripts and I would very like to know what do you think about my code because unfortunately I'm not an expert. I started to study Tkinter just recently.
my software should have a main window with many buttons, each of them associated to a specific menu (a new window). if you click one of them, the main window will close itself and a new window will open. if you close the latter, the main window will appear again, and from there you can choose another button to go ahead with a new window and so on. of course, if you close the main window, the software will be close definitivally.
to accomplish this goal, I wrote this code, it works but I wanted to know your opionions. from your side is it a good code? is it the right way to implement the behaviour that I just explained before?
#!/usr/bin/python3
from tkinter import *
from tkinter import ttk
class MainWindow:
def __init__(self, master):
# definisco gli attributi della finestra principale:
self.master = master
master.title("My Software")
master.iconbitmap("icon.ico")
master.geometry("500x400+250+250")
master.resizable (width=False, height=False)
master.configure(background="#f0f0f0")
# definisco i diversi widget di quest afinestra:
ttk.Button(master, text = "Menu 1", command = self.__Open).pack()
def __Open(self):
ntw = Tk()
app = SecondWindow(self.master, ntw)
ntw.mainloop()
class SecondWindow:
def __init__(self, mw, master):
# chiudo la finestra principale:
mw.destroy()
# definisco gli attributi della finestra "ntw":
self.master = master
master.title("My Software - Menu 1")
master.iconbitmap("icon.ico")
master.geometry("500x400+250+250")
master.resizable (width=False, height=False)
master.configure(background="#f0f0f0")
# se chiudo la finestra:
master.protocol("WM_DELETE_WINDOW", self.__CloseEvent)
def __CloseEvent(self):
self.master.destroy()
main()
def main():
root = Tk()
app = MainWindow(root)
root.mainloop()
if __name__ == "__main__":
main()I'm not sure about my code because to go ahead with a new window I must to destroy the main window, and create it again when I destroy the second window. maybe is it better don't destroy the main window?
