Here is some example code that creates 4 Radiobutton
s, 2 using int
and 2 using str
:
from tkinter import *
class test:
def __init__(self):
wind = Tk()
frame1 = Frame(wind)
frame1.pack()
self.v1 = IntVar()
self.v2 = StringVar()
int1 = Radiobutton(frame1, text = 'int1', variable = self.v1, value = 1,
command = self.ipress)
int2 = Radiobutton(frame1, text = 'int2', variable = self.v1, value = 2,
command = self.ipress)
str1 = Radiobutton(frame1, text = 'str1', variable = self.v2, value = '1',
command = self.spress)
str2 = Radiobutton(frame1, text = 'str2', variable = self.v2, value = '2',
command = self.spress)
int1.grid(row = 1, column = 1)
int2.grid(row = 1, column = 2)
str1.grid(row = 2, column = 1)
str2.grid(row = 2, column = 2)
str1.deselect() #this didn't fix it
str2.deselect()
def ipress(self):
print('int'+str(self.v1.get()))
def spress(self):
print('str'+self.v2.get())
test()
After running this I have a box that look like this:
For some reason the str
ones start selected while the int
ones do not. Is there a reason for this? Is there any fix for it? I know I could work around it by just using number values and then converting them to strings but I'd like to understand why this is happening first.
I am using Windows 10 if that matters.
Edit: for clarification, the buttons still work properly after they have been clicked on.
self.v2.set(None)
. – Flock