[Tutor] Tkinter query?

jfouhy@paradise.net.nz jfouhy at paradise.net.nz
Thu Jul 14 01:19:12 CEST 2005


Quoting Dave S <pythontut at pusspaws.net>:

> >But I do not understand where Frame fits in to this ... ie
> >
> >from Tkinter import *
> >class Hello(Frame):
> > 
> > def __init__(self, parent=None):
> > Frame.__init__(self, parent)
> > self.pack()
> >
> >
> >Why not just use 'class Hello(root)' ? Or have I missed the point :-[
> >
> OK am I right in thinging a 'Frame' always has a parent of a Tk() or
> possibly Toplevel() and its used to help in positioning a widget window
> with .pack() ?
> 
> Dave

There's two separate issues here ...

class Hello(Frame):
 ...

This means that Hello is a subclass of the Frame class.  If you haven't already,
you might want to read the Python tutorial; particularly the section on classes
and inheritance: http://docs.python.org/tut/node11.html
(this is an example of inheritance)

Your second question:

> OK am I right in thinging a 'Frame' always has a parent of a Tk() or
> possibly Toplevel() and its used to help in positioning a widget window
> with .pack() ?

Frames are used to help with positioning other widgets, yes.  They are also used
to affect how the application looks: you can change the background colour of
Frames, and also the border (to make them look like they are sticking out, for
example).  But you can (and frequently will) put Frames inside other Frames.

Example:

from Tkinter import *

tk = Tk()
tk.config(background='pink')
tk.geometry('400x400')

frame1 = Frame(tk, background='blue')
frame1.pack(side=LEFT, padx=15, pady=15, expand=True, fill=BOTH)

frame2 = Frame(tk, background='green', borderwidth=3, relief=RAISED)
frame2.pack(side=LEFT, padx=15, pady=15, expand=True, fill=BOTH)

frame3 = Frame(frame1, background='red', borderwidth=3, relief=SUNKEN)
frame3.pack(side=TOP, padx=15, pady=15, expand=True, fill=BOTH)

frame4 = Frame(frame1, background='white', borderwidth=3, relief=GROOVE)
frame4.pack(side=TOP, padx=15, pady=15, expand=True, fill=BOTH)

label1 = Label(frame4, text='Hello world!')
label1.pack(expand=True)

tk.mainloop()

-- 
John.


More information about the Tutor mailing list