Home > Software engineering >  tkinter radiobutton value no pass to label, remain at initial value
tkinter radiobutton value no pass to label, remain at initial value

Time:01-11

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 ?

CodePudding user response:

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))
  • Related