Search code examples
pythontkinterradio-buttonframe

Why aren't radiobuttons working in a Tkinter window with multiple frames?


I copied Python code to create a Tkinter window with multiple frames. I put many kinds of widgets into it with no problem but when I add radiobuttons, those act funny, although they work fine in a regular window (without multiple pages). Whether I set the value or not, none of the radiobuttons are selected. What's worse, if I just pass the mouse pointer over a radiobutton, it looks like it gets selected although I didn't click it. If I pass the mouse pointer over both radiobuttons, they BOTH look selected, violating the one-of-many-selections rule of radiobuttons.

I should add that I tried this with a pack manager and with a grid manager. The results are the same.

Here's a stripped-down version of my code:

import tkinter as tk

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # Set the title of the main window.
        self.title('Multi-frame window')
        # Set the size of the main window to 300x300 pixels.
        self.geometry('300x100')

        # This container contains all the pages.
        container = tk.Frame(self)
        container.grid(row=1, column=1)
        self.frames = {} # These are pages to which we want to navigate.

        # For each page...
        for F in (StartPage, PageOne):
            # ...create the page...
            frame = F(container, self)
            # ...store it in a frame...
            self.frames[F] = frame
            # ..and position the page in the container.
            frame.grid(row=0, column=0, sticky='nsew')

        # The first page is StartPage.
        self.show_frame(StartPage)

    def show_frame(self, name):
        frame = self.frames[name]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text='Start Page')
        label.grid(row=1, column=1)

        # When the user clicks on this button, call the
        #   show_frame method to make PageOne appear.
        button1 = tk.Button(self, text='Visit Page 1',
            command=lambda : controller.show_frame(PageOne))
        button1.grid(row=2, column=1)

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # When the user clicks on this button, call the
        #   show_frame method to make StartPage appear.
        button1 = tk.Button(self, text='Back to Start',
            command=lambda : controller.show_frame(StartPage))
        button1.grid(row=1, column=1)
        options_label = tk.Label(self, text='Choose an option: ')
        options_label.grid(row=2, column=1)
        options_value = tk.IntVar()

        first_option = tk.Radiobutton( self , text = 'Option 1' ,
            variable = options_value , value = 1 )
        second_option = tk.Radiobutton( self , text = 'Option 2' ,
            variable = options_value , value = 2 )
        first_option.grid(row=2, column=2)
        second_option.grid(row=2, column=3)
        options_value.set(1)

if __name__ == '__main__':
    app = MainWindow()
    app.mainloop()

Solution

  • The problem is that options_value is a local value that gets destroyed when __init__ finishes.

    You need to save a reference to it, such as self.options_value.