Dan S.
Dan S.

Reputation: 1

Tkinter radiobuttons not selecting properly

I'm trying to make a small tkinter program to calculate the area of shapes and volumes and I've made a main py file "area_volume_calculator.py" and two libraries "area.py" and "volume.py"

The main program's radio buttons work fine, but when I create the second window from Area_calculator I can't seem to get radio buttons to work properly. When it opens, the last 2 radio buttons are selected already.

I can still select an option but self.choice.get() always returns the default value which is -1, I don't know how to get it to work.

Can anyone help me get the radio buttons to work properly? Any and all help is appreciated

Main Program:

from tkinter import *

from area import *

# from volume import *

class Menu:
    def __init__(self):
        self.window = Tk()
        self.window.title("Areas and Volumes")
    
    
        # self.name_label = Label(self.window, text = "Name: ")
        # self.name_label.grid(row=0,column=0)
    
        # self.name_entry = Entry(self.window)
        # self.name_entry.grid(row=0,column=1)
    
        # self.age_label = Label(self.window, text = "Age: ")
        # self.age_label.grid(row=1,column=0)
    
        # self.age_entry = Entry(self.window)
        # self.age_entry.grid(row=1,column=1)
    
        self.choice = IntVar()
    
        self.area_radiobutton = Radiobutton(self.window, text = "Area Calculator", var = self.choice, value = 0)
        self.area_radiobutton.grid(row=2,column=0)
    
        self.volume_radiobutton = Radiobutton(self.window, text = "Volume Calculator", var = self.choice, value = 1)
        self.volume_radiobutton.grid(row=3,column=0)
    
        self.choice.set(-1)
    
        self.submit_button = Button(self.window, text = "Submit", command = self.submit)
        self.submit_button.grid(row=4,column=0)
    
        print(self.choice.get())
    
        self.window.mainloop()
    
    def submit(self):
        print(self.choice.get())

        if self.choice.get() == 0:
            area_calculator = Area_calculator()
    
        # elif self.choice.get() == 1:
        #     volume_calculator = Volume_calculator()


menu = Menu()

area.py library:

from tkinter import *
class Area_calculator():
    def __init__(self):
        self.window = Tk()
        self.window.title
        self.window.title("Area Calculator")
    
        self.choice_label = Label(self.window, text = "Choose a shape")
        self.choice_label.grid(row=0,columnspan=1)
    
        self.choice = IntVar()
    
        self.rectangle_radiobutton = Radiobutton(self.window, text = "Rectangle", var = self.choice, value = 0)
        self.rectangle_radiobutton.grid(row=1,column=0)
    
        self.triangle_radiobutton = Radiobutton(self.window, text = "Triangle", var = self.choice, value = 1)
        self.triangle_radiobutton.grid(row=2,column=0)
    
        self.circle_radiobutton = Radiobutton(self.window, text = "Circle", var = self.choice, value = 2)
        self.circle_radiobutton.grid(row=3,column=0)
    
        self.submit_button = Button(self.window, text = "Submit", command = self.submit)
        self.submit_button.grid(row=4,column=0)
    
        self.choice.set(-1)
        print(self.choice.get())
    
        self.window.mainloop()
    
    def submit(self):
        print(self.choice.get())
#        if self.choice.get() == 0:
#            rectangle_calculator = Rectangle_calculator()
#        elif self.choice.get() == 1:
#            triangle_calculator = Triangle_calculator()
#        elif self.choice.get() == 2:
#            circle_calculator = Circle_calculator()

Upvotes: 0

Views: 130

Answers (1)

Lecdi
Lecdi

Reputation: 2241

You are creating two instances of tk.Tk() (one in each class) which is never a good idea. tk.Tk() does not just create a new window but also a new embedded Tcl interpreter and this can mess things up, particularly IntVars and other variables such as self.choice. For more information see here.

If you want to have multiple windows then use tk.Toplevel() instead.

In answer to your second question in the comments, you must first add the parameters name and age to the __init__ of the Area_calculator class, and also pass them to the class when creating it in Menu.

Completed code:

Main program:

from tkinter import *

from area import *


class Menu:
    def __init__(self):
        self.window = Tk()
        self.window.title("Areas and Volumes")
    
        self.name_label = Label(self.window, text = "Name: ")
        self.name_label.grid(row=0,column=0)
    
        self.name_entry = Entry(self.window)
        self.name_entry.grid(row=0,column=1)
    
        self.age_label = Label(self.window, text = "Age: ")
        self.age_label.grid(row=1,column=0)
    
        self.age_entry = Entry(self.window)
        self.age_entry.grid(row=1,column=1)
    
        self.choice = IntVar()
    
        self.area_radiobutton = Radiobutton(self.window, text = "Area Calculator", var = self.choice, value = 0)
        self.area_radiobutton.grid(row=2,column=0)
    
        self.volume_radiobutton = Radiobutton(self.window, text = "Volume Calculator", var = self.choice, value = 1)
        self.volume_radiobutton.grid(row=3,column=0)
    
        self.choice.set(-1)
    
        self.submit_button = Button(self.window, text = "Submit", command = self.submit)
        self.submit_button.grid(row=4,column=0)
    
        print(self.choice.get())
    
        self.window.mainloop()
    
    def submit(self):
        print(self.choice.get())

        if self.choice.get() == 0:
            name = self.name_entry.get()
            age = self.age_entry.get()
            area_calculator = Area_calculator(name, age)


menu = Menu()

area.py library:

from tkinter import *


class Area_calculator():
    def __init__(self, name, age):
        self.window = Toplevel()
        self.window.title
        self.window.title("Area Calculator")

        self.name_label = Label(self.window, text = f"{name=}", width=10)
        self.name_label.grid()

        self.age_label = Label(self.window, text = f"{age=}", width=10)
        self.age_label.grid(row=0, column=1)
    
        self.choice_label = Label(self.window, text = "Choose a shape")
        self.choice_label.grid(row=1,columnspan=1)
    
        self.choice = IntVar()
    
        self.rectangle_radiobutton = Radiobutton(self.window, text = "Rectangle", var = self.choice, value = 0)
        self.rectangle_radiobutton.grid(row=2,column=0)
    
        self.triangle_radiobutton = Radiobutton(self.window, text = "Triangle", var = self.choice, value = 1)
        self.triangle_radiobutton.grid(row=3,column=0)
    
        self.circle_radiobutton = Radiobutton(self.window, text = "Circle", var = self.choice, value = 2)
        self.circle_radiobutton.grid(row=4,column=0)
    
        self.submit_button = Button(self.window, text = "Submit", command = self.submit)
        self.submit_button.grid(row=5,column=0)
    
        self.choice.set(-1)
        print(self.choice.get())
    
        self.window.mainloop()
    
    def submit(self):
        print(self.choice.get())

Upvotes: 2

Related Questions