[Tutor] Tk and Classes

Bryce Embry bryce@bembry.org
Wed, 10 Apr 2002 14:56:49 -0500


Martin,
I thought about taking that same approach (learning classes and Tkinter 
simultaneously),  but I ended up doing learning first.  I would strongly 
recommend choosing one of these to learn first and going from there.

The problem with the class you posted is that every function (aka method) 
in a class must take itself as an argument (have "self" in the 
parenthesis).  So, you should have:

class food:
         def showfood(self, chosen):
                 # Your commands here

Whenever you call a class you create an instance of the class.  To access 
the function (method) inside the class, the class needs to know which 
instance of the class is calling it, so the first argument passed in is 
essentially saying "who am I".  These keeps your different calls to the 
class from getting all crossed up and confused.

Again, I would recommend learning Tkinter and classes separately.  They are 
two different concepts and its hard to wrap your head around them both at 
the same time.   I can understand wnating to learn them simultaneously, 
especially since so many Tkinter tutorials rely very heavily on 
classes.  But Tkinter will work fine without using classes.

If you want to learn Tkinter first, you can check out the notes I have 
online (www.bembry.org/tech/python).  I am currently teaching Tkinter to my 
high school class, and the notes do not assume an understanding of 
classes.  I have the notes from the first two lessons up and the students 
have found it "very easy" (personally, it's been a big challenge to make it 
easy for them).  The next lesson should be up by tomorrow.

For learning classes, I'd suggest http://www.devshed.com/Server_Side/Python 
and check the OOP tutorials.  I found these very useful. I also have some 
notes on my website which you might find useful.

Hope that helps.  Good luck.

Bryce Embry

At 02:32 PM 4/10/2002, you wrote:
>Hi, I'm probably going about this the wrong way, but i'm trying to teach
>myself classes and TK at the same time (They seem very interwoven)
>
>Here the code.
>============
>from Tkinter import *
>import sys
>
>
>class food:
>  def showfood(chosen):
>   newform = Toplevel()
>   Button(newform, text=chosen).pack()
>
>root = Tk()
>FoodOption = StringVar()
>FoodOption.set('Spam')
>OptionMenu(root, FoodOption , 'Spam', 'Egg', 'Chips', 
>command=food.showfood(FoodOption)).pack()
>
>Button(root, text = 'quit', command=sys.exit).pack()
>
>root.mainloop()
>
>============
>
>and this is the error
>
>TypeError: unbound method showfood() must be called with instance as first 
>argument
>
>Can someone explain what i'm doing wrong, or modify the example so it works
>and i can see what changed.
>
>Thanks
>
>Budgester
>
>--
>Budgester Technologies Ltd
>Office : 01992 718568
>Mobile : 07815 982380
>mailto:martin@budgester.com
>http://www.budgester.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