[Tutor] Tkinter

Alan Gauld alan.gauld at freenet.co.uk
Sat Oct 29 13:26:11 CEST 2005


> When I run the following code,
> script kept running and I have to force it to stop.

Yep. Thats usually what happens when you run a GUI program.
What did you expect it to do?

> Could you check the code to give suggestions how to improve it?

I'll add some comments but its not clear what you are trying to do.
Some general points first:

1) You should only have one call to mainloop() in a Tkinter program.
2) Its bad practice to have code after the "if name==main " block.
    That code gets exercised as well as the if block - very confusing!

Specific comments:

class Dialog(Widget):
  def __init__(self, master=None, cnf={}, **kw):
      cnf = _cnfmerge((cnf, kw))
      self.widgetName = '__dialog__'

AG> Looks a bit weird. Names starting with underscores usually mean private
AG> and __xxx___ style names are usually private to Python itself... But 
those are
AG> merely conventions, but useful ones...

      Widget._setup(self, master, cnf)

AG> Are you sure you don't want to call Widget.__init__ first?

      self.num = self.tk.getint(
              apply(self.tk.call,
                    ('tk_dialog', self._w,
                     cnf['title'], cnf['text'],
                     cnf['bitmap'], cnf['default'])
                    + cnf['strings']))

AG> I have no idea whats going on here! Can you explain?

      try: Widget.destroy(self)
      except TclError: pass

AG> And having constructed the Widget subclass you now destroy it?
AG> I'm very confused.

   def destroy(self): pass

AG> except destroy doesn't do anything...

if __name__ == '__main__':
  q = Button(None, {'text': 'How are you',
                    Pack: {}})

AG> This is a very old fashioned way of configuring a Tkinter widget
AG> and suggests you have been looking at a very old Tkinter tutorial.

q = Button(None, text="How are you")
q.pack()

AG> would be more common.

  b1 = Listbox()
  b1.pack()

  c1 = Checkbutton(text="Check")
  c1.pack()

  q.mainloop()

AG> So you set the mainlop running here and wait for events.
AG> But you haven't defined any event handlers anywhere
AG> let alone connected them to your widgets. The only things
AG> that will work are the default window widgets

from Tkinter import *

AG> You've already done this up top

root =Tk()
menu=Menu(root)
root.config(menu=menu)
filemenu=Menu(menu)
menu.add_cascade(label="Test", menu=filemenu)
filemenu.add_command(label="Just Try")
filemenu.add_separator()

AG:> And this all looks a bit confused too, I'm not sure what
AG> you are trying to achieve here?

mainloop()

AG> And you already called mainloop above, although this mainlop isn't
AG> defined anyplace that I can see, its nopt attached to an object of
AG> any kind...

I think you need to clarify in your mind what you are trying to achieve
and restructure the code to reflect that. As it is its not clear what 
exactly
you think the code should do.

Can you simplify it? Just get a basic window/menu combination set up
and working, then once thats fixed fold the config stuff in later?

Also I'd make sure you check out the latest Tkinter tutorial, all that
configuration by dictionary stuff was deprecated about 7 years ago!

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