Why does Tkinter Radio Buttons all start when using StringVar but not IntVar?

Here is an example code that creates 4 switches, 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 starting, I have a field that looks like this:

For some reason, str starts with a choice, but int does not. Is there a reason for this? Is there any fix? I know I can get around this by simply using the valuse number and then converting them to strings, but I would like to understand why this happens in the first place.

I use windows 10 if that matters.

edit: for clearing, the butting still works correctly after it has been pressed.

Thank you for your help

+4
1

" ".

1:

tristateValue, . .

tristatevalue , StringVar . tristatevalue , " ".

IntVar . tristatevalue - . , " ".

, tristatevalue , , , .

int1 = Radiobutton(..., tristatevalue=0)
int2 = Radiobutton(..., tristatevalue=0)

, tristatevalue , , :

str1 = Radiobutton(..., tristatevalue="x")
str2 = Radiobutton(..., tristatevalue="x")

- , ( " ".

:

self.v1 = IntVar(value=1)
self.v2 = StringVar(value="1")

... set:

self.v1.set(1)
self.v2.set("1")

1 man- tcl/tk. Tkinter - tcl/tk, , .

+7

Source: https://habr.com/ru/post/1661233/


All Articles