lmno lmno - 1 year ago 148
Python Question

How to get the Tkinter Label text?

Im making a list of addresses that the user will select from, and the address text will be returned. I need to use

because the
will not allow for newlines.

The kicker is there is no
-like method in the Label class...

I know I can do something like:

v = StringVar()
Label(master, textvariable=v).pack()
v.set("New Text!")
print v.get()

However, I have a list of 5-20 address' keeping a seperate array of
's will be difficult b/c I have no way of identifying the loc of the active label. I would like to just access the activated widget contents.

the right widget to be using?

Answer Source

To get the value out of a label you can use the cget method, which can be used to get the value of any of the configuration options (in this case, the option text).

l = tk.Label(text="hello, world")
print "the label is", l.cget("text")

You can also treat the object as a dictionary, using the options as keys. Using the same example you can do l["text"].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download