[Tutor] Tkinter
Marilyn Davis
marilyn at deliberate.com
Sat Mar 6 20:25:37 EST 2004
On Sun, 7 Mar 2004, Michael Lange wrote:
> On Sat, 6 Mar 2004 11:12:38 -0800 (PST)
> Marilyn Davis <marilyn at deliberate.com> wrote:
>
> > Hi again,
> >
> > I'm a bit confused about the top-level processing in Tkinter.
> >
> > 2 of my books start with tk=Tk() and build inside tk.
> >
> > 1 book starts with o = MyClass() where MyClass(Frame) and
> > builds inside itself. But when I self.destoy() a frame gets
> > left behind.
> >
> > Then I read that there's a toplevel widget. What's Tk()?
> >
> > I know there's some understanding that I'm missing.
> >
> > Can anyone help me?
> >
> > Marilyn Davis
> >
> Hi, Marilyn,
>
> Tk() is the application's main (or root) window; any Tkinter app
> needs a Tk() window as parent for all the other widgets.
>
> That's why you have to call the mainloop() for your Tk() window to
> keep the whole thing alive.
>
> The toplevel widget (called with the Toplevel() command) is a window
> that looks just like the Tk() window, but is in fact a child of the
> mainwindow. You don't need to call a mainloop() on Toplevel() windows.
And you don't call mainloop on any "slaves"?
Is 'slaves' the right word? I see 'master' and I see 'children'.
Maybe 'slaves' is politically incorrect -- but technically correct? I
don't like 'children' because it can be confused with inheritance
hierarchies.
>
> When you close your mainwindow the children Toplevel windows are
> closed as well, but not vice versa.
But it's slaves are. Right. (I've been studying all day)
>
> Toplevel windows are used for things like dialog boxes or in complex
> guis where you need more than one window.
So Toplevel windows are good for having several windows up and active
simultaneously. The user moves the mouse to get from one toplevel
window to the other.
>
> About the example with MyClass(Frame) I can just guess; maybe the
> code looked like this:
>
> root = Tk()
> o = MyClass(root)#Frame that contains some other widgets
> o.pack()
Nope, surprisingly, the examples from the Deitel book look like:
# Fig. 11.5: fig11_05.py
# Canvas paint program.
from Tkinter import *
class PaintBox( Frame ):
"""Demonstrate drawing on a Canvas"""
def __init__( self, title='A simple paint program'):
"""Create Canvas and bind paint method to mouse dragging"""
Frame.__init__( self )
self.pack( expand = YES, fill = BOTH )
self.master.title( title)
self.master.geometry( "300x150" )
self.message = Label( self, text = "Drag the mouse to draw" )
self.message.pack( side = BOTTOM )
# create Canvas component
self.myCanvas = Canvas( self )
self.myCanvas.pack( expand = YES, fill = BOTH )
# bind mouse dragging event to Canvas
self.myCanvas.bind( "<B1-Motion>", self.paint )
def paint( self, event ):
"""Create an oval of radius 4 around the mouse position"""
x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
self.myCanvas.create_oval( x1, y1, x2, y2, fill = "black" )
def main():
PaintBox().mainloop()
if __name__ == "__main__":
main()
--------
> Now when you destroy the MyClass instance an empty window should be
> left (the Tk() was not destroyed) which might look like a window
> with a frame inside (an empty gray rectangle).
Yes. This happened. But when I called myclass.master.destroy(), it
all went away. So I'm thinking that if you instantiate Frame without
a master, it automatically makes a Tk object for you to be your
master? Or a Toplevel object for you? Probably Tk?
(It's cool, the slave generates the master. Maybe that's politically
correct.)
I don't think I ever got it to call my __del__ method though. I wonder
what is the relationship between destroy() and __del__(). Maybe I have
to look at the source code some time.
>
> I hope this helps
Yes yes. Thank you so much. Anything more you can tell me?
Marilyn
>
> Michael
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
More information about the Tutor
mailing list