[Tutor] Tk and Classes

alan.gauld@bt.com alan.gauld@bt.com
Thu, 11 Apr 2002 23:08:49 +0100


> myself classes and TK at the same time (They seem very interwoven)

Bad idea IMHO. Teach yourself classes frst then do Tkinter.
Tkinter uses classes but classes don't use Tkinter...

> class food:
>  def showfood(chosen):

This needs a self as first parameter

> FoodOption = StringVar()
> FoodOption.set('Spam')
> OptionMenu(root, FoodOption , 'Spam', 'Egg', 'Chips', 
> command=food.showfood(FoodOption)).pack()

You command tries to call the showfood method but 
should instead pass a reference to it. You need to try 
something like:

...command = lambda s=FoodOption: f.showfood(s)).pack()

Note that I use f which implies that somewere you have 
instantiated the food class to:

f = food()

You are missing several bits of the class/object jigsaw, 
thats why I'd recommend stepping back a bit to just 
learn OOP then move onto Tkinter.

Consider reading my OOP, event driven and GUI tutor pages.

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld