Reputation:
I'm trying to create a settings window, with multiple groups of TkInter Radiobutton
s. They should modify a text variable that I can work with later on.
I have the following code:
# radiobutton group 1
settingSort = ""
settingSortRadio1 = tkinter.Radiobutton(settingsWindow, text="Frequency", variable=settingSort, value="freq")
settingSortRadio1.select()
settingSortRadio1.pack()
settingSortRadio2 = tkinter.Radiobutton(settingsWindow, text="Alphabetical", variable=settingSort, value="alpha")
settingSortRadio2.pack()
#radiobutton group 2
settingAnalyseRadio1 = tkinter.Radiobutton(settingsWindow, text="Word frequency", variable=settingAnalyse, value="wfreq")
settingAnalyseRadio1.select()
settingAnalyseRadio1.pack()
settingAnalyseRadio2 = tkinter.Radiobutton(settingsWindow, text="Letter frequency", variable=settingAnalyse, value="lfreq")
settingAnalyseRadio2.pack()
However, these buttons all seem to be part of the same group still. Selecting a button in group 1 will deselect all others in the window (including in group 2), and vice versa.
How can I fix this?
Upvotes: 3
Views: 12229
Reputation: 4740
This is because you're not using the in built variable classes from tkinter.
You need to do something like the below:
from tkinter import *
root = Tk()
var1 = StringVar()
var2 = StringVar()
var1.set(0)
var2.set(0)
Radiobutton(root, text = "group1", variable = var1, value = 0).pack()
Radiobutton(root, text = "group1", variable = var1, value = 1).pack()
Radiobutton(root, text = "group2", variable = var2, value = 0).pack()
Radiobutton(root, text = "group2", variable = var2, value = 1).pack()
root.mainloop()
Let's break this down to make it easier to understand.
var1 = StringVar()
var2 = StringVar()
So above we create two StringVar()
variables. As far as you need to be concerned these are just "containers" which store the value of specific widgets within themselves. We create two because we have two different groups of Radiobutton
widgets.
var1.set(0)
var2.set(0)
Above we set the value of the StringVar()
variables to be the same as the values of the first Radiobutton
widgets of each group (which we're about to initialise). This means that when the Radiobutton
s are drawn, the first one in each group will start selected by default.
Radiobutton(root, text = "group1", variable = var1, value = 0).pack()
Radiobutton(root, text = "group1", variable = var1, value = 1).pack()
Radiobutton(root, text = "group2", variable = var2, value = 0).pack()
Radiobutton(root, text = "group2", variable = var2, value = 1).pack()
In the above we initialise the Radiobutton
widgets. We use the variable
attribute of each Radiobutton
to assign the variable class associated with each group (var1
for group 1 and var2
for group 2) and then we use the value
attribute to assign the value that the Radiobutton
s should "store" in their associated variables.
This allows us to create 4 Radiobutton
s in 2 "groups" which can update two variables independent of the other "group".
Upvotes: 4
Reputation: 22804
That does not work because you need to use a variable class instead of what you did.
I mean, you need to change this line:
settingSort = ""
to:
settingSort = tkinter.StringVar()
Then you need to initialize settingSort
according to your specific need.
Upvotes: 1