Question

I'm trying to build an app with pythons tkinter module.

Currently I'm trying to get a labels displayed text to change when a radiobutton is selected. I have the labels text set to a textvariable, and a function which changes the textvariable to the required text depending on which button is selected. However, I'm expecting the Labels text to change since it's governing textvariable has changed. However is doesn't update.

Any help to get it updating will be greatly appreciated thanks.

info = Label(mainwindow, bg = 'magenta', height = 10, width = 40, text = weatherinfo, font = ('arial', 14, 'normal'))
info.pack(side = LEFT,padx = 20)

weatherinfo = 'select your city'

Then my check function changes weatherinfo

weatherinfo = '\n'.join([z, y, x, w, v, u,t])
print weatherinfo

The correct values print out on the shell but the original info label doesnt update and remains displaying 'select your city'

Was it helpful?

Solution

In this code, Label updates every click of Radiobutton using command option of Radiobutton and Label's text variable.

from Tkinter import *

def radio_action():
    value1 = var1.get()
    changeable_label['text'] = value1

the_window = Tk()
the_window.title('Example')

the_window.geometry("200x150")

var1 = StringVar()

changeable_label = Label(the_window, text = "Null")
button1 = Radiobutton(the_window, text = 'Button1', variable = var1,
                     value="Something", command = radio_action)

changeable_label.pack(side = TOP)
button1.pack()


the_window.mainloop()

Since I don't know your whole code, this is I can help with an example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top