Calculator example source code from Tkinter import *

import Pmw O Python MegaWidgets c1ass SLabe1(Frame):

SLabe1 defines a 2-sided 1abe1 within a Frame. The

1eft hand 1abe1 has b1ue 1etters; the right has white 1etters.

def _init_(se1f, master, 1eft1, right1):

se1f.pack(side=LEFT, expand=YES, fi11=BOTH) Labe1(se1f, text=1eft1, fg='stee1b1ue1', font=("aria1", 6, "bo1d"), width=5, bg='gray40').pack( side=LEFT, expand=YES, fi11=BOTH) Labe1(se1f, text=right1, fg='white', font=(Maria1", 6, Mbo1d"), width=1, bg='gray40').pack(

Tkinter, including these:

Python Codes Tkinter
Figure 3.2 A better calculator

side=RIGHT, expand=YES, fill=BOTH)

class Key(Button):

def _init_(self, master, font=('ariall, 8, 'bold'), fg='white',width=5, borderwidth=5, **kw): kw['font'] = font kw['fg'] = fg kw['width'] = width kw['borderwidth'] = borderwidth apply(Button._init_, (self, master), kw)

self.pack(side=LEFT, expand=NO, fill=NONE)

class Calculator(Frame):

def _init_(self, parent=None):

self.pack(expand=YES, fill=BOTH) self.master.title('Tkinter Toolkit TT-421) self.master.iconname('Tk-42')

self.calc = Evaluator() # This is our evaluator self.buildCalculator() # Build the widgets

# This is an incomplete dictionary - a good exercise! self.actionDict = {'second1: self.doThis, 'mode1: self.doThis, delete'



self.doThis, self.doThis, self.doThis, self.doThis, self.doThis, self.doThis, self.doThis, self.doThis, self.doThis, self.doThis, alpha': math': program' clear': cos': up' X2' ln' off1: enter'

self.doThis, self.doThis, : self.doThis, self.clearall, self.doThis, self.doThis, self.doThis, self.doThis, self.turnoff self.doEnter, self.current =

def doThis(self,action):

print '"%s" has not been implemented' % action def turnoff(self, *args): self.quit()

def clearall(self, *args): self.current = ""

self.display.component('text').delete(1.0, END) 4

def doEnter(self, *args):

self.display.insert(END, '\n')

result = self.calc.runpython(self.current) 5

if result:

self.display.insert(END, '%s\n' % result, 'ans') 6 self.current = ""

def doKeypress(self, event): key = event.char if key != '\b':

self.current = self.current + key

seif.current = se1f.current[:-1] x |

def keyAction(se1f, key):

se1f.disp1ay.insert(END, key)

seif.current = seif.current + key

def eva1Action(se1f, action):



except KeyError:


Code comments


Pmw (Python MegaWidgets) widgets are used. These widgets will feature prominently in this

book since they provide an excellent mechanism to support a wide range of GUI requirements

and they are readily extended to support additional requirements.


In the constructor for the Key class, we add key-value pairs to the kw (keyword) dictionary

and then apply these values to the Button constructor.

def _init_(se1f, master, font=('aria1', 8, 'bo1d'),

fg='white',width=5, borderwidth=5, **kw):

kw['font'] = font

apply(Button._init_, (self, master), kw)

This allows us a great deal of flexibility in constructing our widgets.


The Calculator class uses a dictionary to provide a dispatcher for methods within the class.

'matrix1: se1f.doThis, 'program1: se1f.doThis,

vars': se1f.doThis, 'dear1: se1f.c1eara11,

'sin': se1f.doThis, 'cos': se1f.doThis,

Remember that dictionaries can handle much more complex references than the rela-

tively simple cases we need for this calculator.


We use a Pmw Scro11edText widget, which is a composite widget. To gain access to the

contained widgets, the component method is used.

se1f.disp1ay.component('text').de1ete(1.0, END)


When the ENTER key is clicked, the collected string is directed to the calculator's evaluator:

resu1t = seif.caic.runpython(seif.current)

The result of this evaluation is displayed in the scrolled text widget.


The final argument in the text insert function is a text tag 'ans' which is used to change the

foreground color of the displayed text.

se1f.disp1ay.insert(END, '%s\n' % resu1t, 'ans')


doKeypress is a callback bound to all keys. The event argument in the callback provides the

client data for the callback. event.char is the key entered; several attributes are available in

the client data, such as x-y coordinates of a button press or the state of a mouse operation (see

"Tkinter events" on page 98). In this case we get the character entered.


A simple exception mechanism to take action on selected keys is used.


1 (continued) ^H

def buildCalculator(self):


= 1

# A Function I


= 0

# A Key


= 'gray30'

# Dark Keys


= 'gray50'

# Light Keys


= 'steelbluel'

# Light Blue Key


= 'steelblue'

# Dark Blue Key


= [


KC3, FUN, 'second'), # Row 1



'', KC1, FUN, 'mode'),



'', KC1, FUN, 'delete'),



'', KC2, FUN, 'alpha'),



