tkinter radiobutton value no pass to label, remain at initial value

Question:

I try to display the current variable for the radio button but it seems remain at the initial value which is zero.

my code was as below:

single_magnet_plot_style=IntVar(value=0)

Radio Button variable assignment:

scatter_rbtn=Radiobutton(window, text="Scatter Plot", variable=single_magnet_plot_style,value=1)
scatter_rbtn.place(relx=0.4,y=600)
        
line_rbtn=Radiobutton(window, text="Line Plot", variable=single_magnet_plot_style,value=2)
line_rbtn.place(relx=0.6, y=600)

label display:

new_label=Label(window, text=single_magnet_plot_style.get(), fg='red', font=("Helvetica", 13))
new_label.place(relx=0.5, y=550,anchor=CENTER)

at my window, the label constantly stay at value 0.

enter image description here

Any advice to return the new_label from the radio button value ?

Asked By: Chun Seong

||

Answers:

You need to use option textvariable instead of text if you want the label be updated whenever the selection of radiobuttons is changed:

new_label=Label(window, textvariable=single_magnet_plot_style, fg='red', font=("Helvetica", 13))
Answered By: acw1668
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.