[Tutor] working with Tkinter

Bryce Embry bryce@bembry.org
Thu, 25 Apr 2002 09:42:30 -0500


If the buttons are all doing something similar, it might be easier to 
create a generic function / method and pass it whatever button-dependent 
information you need by using a lambda function.

Here's an example that I developed recently as a teaching project:

from Tkinter import *
root = Tk()

box = Entry(root, width=30)
box.grid(row=0, column=0, columnspan=5)

def boxupdate(critter):
      length=len(box.get())
      box.delete(0, length)
      box.insert(0, critter)

dict = {}
col = 0
words = ["Tiger", "Parrot", "Elephant", "Mouse", "Python"]
for animal in words:
       action = lambda x =animal: boxupdate(x)
      dict[animal] = Button(root, text=animal, command=action)
      dict[animal].grid(row=1, column = col)
      col += 1


This method allows the buttons to be dynamically created and mapped and 
might make your code a little less bulky.  Each button passes to the 
function its unique word, then the function does whatever it is supposed to 
do using that word.  This example does not call a second dialogue box, but 
the boxupdate(x) function could be made to do that, or could even be in a 
separate class definition.

  If you haven't seen lambda much before, this explanation may not make any 
sense.  I just finished teaching a unit on dynamically designing buttons in 
Tkinter using dictionaries and lambda.  I have the notes posted at 
http://www.bembry.org/tech/python/tknotes4.shtml.  There are a number of 
examples on that site and perhaps some more useful explanations.

Well, this won't fix everything, but it's one direction that might help.

Bryce





At 09:16 AM 4/25/2002, you wrote:
>I don't have the code in the computer I'm using right now but the code
>is in the following style:
>
>class prg:
>
>    button1= Button(text="whatever", command= self.do_whatever)
>more buttons and widgets
>
>    def whatever(self):
>       and here other dialog with its buttons calling more dialogs.
>
>At the end, the different dialogs call MySQL to put or fetch data (BTW,
>MySQL and Python is just marvellous).
>
>
>
>
>
>
>--- Bryce Embry <bryce@bembry.org> wrote:
> > Can you post some of the code you are working with?  It will be
> > easier to
> > help if we can see what you are trying to do.
> >
> > Thanks,
> > Bryce
> >
> > At 08:11 AM 4/25/2002, you wrote:
> > >I'm working out a small program with Tkinter that has an initial
> > dialog
> > >with many buttons calling many dialogs with many buttons calling
> > more
> > >dialogs.
> > >
> > >My problem is about the code, it looks like spaghetti. I would like
> > to
> > >hear some advice re how to organize code preventing spaghetti-like
> > >programming.
> > >
> > >Thanx,
> > >
> > >Max
> > >
> > >__________________________________________________
> > >Do You Yahoo!?
> > >Yahoo! Games - play chess, backgammon, pool and more
> > >http://games.yahoo.com/
> > >
> > >
> > >_______________________________________________
> > >Tutor maillist  -  Tutor@python.org
> > >http://mail.python.org/mailman/listinfo/tutor
> >
> >
>--------------------------------------------------------------------------------------------
> > "Lord, you establish peace for us.
> > All that we have accomplished
> > you have done for us" -- Isaiah 26:12
> >
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
>
>__________________________________________________
>Do You Yahoo!?
>Yahoo! Games - play chess, backgammon, pool and more
>http://games.yahoo.com/


Bryce Embry
Geek-Of-All-Trades / Master-Of-None

www.bembry.org
--------------------------------------------------------------------------------------------------------------------------------------------------------
Technology Coordinator for MHA/FYOS ^ 390 South White Station ^ Memphis, TN 
38117 ^ (901)682-2409
--------------------------------------------------------------------------------------------------------------------------------------------------------