[Tutor] help

Alan G alan.gauld at freenet.co.uk
Tue Aug 9 10:02:03 CEST 2005


Hi Dan,


I think you need to back up a little and build the program piece by 
piece.

> i am making a simple script to get the hang of Tkinter.

The script is quite complex for a starter. You could focus down on 
just
a single control initially, the area button. Get that to pop up the 
dialog
and read the numbers. Have a button on that to do the calculation and
display the result. Only once you get that working expand the UI to 
have
the other operations. That way you can focus on one problem at a time.

Now in your code:

First you have some very long lines, they wouldn't even fit on my 
screen
at 1200x1000 resolution! Either move the comments before the lines 
they
describe or 'fold' the function parameters

foo(param1,
    param2,
    param3):
  code here

It might seem cosmetic but in a language like Python where indentation
is critical it can make a big difference in spotting simple mistakes.

Second you use pack() in your widget creation code:

w = foo(....).pack()

w will always hold NOne because pack returns None.
You need top do it in two steps:

w = foo(....)
w.pack()

An annoying foible of Tkinter which catches most folks at some point.

Thirdly you need to pass parameters in and return values out of your
functions to ebnable communication between you different windows.
Either that or use global variables but that gets messy very quickly.

You probably need to use some globals because you are not using OOP 
yet
but the way the code is just now the dialog functions cannot see the
toplevel window controls and vice versa.

Finally I'm not sure what cal() is doing:

def cal():
    win4 = Toplevel   <------ Huh???
    print 'area = ', 'w'*'h'

The win4 line assigns a local(to cal() ) variable to the 'TopLevel'
Tkinter function but then does nothing with it.
WE then try to print the result of multiplying two strings together,
which is an invalid operation. I assume you wanted w*h where w and h
are the valuesset in area() but those are hidden inside area().
You need to either pass them to cal() as parameters or make them 
global.

Also printing will try to display the result on a console window,
but by naming the script .pyw there will be no console so you won't
see the output! Again you probably need to use a global Label control
and set the text proprty to the result.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list