[Tutor] a question based on Sheila's Tkinter class defn questions
Stephen Lastinger
s.lastinger@computernetdesign.com
Sat, 7 Jul 2001 19:39:58 -0400 (EDT)
Just to see if I got this straight....Based on this formula:
instance.method(args...) => becomes => class.method(instance, args...)
Frame.__init__(self, parent=None)
calls the Frame superclass __init__ method by a (and i'm not sure i'm
using the right term here) instance caller like:
im_feeling_dufus_like=Frame(blah)
and blah would be the argument passed to parent?
I've got inheretance[sp.] and specialization down cold, but this
....ugh
and passing blah to parent...would that in turn be passed to the Frame
__init__ method, and if so is it master, cnf, **kw, or none of the
above...
this just seems like a damn ugly way to do things.....
on an opposite note...I ment to say something earlier but I've been trying
to grapple with the above stuff....Danny your Ballon class e-mail on june
28th was beautiful. This is simple and elegant (and fun!).
Loved it.
###
class Balloon:
def __init__(self, shape, color):
self.shape, self.color = shape, color
def __add__(self, other):
return Balloon('%s tied with %s' % (self.shape, other.shape),
'%s-%s' % (self.color, other.color))
myballoon = Balloon('airplane', 'blue')
print myballoon.color, 'is the color of my balloon.'
balloon_bag = [ Balloon('heart', 'red'),
Balloon('robot', 'silver'),
Balloon('blimp', 'black') ]
###
Let's see what happens:
###
>>> b1 = Balloon('tree', 'red')
>>> b2 = Balloon('bear', 'gold')
>>> b1 + b2
<__main__.Balloon instance at 80ccd30>
>>> b3 = b1 + b2
>>> print b3.shape
tree tied with bear
>>> print b3.color
red-gold
On Sat, 7 Jul 2001, Sheila King wrote:
[snip..]
>
> Here is an example from the book:
>
> ---------------page 305 quitter.py------------------------------
> #############################################
> # a quit button that verifies exit requests;
> # to reuse, attach an instance to other guis
> #############################################
>
> from Tkinter import * # get widget classes
> from tkMessageBox import askokcancel # get canned std dialog
>
> class Quitter(Frame): # subclass our GUI
> def __init__(self, parent=None): # constructor method
> Frame.__init__(self, parent)
> self.pack()
> widget = Button(self, text='Quit', command=self.quit)
> widget.pack(expand=YES, fill=BOTH, side=LEFT)
> def quit(self):
> ans = askokcancel('Verify exit', "Really quit?")
> if ans: Frame.quit(self)
>
> if __name__ == '__main__': Quitter().mainloop()
> -----------------------------------------------------------------
> class Frame(Widget):
> """Frame widget which may contain other widgets and can have a 3D
> border."""
> def __init__(self, master=None, cnf={}, **kw):
> """Construct a frame widget with the parent MASTER.
>
> #[much snipped]
> -----------------------------------------------------------------
--
Stephen Lastinger - s.lastinger@computernetdesign.com
Computer Network Design,Inc. - http://www.computernetdesign.com