Mailing List Archive

Tkinter: why does an empty frame not shrink?
If you have several child widgets packed in a frame and you unpack the
children one by one, the frame will gradually get smaller, except when
the last child is unpacked, in which case the frame does not resize.
Does anyone know why the frame does not shrink to 0x0 when you unpack
the last child? Any soluton?

Example script:
============================================================
import Tkinter

root = Tkinter.Tk()
root.geometry('400x400')

frame = Tkinter.Frame(background = 'red')
frame.pack(fill = 'x')

for i in range(5):
button = Tkinter.Button(frame, text = 'Unpack me')
button.configure(command = button.pack_forget)
button.pack()

root.mainloop()
============================================================

--
Greg McFarlane: INMS Telstra Australia (gregm@iname.com)
Today's forecast: Sunny, with occasional cloudy periods and a chance
of precipitation in some areas.
Tkinter: why does an empty frame not shrink? [ In reply to ]
>>>>> "GM" == Greg McFarlane <gregm@iname.com> writes:

GM> If you have several child widgets packed in a frame and you
GM> unpack the children one by one, the frame will gradually get
GM> smaller, except when the last child is unpacked, in which case
GM> the frame does not resize. Does anyone know why the frame does
GM> not shrink to 0x0 when you unpack the last child? Any soluton?

In one of my applications, I put the frame inside another frame, and
resize the empty container frame to 1x1 manually to solve this. Ugly,
but it works:

def makeWidgets(self,fn=None):
self.rightbarvar=variables.booleanvariable() # used in a menu entry
self.rightbarvar.set(1)
....
self.rightbarc=Tkinter.Frame(self)
self.rightbarc.pack(expand=Tkinter.NO,fill=Tkinter.Y,side=Tkinter.RIGHT)
self.rightbar=Tkinter.Frame(self.rightbarc)
self.rightbar.pack(expand=Tkinter.NO,fill=Tkinter.Y,side=Tkinter.RIGHT)
....

def togglerightbar(self):
if self.rightbarvar.get():
self.rightbar.pack(expand=Tkinter.NO,fill=Tkinter.Y,side=Tkinter.RIGHT)
else:
self.rightbar.pack_forget()
self.rightbarc.configure(width=1,height=1)

Regards,

Rob Hooft
--
===== R.Hooft@EuroMail.net http://www.xs4all.nl/~hooft/rob/ =====
===== R&D, Nonius BV, Delft http://www.nonius.nl/ =====
===== PGPid 0xFA19277D ========================== Use Linux! =========
Tkinter: why does an empty frame not shrink? [ In reply to ]
Greg McFarlane wrote:
> If you have several child widgets packed in a frame and you unpack the
> children one by one, the frame will gradually get smaller, except when
> the last child is unpacked, in which case the frame does not resize.
> Does anyone know why the frame does not shrink to 0x0 when you unpack
> the last child?

when geometry propagation is on (as it is by default), the
packer sets the size of the geometry master to exactly fit
the child widgets. if there are no children, the packer
simply keeps the current size.

> Any solution?

why not just insert an extra widget that you never delete?

from Tkinter import *

root = Tk()
root.geometry("400x400")

frame = Frame(background="red")
frame.pack(fill=X)

Frame(frame).pack()

for i in range(5):
button = Button(frame, text="Unpack me")
button.configure(command=button.pack_forget)
button.pack()

mainloop()

</F>
Tkinter: why does an empty frame not shrink? [ In reply to ]
From: "Fredrik Lundh" <fredrik@pythonware.com>

Greg McFarlane wrote:
> If you have several child widgets packed in a frame and you unpack the
> children one by one, the frame will gradually get smaller, except when
> the last child is unpacked, in which case the frame does not resize.
> Does anyone know why the frame does not shrink to 0x0 when you unpack
> the last child?

when geometry propagation is on (as it is by default), the
packer sets the size of the geometry master to exactly fit
the child widgets. if there are no children, the packer
simply keeps the current size.

> Any solution?

why not just insert an extra widget that you never delete?

from Tkinter import *

root = Tk()
root.geometry("400x400")

frame = Frame(background="red")
frame.pack(fill=X)

Frame(frame).pack()

for i in range(5):
button = Button(frame, text="Unpack me")
button.configure(command=button.pack_forget)
button.pack()

mainloop()

</F>
Tkinter: why does an empty frame not shrink? [ In reply to ]
From: R.Hooft@EuroMail.com (Rob Hooft)

>>>>> "GM" == Greg McFarlane <gregm@iname.com> writes:

GM> If you have several child widgets packed in a frame and you
GM> unpack the children one by one, the frame will gradually get
GM> smaller, except when the last child is unpacked, in which case
GM> the frame does not resize. Does anyone know why the frame does
GM> not shrink to 0x0 when you unpack the last child? Any soluton?

In one of my applications, I put the frame inside another frame, and
resize the empty container frame to 1x1 manually to solve this. Ugly,
but it works:

def makeWidgets(self,fn=None):
self.rightbarvar=variables.booleanvariable() # used in a menu entry
self.rightbarvar.set(1)
....
self.rightbarc=Tkinter.Frame(self)

self.rightbarc.pack(expand=Tkinter.NO,fill=Tkinter.Y,side=Tkinter.RIGHT)
self.rightbar=Tkinter.Frame(self.rightbarc)
self.rightbar.pack(expand=Tkinter.NO,fill=Tkinter.Y,side=Tkinter.RIGHT)
....

def togglerightbar(self):
if self.rightbarvar.get():

self.rightbar.pack(expand=Tkinter.NO,fill=Tkinter.Y,side=Tkinter.RIGHT)
else:
self.rightbar.pack_forget()
self.rightbarc.configure(width=1,height=1)

Regards,

Rob Hooft
--
===== R.Hooft@EuroMail.net http://www.xs4all.nl/~hooft/rob/ =====
===== R&D, Nonius BV, Delft http://www.nonius.nl/ =====
===== PGPid 0xFA19277D ========================== Use Linux! =========