rasPi2bSarah
rasPi2bSarah

Reputation: 63

py2.7 label text function fails

Button not putting text in label with btnWork function. This is as simple as I can make the code. I'm hoping, this will finally explain this problem for me. Please help me. Thanks So Much

from Tkinter import *

root = Tk()
root.geometry("200x200")
root.title('label')
root.configure(background='gray')

def btnWork():
    anyVar.set("wow!!!")

myBtn=Button(text="click",
             command=btnWork)
myBtn.pack()

anyVar = StringVar()
anyVar.set("0")

myLabel=Label(textvariable = "anyVar",
              width = 10)
myLabel.pack()

mainloop()

Upvotes: 0

Views: 40

Answers (2)

rasPi2bSarah
rasPi2bSarah

Reputation: 63

From Bryan Oakley's comments, the code should be:

myLabel=Label(textvariable = anyVar

Thanks Bryan

Upvotes: 2

Darius
Darius

Reputation: 12142

This is my approach without an instance of StringVar:

from Tkinter import *

root = Tk()

lab = Label(text="hello", width=10)
lab.pack()

def callback():
    lab.config(text='world') # Use config to change the value of 'text'

btn = Button(text="click me", command=callback)
btn.pack()

root.mainloop()

Upvotes: 1

Related Questions