This is my first experience with Python's Tkinter GUI. I want to write a program where the result of an interim calculation is displayed on a label. Later in the program I want to use this result in another calculation by simply clicking on the label.

This is what I have set up for a test:

from Tkinter import *

root = Tk()

label1 = Label(root, text='')
label1.pack(side=TOP)
# do some caculation and format result
pi_approx = 355/113.0
str1 = "%.4f" % (pi_approx)    # 3.1416
# show result in label text
label1.config(text=str1)

# more code here

mainloop()

I can bind the label to a mouse click, but can't figure out how to retrieve the label's content. Any advice how to do this? There must be some Tk mavens out there.

Hi Ene Uran,

There are two ways of doing this.

The first way: use the textvariable option in the Label constructor to set the Label text to the value of a StringVar maintained elsewhere in your program. Then, in your callback function, change the value of the StringVar and the Label should update automatically.

x = StringVar()
def callback(event):
    x.set("Two")
# Set x to the initial value and instantiate your Label here

The other way of doing it is to go through event.widget in your callback function - this lets you grab the widget making the call and make alterations to it.

def callback(event):
    event.widget.config(text="Something else")
# Instantiate your Label and bind the callback to it here

But really, the first method is the preferred method when dealing with Label text/images.

If you want to display and later retrieve results from a Tkinter GUI, it would be better to use the Text widget rather than the Label widget. The Text widget is a little more complex, but amongst the many things you can do with it, is the retrieval of the string or part of the string displayed.

Here is an example along the line you described ...

from Tkinter import *

def get_value(event):
    # retrieve text widget contents between starting_index and ending_index
    # starting_index = "%d.%d" % (line, column)  here "1.0"
    # note, line starts with 1 and column with 0
    # here ending_index = END
    txt1 = text1.get("1.0", END)
    # do something with string value txt1
    print "Value in Text Widget =", txt1  # testing


root = Tk()

text1 = Text(root, height=1, width=25)
text1.pack(side=TOP)
# do some caculation and format result
pi_approx = 355/113.0
str1 = "%.4f" % (pi_approx)    # 3.1416
# show result in text widget
text1.insert(INSERT, str1)

text1.bind('<Button-1>', get_value)   # bind left mouse click

mainloop()

Hi Friends,
I too need a solution for that problem. I took a label and i need to extract the label's content and print that. Can anyone give a small coding example.....

Hi Buddy .... Please do gimme a small example to display the content of a label when a mouse click is done on that. Its a bit urgent. If you dont mind please post this coding example to my mail id: girishmsampath@gmail.com
(or)
sampath@venlabs.net

You can get the current text of a tkinter Label via the __getitem__ method, like so:

labelText = 'all your base are belong to us'
        testLabel = Label(self.frame, text=labelText)
        print 'The text is %s' % testLabel.__getitem__("text")

Hope this helps.

Alex

Hi Buddy .... Please do gimme a small example to display the content of a label when a mouse click is done on that. Its a bit urgent. If you dont mind please post this coding example to my mail id: girishmsampath@gmail.com
(or)
sampath@venlabs.net

You can get the current text of a tkinter Label via the __getitem__ method, like so:

labelText = 'all your base are belong to us'
        testLabel = Label(self.frame, text=labelText)
        print 'The text is %s' % testLabel.__getitem__("text")

Hope this helps.

Alex

Even simpler, use the dictionary approach
label_text = testLabel["text"]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.