Oliver Almonte
Oliver Almonte

Reputation: 9

Displaying Tkinter Label with a variable

Having trouble displaying a variable for the Tkinter Label Code example

q=StringVar()
q="blah blah" 

test = Label(main, textvariable=q)
test.pack()

I try this code and nothing displays. I have also tried

q="blah blah"
test = Label(main, text= )+q 
test.pack()

but that still doesn't. I want label to display "blah blah" when I run.

Upvotes: 0

Views: 2250

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

First your assigning q to be a StringVar() and then your asking it to be a string. To set the value of a tkinter variable, say q.set('bla bla') or you can also define it while defining variable itself, like q = StringVar(value='bal bla').

To verify and understand better, say:

q = StringVar()
print(type(q))
q = 'bla bla'
print(type(q))

You will notice it will show a tkinter.StringVar first and then a str class for the second. So they both are not same.

Or as per your second example it should be something like:

q = 'bla bla'
test = Label(main, text=q)

What you were trying to do was concatenating a class and a string, which would give an error. Moreover you will get syntax error first, as your not passing anything to the text argument of Label().

Upvotes: 1

Related Questions