hi guys,
I wrote a GUI with a main window and other ones. if you want to go ahead with a secondary window, first you have to choose one of them using the radio buttons, and then click on "OK" to open the new window.
the behaviour is simple but I can't to take the value associated to the radio button and without this information the "OK" button cannot work. below my code:
1. where did I wrong? and how can I fix the issue?
2. why when I use a function with an extra argument than "self", associated to a widget, it always starts automatically without any kind of interaction with the user? in my specific case I'm talking about the "self.__OpenTool(tools)" statement. when I opened the software it started itself. I know it because on the terminal has been printed the string "???" (see my code and my screenshot attached). I really don't understand this behaviour.
3. when the issue will be solved, what about to have always the first radio button checked? when I open my software, all radio buttons are uncheked and from my side I don't like this behaviour. at least one of them must be always checked.
I wrote a GUI with a main window and other ones. if you want to go ahead with a secondary window, first you have to choose one of them using the radio buttons, and then click on "OK" to open the new window.
the behaviour is simple but I can't to take the value associated to the radio button and without this information the "OK" button cannot work. below my code:
#!/usr/bin/python3 from tkinter import * from tkinter import ttk class MainWindow: def __init__(self, parent): # definisco gli attributi della finestra principale: self.parent = parent parent.title("My Software") parent.iconbitmap("icon.ico") parent.geometry("530x515+360+200") parent.resizable (width=False, height=False) parent.configure(background="#f0f0f0") # Radio button "tools": tools = IntVar() ttk.Radiobutton(self.parent, text = "Menu 1", variable = tools, value = 1).pack() ttk.Radiobutton(self.parent, text = "Menu 2", variable = tools, value = 2).pack() ttk.Radiobutton(self.parent, text = "Menu 3", variable = tools, value = 3).pack() ttk.Radiobutton(self.parent, text = "Menu 4", variable = tools, value = 4).pack() ttk.Radiobutton(self.parent, text = "Menu 5", variable = tools, value = 5).pack() # bottoni "Settings" e "OK": ttk.Button(self.parent, text = "OK", command = self.__OpenTool(tools)).pack() ttk.Button(self.parent, text = "Settings", command = self.__OpenSettings).pack() def __OpenTool(self, variable): if variable.get() == 1: FirstWindow(self.parent) self.parent.withdraw() elif variable.get() == 2: SecondWindow(self.parent) self.parent.withdraw() elif variable.get() == 3: ThirdWindow(self.parent) self.parent.withdraw() elif variable.get() == 4: FourthWindow(self.parent) self.parent.withdraw() elif variable.get() == 5: FifthWindow(self.parent) self.parent.withdraw() else: print("???") def __OpenSettings(self): SixthWindow(self.parent) class FirstWindow: def __init__(self, parent): # definisco gli attributi della finestra "ntw": self.parent = parent self.window = Toplevel(parent=None) self.window.title("My Software - Menu 1") self.window.iconbitmap("icon.ico") self.window.geometry("530x515+360+200") self.window.resizable (width=False, height=False) self.window.configure(background="#f0f0f0") # se chiudo la finestra: self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent) def __CloseEvent(self): self.parent.deiconify() self.window.destroy() class SecondWindow: def __init__(self, parent): # definisco gli attributi della finestra "ntw": self.parent = parent self.window = Toplevel(parent=None) self.window.title("My Software - Menu 2") self.window.iconbitmap("icon.ico") self.window.geometry("530x515+360+200") self.window.resizable (width=False, height=False) self.window.configure(background="#f0f0f0") # se chiudo la finestra: self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent) def __CloseEvent(self): self.parent.deiconify() self.window.destroy() class ThirdWindow: def __init__(self, parent): # definisco gli attributi della finestra "ntw": self.parent = parent self.window = Toplevel(parent=None) self.window.title("My Software - Menu 3") self.window.iconbitmap("icon.ico") self.window.geometry("530x515+360+200") self.window.resizable (width=False, height=False) self.window.configure(background="#f0f0f0") # definisco i diversi widget di questa finestra: frame = ttk.Frame(self.window) frame.config(height = 465, width = 505) # se chiudo la finestra: self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent) def __CloseEvent(self): self.parent.deiconify() self.window.destroy() class FourthWindow: def __init__(self, parent): # definisco gli attributi della finestra "ntw": self.parent = parent self.window = Toplevel(parent=None) self.window.title("My Software - Menu 4") self.window.iconbitmap("icon.ico") self.window.geometry("530x515+360+200") self.window.resizable (width=False, height=False) self.window.configure(background="#f0f0f0") # se chiudo la finestra: self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent) def __CloseEvent(self): self.parent.deiconify() self.window.destroy() class FifthWindow: def __init__(self, parent): # definisco gli attributi della finestra "ntw": self.parent = parent self.window = Toplevel(parent=None) self.window.title("My Software - Menu 5") self.window.iconbitmap("icon.ico") self.window.geometry("530x515+360+200") self.window.resizable (width=False, height=False) self.window.configure(background="#f0f0f0") # se chiudo la finestra: self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent) def __CloseEvent(self): self.parent.deiconify() self.window.destroy() class SixthWindow: def __init__(self, parent): # definisco gli attributi della finestra "ntw": self.parent = parent self.window = Toplevel(parent=None) self.window.title("My Software - Settings") self.window.iconbitmap("icon.ico") self.window.geometry("530x515+360+200") self.window.resizable (width=False, height=False) self.window.configure(background="#f0f0f0") # se chiudo la finestra: self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent) def __CloseEvent(self): self.parent.deiconify() self.window.destroy() def main(): root = Tk() app = MainWindow(root) root.mainloop() if __name__ == "__main__": main()now I have three questions:
1. where did I wrong? and how can I fix the issue?
2. why when I use a function with an extra argument than "self", associated to a widget, it always starts automatically without any kind of interaction with the user? in my specific case I'm talking about the "self.__OpenTool(tools)" statement. when I opened the software it started itself. I know it because on the terminal has been printed the string "???" (see my code and my screenshot attached). I really don't understand this behaviour.
3. when the issue will be solved, what about to have always the first radio button checked? when I open my software, all radio buttons are uncheked and from my side I don't like this behaviour. at least one of them must be always checked.