var=IntVar() rb1=Radiobutton(root,text='50',variable=var,value=1) rb2=Radiobutton(root,text='100',variable=var,value=2) rb3=Radiobutton(root,text='200',variable=var,value=3) rb4=Radiobutton(root,text='500',variable=var,value=4) rb1.place(x=150, y=125) rb2.place(x=200, y=125) rb3.place(x=150, y=150) rb4.place(x=200, y=150) 

Take the part of the code that creates 4 buttons with the choice of one of them. Suppose the user clicked on rb2 in the window. As I understand it, the data should be stored in the value variable, but after trying to display it on the screen, I received an error. What variable is stored in the data that the choice of the user rb2 ?

    2 answers 2

    You need to create event handling actions for clicking the button. You can either create a Button two ways and assign a check function to it, as in the example and using var.get() to get the value from Radiobutton

     from tkinter import * def check(): radio_button = var.get() print(radio_button) root = Tk() var = IntVar() rb1 = Radiobutton(root,text='50',variable=var,value=1) rb2 = Radiobutton(root,text='100',variable=var,value=2) rb3 = Radiobutton(root,text='200',variable=var,value=3) rb4 = Radiobutton(root,text='500',variable=var,value=4) rb1.place(x=50, y=125) rb2.place(x=100, y=125) rb3.place(x=50, y=150) rb4.place(x=100, y=150) button = Button(root, text='ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Ρ€Π°Π΄ΠΈΠΎΠΊΠ½ΠΎΠΏΠΊΠΈ', command=check) button.place(x=0, y=0) root.mainloop() 

    Or assign a command to each Radiobutton and then use the same method to get the value

     from tkinter import * def check(): radio_button = var.get() print(radio_button) root = Tk() var = IntVar() rb1 = Radiobutton(root, text='50', variable=var, value=1, command=check) rb2 = Radiobutton(root, text='100', variable=var, value=2, command=check) rb3 = Radiobutton(root, text='200', variable=var, value=3, command=check) rb4 = Radiobutton(root, text='500', variable=var, value=4, command=check) rb1.place(x=50, y=125) rb2.place(x=100, y=125) rb3.place(x=50, y=150) rb4.place(x=100, y=150) root.mainloop() 

      As I understand the data should be stored in the variable value

      In fact, the data is stored in the variable var (there is no value variable in the code, it is a named constructor parameter). You can get the current value of RadioButton calling the get() method on the var variable:

       var = IntVar() button1 = Radiobutton(root, text='sometext', variable=var, value=1) ... print(var.get()) # ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ состояниС Ρ€Π°Π΄ΠΈΠΎΠΊΠ½ΠΎΠΏΠΎΠΊ