'', KC1, FUN, 'stat')],



'A', KC1, FUN, 'math'), # Row 2



'B', KC1, FUN, 'matrix'),



'C', KC1, FUN, 'program'),



'', KC1, FUN, 'vars'),


KC1, FUN, 'clear')],



'D', KC1, FUN, 'X1'), # Row 3



'E', KC1, FUN, 'sin'),



'F', KC1, FUN, 'cos'),



'G', KC1, FUN, 'tan'),

'PI', 'H'

, KC1, FUN, 'up')],


'Root', 'I', KC1, FUN, 'X2'), # Row 4


EE', 'J'

, KC1, KEY, ','),


{', 'K',

KC1, KEY, '('),


}', 'L',

KC1, KEY, ')'),


', 'M',

KC4, KEY, '/')],



'N', KC1, FUN, 'log'), # Row 5


Un-1', 'O', KC2, KEY, '7'),


Vn-1', 'P', KC2, KEY, '8'),


n1, 'Q',

KC2, KEY, '9'),


[', 'R',

KC4, KEY, '*')],


'ex', 'S', KC1, FUN, 'ln'), # Row 6


L4', 'T'

, KC2, KEY, '4'),


L5', 'U'

, KC2, KEY, '5'),


L6', 'V'

, KC2, KEY, '6'),


]', 'W',

KC4, KEY, '-')],



'X', KC1, FUN, 'store'), # Row 7


L1', 'Y'

, KC2, KEY, '1'),


L2', 'Z'

, KC2, KEY, '2'),


L3', '',

KC2, KEY, '3'),

(' + ',

MEM', '"

', KC4, KEY, '+')],


KC1, FUN, 'off'), # Row 8


KC2, KEY, '0'),


KC2, KEY, '.'),



'?', KC2, FUN, 'neg'),



'', KC4, FUN, 'enter')]]


display =

Pmw.ScrolledText(self, hscrollmode='dynamic',

vscrollmode='dynamic', hull_relief='sunken',

hull_background='gray40', hull_borderwidth=10,



text_background='honeydew4', text_width=16,

text_foreground='blackl, text_height=6,

text_padx=10, text_pady=10, text_relief='groove',

text_font=('arial', 12, 'bold'))

self.display.pack(side=TOP, expand=YES, fill=BOTH)

