[Tkinter-discuss] Confused by frames

Alexander Belchenko bialix at ukr.net
Sun Jan 14 01:49:16 CET 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chris Niekel пишет:
> Hi,
> 
> I'm trying to combine a couple of widgets into a frame as a reusable
> component. Somehow though, things change when I put some part in a class,
> derived from Frame.
> 
> Version 1:
> 
> class F1(Frame):
>     def __init__(self, *args, **kw):
>         Frame.__init__(self, *args, **kw)
>         Label(text="F1").grid(row=5,column=5)

Last line contains error: you need to pass self as master for Label:

         Label(self, text="F1").grid(row=5,column=5)


> r = Tk()
> 
> b = Button(r, text='button').grid(row=0,column=1)
> f = F1(r).grid(row=0,column=0)
> r.mainloop()
> 
> Version 2:
> 
> from Tkinter import *
> r = Tk()
> 
> b = Button(r, text='button').grid(row=0,column=1)
> f1 = Frame(r)
> f1.grid(row=0,column=0)
> Label(f1, text='F1').grid(row=5,column=5)
> r.mainloop()
> 
> 
> I would think these are equivalent, but they're not. Version 2 has the
> behaviour I expect (a new frame, so the label is on the left).
> 
> Did I do something wrong in version 1, or is there another way to make a
> class with the label which behaves like a frame?
> 
> Regards,
> Chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFqX4MzYr338mxwCURAsJEAJ49MBKjVsSY5evFYivWAXih50rX2wCfZBsu
HBf9aPjq2tb8q6H2ln+6KUs=
=c1SZ
-----END PGP SIGNATURE-----



More information about the Tkinter-discuss mailing list