[Tutor] Updating Label widgets in Tkinter
Tue, 26 Feb 2002 23:21:07 -0600 (CST)
I'm hoping there's a Tkinter expert or two out there who can offer some
help with the following:
I'm working through a number of the Tkinter tutorials and John Grayson's
book "Python and Tkinter Programming," but I continue to be stymied by
the problem of updating the text of a label widget based on some
calculation that's bound to a button. I'm including some code that draws
a very simple screen that presents an entry widget. The user is supposed
to enter a number, click calculate, and be presented with the square
root of the number they typed. I've tried a bunch of things at this
point, but everything's starting to blur. Can anyone point me in the
Here's the code. Thanks.
from Tkinter import *
from math import sqrt
"""Create an instance of a very simple calculator."""
self.master.title('Square Root Calculator')
fInput = Frame(self)
Label(fInput, text='x = ').pack(side=LEFT)
number = StringVar()
fOutput = Frame(self)
result = Label(fOutput,
text='Waiting for a number...').pack(pady=10)
buttons = Frame(self)
def calc(self, number):
"""Update the label named 'result' to say 'The square root of
<insert original number> is <square root of that number>'
and then clear the contents of the entry box."""
Tim Wilson | Visit Sibley online: | Check out:
Henry Sibley HS | http://www.isd197.org | http://www.zope.com
W. St. Paul, MN | | http://slashdot.org
firstname.lastname@example.org | <dtml-var pithy_quote> | http://linux.com