Discussion:
[Tkinter-discuss] Problem using StringVar()
Chris Roy-Smith
2014-09-01 02:46:53 UTC
Permalink
Just trying to learn to use Tkinter.

python 2.7.3 OS Ubuntu 12.4

This code
-----------------------
#!/usr/bin/python
import Tkinter
top = Tkinter.Tk()

v = top.StringVar()
Label(master, textvariable=v).pack()

v.set("hello")

top.mainloop()
---------------------------
returns this error message

Traceback (most recent call last):
File "./hello.py", line 5, in <module>
v = top.StringVar()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __getattr__
return getattr(self.tk, attr)
AttributeError: StringVar


Please what am I doing wrong?
Michael O'Donnell
2014-09-01 07:16:00 UTC
Permalink
Dear Chris,

StringVar is a method in the module Tkinter, it is not a method of a
Tk instance.
Also, you reference an undefined "master" in the Label line.
Thus, the following should work:

from Tkinter import Tk, StringVar, Label
top =Tk()
v = StringVar()
Label(top, textvariable=v).pack()
v.set("hello")
top.mainloop()

Mick

On 1 September 2014 04:46, Chris Roy-Smith
Post by Chris Roy-Smith
Just trying to learn to use Tkinter.
python 2.7.3 OS Ubuntu 12.4
This code
-----------------------
#!/usr/bin/python
import Tkinter
top = Tkinter.Tk()
v = top.StringVar()
Label(master, textvariable=v).pack()
v.set("hello")
top.mainloop()
---------------------------
returns this error message
File "./hello.py", line 5, in <module>
v = top.StringVar()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __getattr__
return getattr(self.tk, attr)
AttributeError: StringVar
Please what am I doing wrong?
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss
Chris Roy-Smith
2014-09-01 07:47:54 UTC
Permalink
Post by Michael O'Donnell
from Tkinter import Tk, StringVar, Label
top =Tk()
v = StringVar()
Label(top, textvariable=v).pack()
v.set("hello")
top.mainloop()
Thanks for the pointer. The tutorial I was working through did not explain
where the StringVar() came from, and showed what I now understand as a
code snippet.

Chris Roy-Smith
2014-09-01 02:46:53 UTC
Permalink
Just trying to learn to use Tkinter.

python 2.7.3 OS Ubuntu 12.4

This code
-----------------------
#!/usr/bin/python
import Tkinter
top = Tkinter.Tk()

v = top.StringVar()
Label(master, textvariable=v).pack()

v.set("hello")

top.mainloop()
---------------------------
returns this error message

Traceback (most recent call last):
File "./hello.py", line 5, in <module>
v = top.StringVar()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __getattr__
return getattr(self.tk, attr)
AttributeError: StringVar


Please what am I doing wrong?
Loading...