Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Tkinter.Label because the Tkinter.Listbox will not allow for newlines.

The kicker is there is no .get()-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 StringVar()'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.

Is Tkinter.Label the right widget to be using?

like image 769
lmno Avatar asked May 24 '11 14:05

lmno


People also ask

Can you change the text of a label in Tkinter?

Tkinter Label widgets are commonly used in applications to show text or images. You can change the label widget's text property, color, background, and foreground colors using different methods. You can update the text of the label widget using a button and a function if you need to tweak or change it dynamically.

What is CGET () in Tkinter?

An Entry widget in Tkinter is nothing but an input widget that accepts single-line user input in a text field. To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.


2 Answers

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.

For example:

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 use l["text"].

like image 68
Bryan Oakley Avatar answered Sep 18 '22 16:09

Bryan Oakley


label = Label(text = 'Hello, World!') print(label['text']) # output is: Hello, World! 
like image 25
Yas Avatar answered Sep 20 '22 16:09

Yas