tkInter Listbox question

eb303 eric.brunel.pragmadev at gmail.com
Mon Jun 21 04:18:12 EDT 2010


On Jun 21, 7:36 am, Anthony Papillion <papill... at gmail.com> wrote:
> So I'm trying to add a Listbox to my window. I want it to be the width
> of my window and the height of my window.  I'm using the following
> code ('root' is my toplevel window):
>
> gsItems = Listbox(root, width=root.winfo_width(),
> height=root.winfo_height())
> gsItems.pack()
>
> While you would think this code would set the height and width of
> gsItems to the height and width of root, it doesn't. Instead it puts a
> little tiny listbox in the middle of the form.
>
> I've been Googling for almost an hour. Can anyone help me figure this
> out? Point me in the right direction?
>
> Thanks!
> Anthony

The problem you have in your script is probably simple: at the time
when you put the listbox in it, the windows still contains nothing, or
very little. And what you're doing is to take its _current_ size and
assign it to the listbox. So the listbox will be very small, since at
the time you ask for its dimensions, the window itself is very small.

Basically, you're taking the wrong approach there: you should put the
listbox in the window telling it to adapt its size to its parent
container, and then resize the window if you want to.

The first step is done via the geometry manager, which can be pack or
grid. Please refer to your documentation to know how to use them to
make a widget adapt its size to its container. The documentation I use
myself is the plain tcl/tk documentation, that you can find at
http://www.tcl.tk/man/tcl8.5/contents.htm. The documentation for pack
is at http://www.tcl.tk/man/tcl8.5/TkCmd/pack.htm and the one for grid
at http://www.tcl.tk/man/tcl8.5/TkCmd/grid.htm. Please note this
documentation might not be suitable for you, as it is not a Python/
Tkinter documentation.

The second step can be done with the method called geometry on your
window. The size you want should then be specified as a string
'<width>x<height>'.

Here is an example, using the pack geometry manager:

from Tkinter import *
root = Tk()
gsItems = Listbox(root)
gsItems.pack(fill=BOTH, expand=TRUE)
root.geometry('400x300')
root.mainloop()

HTH
 - Eric -



More information about the Python-list mailing list