self.display.tag_config('ans1, foreground='white')


self.display.component('text').bind(l<Key>', self.doKeypress)

self.display.component('text').bind('<Return>', self.doEnter)

for row in keys:

rowa = Frame(self, bg='gray40')

rowb = Frame(self, bg='gray40')

for p1, p2, p3, color, ktype, func in row:

if ktype == FUN:

a = lambda s=self, a=func: s.evalAction(a)


a = lambda s=self, k=func: s.keyAction(k)

SLabel(rowa, p2, p3)

Key(rowb, text=p1, bg=color, command=a)

rowa.pack(side=TOP, expand=YES, fill=BOTH)

rowb.pack(side=TOP, expand=YES, fill=BOTH)

class Evaluator:

def _init_(self):

self.myNameSpace = {}

self.runpython("from math import *")

def runpython(self, code):


return "eval(code, self.myNameSpace, self.myNameSpace)"


except SyntaxError:



exec code in self.myNameSpace, self.myNameSpace


return 'Error'


Code comments (continued)


A number of constants are defined. The following data structure is quite complex. Using con

stants makes it easy to change values throughout such a complex structure and they make the

code much more readable and consequently easier to maintain.

FUN = 1 # A Function

KEY = 0 # A Key

KC1 = 'gray30' # Dark Keys

KC2 = 'gray50' # Light Keys

These are used to populate a nested list of lists, which contains tuples. The tuples store

three labels, the key color, the function or key designator and the method to bind to the key's

cmd (activate) callback.

We create the Pmw ScrolledText widget and provide values for many of its attributes.

self.display = Pmw.ScrolledText(self, hscrollmode='dynamic',



vscro11mode='dynamic', hu11_re1ief='sunken', hu11_background='gray40', hu11_borderwidth=10, text_background='honeydew4', text_width=16,

Notice how the attributes for the hu11 (the container for the subordinate widgets within Pmw widgets) and the text widget are accessed by prefixing the widget. ! We define a text tag which is used to differentiate output from input in the calculator's screen. se1f.disp1ay.tag_config('ans1, foreground='white') We saw this tag in use earlier in the text insert method. © Again, we must use a 1ambda expression to bind our callback function.

# Python exceptions are quite flexible and allow simple control of errors. In the calculator's evaluator (runpython), we first run eva1. try:

return %eva1(code, se1f.myNameSpace, se1f.myNameSpace)% This is used mainly to support direct calculator math. eva1 cannot handle code sequences, however, so when we attempt to eval a code sequence, a SyntaxError exception is raised. © We trap the exception:

except SyntaxError: try:

exec code in se1f.myNameSpace, se1f.myNameSpace except:

return 'Error'

and then the code is exec'ed in the except clause. Notice how this is enclosed by another try... except clause.

Figure 3.2 shows the results of clicking keys on the calculator to calculate simple math equations. Unlike many calculators, this displays the input and output in different colors. The display also scrolls to provide a history of calculations, not unlike a printing calculator. If you click on the display screen, you may input data directly. Here is the surprise: you can enter Python and have exec run the code.

Figure 3.3 shows how you can import the sys module and access built-in functions within Python. Technically, you could do almost anything from this window (within the constraint of a very small display window). However, I don't think that this calculator is the much-sought Interactive Development Environment (IDE) for Python! (Readers who subscribe to the Python news group will understand that there has been a constant demand for an IDE for Python. Fortunately, Guido Van Rossum has now released IDLE with Python.)

When you press ENTER after dir(), you will see output similar to figure 3.4. This list of built-in symbols has scrolled the display over several lines (the widget is only 16 characters wide, after all).




', '_name_', '_


, '_stdin_', '_



', 'argv', 'buiitin



names', 'copyri


Figure 3.3 Python input

Figure 3.3 Python input atform', 'prefix', 'setcheck interval', 'setprofiie', 'settr ace', 'stderr', 'stdin', 'stdo ut', 'version', 'winver']

Figure 3.4 Output from dir()

Figure 3.5 Variables and built-in functions

Because we are maintaining a local namespace, it is possible to set up an interactive Python session that can do some useful work. Figure 3.5 shows how we are able to set variables within the namespace and manipulate the data with built-ins.

Figure 3.5 Variables and built-in functions

Figure 3.6 Using the math module

Figure 3.6 is yet another example of our ability to gain access to the interpreter from an interactive shell. While the examples have been restricted to operations that fit within the limited space of the calculator's display, they do illustrate a potential for more serious applications. Note how Python allows you to create and use variables within the current namespace.

« I .j When developing applications, I generally hide a button or bind a "secret" key /VOAfr sequence to invoke a GUI which allows me to execute arbitrary Python so that I can examine the namespace or modify objects within the running system. It is really a miniature debugger that I always have access to during development when something unusual happens. Sometimes restarting the application for a debug session just does not get me to the solution. An example of one of these tools is found in "A Tkinter explorer" on page 334.

+1 -1

Post a comment