[Tutor] 2 quick questions on python/tkinter

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 21 Sep 2002 19:10:05 -0700 (PDT)


> It runs fine under windows, but under linux it gave the following error:
>
> bash: ./widgets2.py: bad interpreter: No such file or directory
>
> Oddly, it runs fine when i type python widgets2.py.  Whats going on?


It sounds like the magic line:

> #!/usr/bin/python

isn't doing the right thing; let's double check to see where the Python
executable is really located.  What happens when you try:

###
$ which python
###

at your shell prompt?  Also, the following alternative magic line:

###
#!/usr/bin/env python
###

might work better for you; this will search your path for the first
available Python interpreter it can find, and the run with that, so you
won't have to hardcode the path that Python lives in.



> My second question - you see the self.widget.grid()  line repeated
> several times in the Application class.  Is there some way i can apply
> the grid()  function once to to all of the widgets?

Yes, it does look a bit repetitive.  Hmmmm.. perhaps something like:

###
for widget in [self, self.label, self.chkbox, self.entry,
               self.message, self.quitbutton]:
    widget.grid()
###

might work?  What this loop does is allow us to apply the grid() method to
each widget that's in that list above.


It does still feel a little weird having to write out the widgets
manually.  Since an object's attributes live in a special attribute
dictionary named '__dict__', we can restate the above and take advantage
of __dict__:

###
for widget in self.__dict__.values():
    widget.grid()
####

This assumes, though, that all the object's attributes will be widgets.
But something like this should be better than having to manually type out
'foo.grid()' all the time... *grin*


Good luck!