Difference between .pack and .configure for widgets in TkInter?
Tkinter objects attribute are not handled through python attribute mechanism (ie you can not do ). Instead, tkinter provide two ways to alter this attribute:self.button1.text = "hello"
- use the object as a dictionnary:
self.button1["text"] = "hello"
- use the
config
method with named argument:self.button1.config(text="hello")
Both are equivalent. Note that you could also have passd such initialisation value through constructor named argument to perform both instanciation an initialisation in one step: self.button1 = Button(self.myContainer1, text="hello")
pack
serve a totally different purpose. It is a geometry management instruction. Used without argument button1.pack()
ask to place button1
in its parent widget below the precedent sibling (if any). You can use options to specify relative position, or resize behavior.
There are other geometry manager for tkinter: grid and place, see this response for a comparison.
Each widget has a dictionary of attributes (text
, background
, ...). You can access it using the regular dictionary syntax, as in self.button1["text"] = "Hello, World!"
or using the configure
method that you see in the other examples. That's just to set up the looks and behaviour of the widget.
Once you're done, you call pack
to let Tkinter now that the widget is ready to be used. Then it will be displayed, etc.
You can see this by executing Tkinter commands step by step in the interpreter, like this:
>>> from Tkinter import *>>> root = Tk()>>> bt = Button(root)>>> bt['text'] = 'hello'>>> bt.pack()