TKinter is driving me crazy!

Daniel Yoo dyoo at hkn.eecs.berkeley.edu
Tue Sep 2 14:42:11 EDT 2003


Cousin Stanley <CousinStanley at hotmail.com> wrote:
: | Frames don't have titles, but toplevels do.
: | Try using the 'title()' method of the toplevel widget.

: Daniel ...

:    Thanks for the reply ...

:    I'm familar with applying the  title()  method
:    to a Tkinter root or Toplevel object, but couldn't
:    figure out how to change the title on a sub-classed frame

Hello again!


But that's because frames don't have titles.  *grin*

(If you're coming from a Java background, and are thinking about Java
Swing's JFrames, don't!  Tkinter's Frame is a grouping widget, and is
more like a JPanel than a JFrame.)


If we try making a widget without a toplevel container, Tkinter will
automagically create a default toplevel master object for us, and use
that to subsequently attach widgets.  This default behavior is
convenient, but can cause the confusion that we're having between
frames and toplevels.

In fact, it's perfectly ok to have two frames in a toplevel window:

###
>>> from Tkinter import *
>>> root = Tk()
>>> p1 = Frame(root)
>>> p2 = Frame(root)
>>> Label(p1, text="hello").pack(side=TOP)   
>>> Label(p1, text="world").pack(side=BOTTOM)
>>> Label(p2, text="tkinter").pack(side=TOP)
>>> Label(p2, text="programming").pack(side=BOTTOM)
>>> p1.pack(side=LEFT)
>>> p2.pack(side=RIGHT)
###

So in this context, asking for the title of a Frame is a meaningless
question: you really want to get at the title of the toplevel object.


I guess another way to do the title setting, if all we have is the
frame, is to grab the 'master' from our widget, and hope that it's the
toplevel:

###
from Tkinter import *
button = Button(text="Press me!")
button.pack()
button.master.title("This is a test title")
###


Even so, the examples in "An Introduction to Tkinter" don't depend on
the default toplevel object --- those examples explicitly create a
root toplevel object --- so it's probably a better idea to do the
same.

###
from Tkinter import *
root = Tk()
button = Button(root, text="Press me!")
button.pack()
root.title("This is a test title")
###






More information about the Python-list mailing list