Python Forum
[Tkinter] I can't get information from a radio button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] I can't get information from a radio button
#1
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:

#!/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.

Attached Files

Thumbnail(s)
   
Reply
#2
Comments added to altered code shown below solving all the problems.
#!/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()
        tools.set(1)  # set 1 as the default
        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).pack() # don't call __OpenTool ie remove () pass in a callable.
        ttk.Button(self.parent, text="Settings",
                   command=self.__OpenSettings).pack()
        self.tools = tools  # make tools accessable in methods

    def __OpenTool(self): # removed variable
        tools_value = self.tools.get()
        if tools_value == 1:
            print('first')
            # FirstWindow(self.parent)
            # self.parent.withdraw()
        elif tools_value == 2:
            print('2')
            # SecondWindow(self.parent)
            # self.parent.withdraw()
        elif tools_value == 3:
            print('3')
            # ThirdWindow(self.parent)
            # self.parent.withdraw()
        elif tools_value == 4:
            print('4')
            # FourthWindow(self.parent)
            # self.parent.withdraw()
        elif tools_value == 5:
            print('5')
            # FifthWindow(self.parent)
            # self.parent.withdraw()
        else:
            print("???")

    def __OpenSettings(self):
        print('open setting window')
        # SixthWindow(self.parent)


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


if __name__ == "__main__":
    main()
Reply
#3
Thanks Yoriz!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Is there a way to determine if a radio button has been selected? TWB 5 4,741 Jan-31-2023, 09:44 AM
Last Post: Vadanane
  [Tkinter] Radio Buttons Working Bassackwards gw1500se 6 2,254 Dec-07-2021, 07:13 PM
Last Post: menator01
  [Tkinter] Grid the radio buttons Joni_Engr 6 4,663 Nov-24-2021, 07:20 PM
Last Post: menator01
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 5,395 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  problem with radio button crook79 3 3,625 Aug-12-2021, 02:30 PM
Last Post: deanhystad
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,947 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Radio button help Muzz 5 3,626 Apr-28-2019, 07:43 AM
Last Post: Muzz
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 11,766 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020