Not getting value of ttk.Radiobutton

I have this code:

radio_var = tk.StringVar()

radio_1 = ttk.Radiobutton(okno, text = "Kulatý knoflík 1", variable = radio_var, value = "radio 1").pack() 
radio_2 = ttk.Radiobutton(okno, text = "Kulatý knoflík 2", variable = radio_var, value = 2).pack()

And I would expect to get the value of the value option to the console. But it is not happening. Why?

Hi,

try this as a replacement:

radio_var = tk.StringVar()

radio_1 = ttk.Radiobutton(okno, text = "Kulatý knoflík 1", value = "radio 1", variable = radio_var) 
radio_2 = ttk.Radiobutton(okno, text = "Kulatý knoflík 2", value = "radio 2", variable = radio_var)

radio_1.pack(fill = 'x', padx = 5, pady = 5) # Here, modify the values for x and y according your requirements
radio_2.pack(fill = 'x', padx = 5, pady = 5)

By the way, value was ‘2’ for radio_2. I modified it to a string radio 2. I figured it was a typo.
I also moved out the .pack methods outside to their own distinctive code. You will have to modify the values for x and y to suit your needs.

The easiest thing is to add a command to the buttons.

import tkinter as tk
from tkinter import ttk

okno = tk.Tk()

radio_var = tk.StringVar()
def radio_print(): print(radio_var.get())

radio_1 = ttk.Radiobutton(okno, text="Kulatý knoflík 1",
                          command=radio_print,
                          variable=radio_var, value="r1").pack() 
radio_2 = ttk.Radiobutton(okno, text = "Kulatý knoflík 2",
                          command=radio_print,
                          variable=radio_var, value="r2").pack()

okno.mainloop()

That’s assigning the result of pack to the variable.

I know. I just edited the OPs code. Should have deleted the assignment as not needed for the question.