First Tkinter application. Comments please?

Matt Gushee mgushee at havenrock.com
Wed Dec 22 20:55:29 EST 1999


Gerrit Holl <gerrit.holl at pobox.com> writes:

> I just created my first Tkinter.Canvas application.

Hey, that's fun! Next you can do a Python version of the classic
Breakout game :-)

> class App:
  ...
>     def __init__(self, root):

  ...

>         self.root = root

My preference would be subclass App from Tk, but I'm not sure it's
necessarily better. There are probably arguments on both sides.

>	  self.root.mainloop()

Usually not a good idea to do this in your __init__ method. See below.

>         # What's better: this, or binding <KeyPress> and ignoring everything
>         # that's not a keycode of one of these?

One or the other, depending on what you're trying to do ;-) You seem
to have mixed the two approaches, so that you're recognizing each
keystroke twice -- once to invoke the bindings, and again within
move_it(). 

Which is better in any given application depends on a number of
factors, such as:

* How many keystrokes you use, and how you use them (e.g. if you were
  making a text editor, you would want your keybindings to be as
  specific as possible, to avoid invoking event handlers when you're
  just typing text -- whereas in, say, a game, you can assume that any 
  keypress is intended to execute some code)
* How much code is shared between the different keystroke actions.

Of course, as you probably know, in a program of this size it really
doesn't make much difference.

>     def move_it(self, *args):

A conventional way of doing this is 

      def move_it(self, event=None):

AFAIK you will only ever get one argument other than self, which is
the event info object. And calling it 'event' makes the code easier to 
understand.

Continuing:

           if event is not None:
               if event.keycode == UP:
                   self.draw.move("it", 0, -1)
               elif event.keycode == DOWN:
                   self.draw.move("it", 0, 1)
               elif event.keycode == RIGHT:
                   self.draw.move("it", 1, 0)
               elif event.keycode == LEFT:
                   self.draw.move("it", -1, 0)

> App(Tk())

Have you noticed how many Python modules (the well-structured ones)
have a section at the end like:

if __name__ == '__main__':

    spam = DeadParrot()
    siss(boom(bah()))

?

My version of your app would have:

if __name__ == 'main':  ## detects that this module is being run as a
                        ## stand-alone script.

    spam = App()  ## App being a subclass of Tk
    spam.mainloop()

This way, you have a portable class definition which can be imported
into another app without trying to mainloop() itself -- because main
windows should normally be created at the application level -- or you
can import it into the Python shell and experiment with the window
interactively -- but when you run this file as a script, it gives you
a nice little demo.

Hope that's not too much criticism. I like your concepts a lot!  

-- 
Matt Gushee
Portland, Maine, USA
mgushee at havenrock.com
http://www.havenrock.com/



More information about the Python-list mailing list