[Tutor] Tk and Classes

Ike Hall hall@nhn.ou.edu
Fri, 12 Apr 2002 02:03:49 -0500


This is not necessarily the only way to go about doing this.  When I first 
began learning python, the whole reason for doing so was to create a gui 
application for use in a Physics experiment by way of Tkinter/Pmw.  Now I am 
a physics grad student, and do not really consider myself a programmer, 
althogh the other members of my collaboration are now calling me the python 
expert....eeek...anyway, before doing so, my only experience with OOP and 
classes and such was merely in passing.  I had done some programming in C and 
fortran before (simply to get thru nasty calculations quickly) and never 
needed objects.  So learning python for me began with the first few chapters 
of the python tutorial simply to see how the data types and flow structures 
worked in python, and then from there I dove straight in to Tkinter by way of 
Fredrick Lundh(sp?)'s Tkinter guide.  In doing so I was able to quickly write 
short scripts to flash pretty displays on the screen, and thus get used to 
using Tkinter when I decided that in order to write the larger application 
that was needed, widget classes would need to be created by me in order to 
make the thing work, so in order to learn how classes work in python (and 
with Tkinter) I did two things, first I looked on the web for any and all 
sample programs I could find  on the web.  This was helpful, but at the same 
time very frustrating, as some of the examples were difficult to follow due 
to sparse commenting and usage of commands and commands that a newbie like me 
at the time (and sometimes I still consider myself a newbie even tho Ive been 
working in python now almost every day for almost a year) was unfamiliar 
with.  so noting that I decided to to spend the $45 bucks or so to buy 
Programming Python by Mark Lutz, which I might add is a fine text.  By 
following many of the examples in that book (and depending on how far you 
have already progressed, not necessarily in order), I think, it is very very 
possible to learn how to manipulate classes and use Tkinter in a fluid 
manner.  I did anyway....well, ok, I still am learning too, but that is 
because I learn based on my needs.  (i.e., I need to do this, how can I do 
it).  Of course as we all know that is not always the best way to learn.  But 
I find that for me, that is the way that sticks.  However, because that is 
the way I choose to learn python, my programs have gone through countless 
revisions as I have learned new tricks and better ways of doing the same 
thing.  But I don't mind.   Anyway, sorry to get long on everyone, but I 
thought it might help to see that there are many ways of teaching yourself 
what you need to know, and this is one of them.  Good luck!!!

On Thursday 11 April 2002 05:08 pm, you wrote:
> > 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor