tkinter question

G

goldtech

This works OK. But I notice that if I enlarge the window after the
script has run, the white listbox only gets "so" big while the grey
background enlarges.

Is there a way to have it all white when I enlarge a window - like
what normally happens?



from Tkinter import *
root = Tk()
root.title("Dirty Words")
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(root, bg='white', font = "Courier 16", width=60)
listbox.pack()
i='123456789abcdefghijklmnopqrstuvwxyz'
for x in range(10):
listbox.insert(END, i)
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
mainloop()
 
E

Eric Brunel

This works OK. But I notice that if I enlarge the window after the
script has run, the white listbox only gets "so" big while the grey
background enlarges.

Is there a way to have it all white when I enlarge a window - like
what normally happens?



from Tkinter import *
root = Tk()
root.title("Dirty Words")
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(root, bg='white', font = "Courier 16", width=60)
listbox.pack()

=> listbox.pack(fill=BOTH, expand=1)
i='123456789abcdefghijklmnopqrstuvwxyz'
for x in range(10):
listbox.insert(END, i)
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
mainloop()

BTW, even for something that simple, using the grid geometry manager may
be easier... It is at least easier to explain: don't ask me what the
expand=1 option means, I never understood it... I just add or remove it
when the pack doesn't do what I want. The sticky option with grid is
easier to handle.

HTH
 
M

Michal Bozon

This works OK. But I notice that if I enlarge the window after the
script has run, the white listbox only gets "so" big while the grey
background enlarges.

Is there a way to have it all white when I enlarge a window - like
what normally happens?



from Tkinter import *
root = Tk()
root.title("Dirty Words")
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(root, bg='white', font = "Courier 16", width=60)
listbox.pack()
i='123456789abcdefghijklmnopqrstuvwxyz'
for x in range(10):
listbox.insert(END, i)
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
mainloop()

try to change listbox.pack() to listbox.pack(expand=True, fill=BOTH)
... is it that you want ?

-mykhal
 
K

Kevin Walzer

Eric said:
BTW, even for something that simple, using the grid geometry manager may
be easier... It is at least easier to explain: don't ask me what the
expand=1 option means, I never understood it... I just add or remove it
when the pack doesn't do what I want. The sticky option with grid is
easier to handle.

"expand = 1" == "expand=TRUE"--that means the widget resizes itself
when the window is re-sized.
 
G

goldtech

try to change listbox.pack() to listbox.pack(expand=True, fill=BOTH)
.. is it that you want ?
Yes.


-mykhal

Worked with TRUE all uppercase.

Exactly what I needed. Thank you.
 
E

Eric Brunel

"expand = 1" == "expand=TRUE"--that means the widget resizes itself
when the window is re-sized.

That's the theory... But what does fill=BOTH means then? And why does
expand=1 (or TRUE, or True) is only needed in some cases and not others?
And I'm quite sure that sometimes, I use expand=1 and it does exactly what
I don't want, and removing it gives the behaviour I want. I've been doing
tk/Tkinter stuff for quite a few years now, and I still haven't understood
exactly when I should use this option... Quite counter-intuitive, IMHO.

But then, since I almost never use pack for this kind of layout, I guess
it's not really important...
 
K

Kevin Walzer

Eric said:
That's the theory... But what does fill=BOTH means then? And why does
expand=1 (or TRUE, or True) is only needed in some cases and not others?
And I'm quite sure that sometimes, I use expand=1 and it does exactly
what I don't want, and removing it gives the behaviour I want. I've been
doing tk/Tkinter stuff for quite a few years now, and I still haven't
understood exactly when I should use this option... Quite
counter-intuitive, IMHO.

But then, since I almost never use pack for this kind of layout, I guess
it's not really important...

"Fill" refers to the dimension/space that is filled when you expand the
widget (assuming "expand" is set to true). "fill=x" means the widget
will expand horizontally (along the x axis). "fill=y" means that the
widget will expand vertically (along the y axis). "fill=both" means the
widget will expand in both direction.

Frederick Lundh has a good introduction to the packer here:
http://effbot.org/tkinterbook/pack.htm

I find "pack" to be more flexible than "grid," so I prefer it for
complex layouts. "grid" is better for simple layouts.

HTH,
Kevin
 
H

Hendrik van Rooyen

Kevin Walzer said:
I find "pack" to be more flexible than "grid," so I prefer it for
complex layouts. "grid" is better for simple layouts.

*does a double take* are you serious? - my experience is that
pack is only good for simple single row or single column stuff.

- Hendrik
 
K

Kevin Walzer

Hendrik said:
*does a double take* are you serious? - my experience is that
pack is only good for simple single row or single column stuff.

- Hendrik
Well, I guess it depends on your viewpoint. I find "pack" flexible
because it allows me to think in terms of top, bottom, right, and left
in terms of arranging UI elements--it's an elegant way to do it in my
view. I tend to use "grid" if I have, say, a window with several
preference items: a label, an entry field, and a button, all arranged in
rows. "grid" is better for stuff like that, I agree.

See http://www.codebykevin.com/blosxom/business/phynchronicity-new.png:
this is an application I develop. The layout is all handled by "pack"
and paned windows. Where you you use "grid" in a layout like this?
 
K

Kevin Walzer

Hendrik said:
*does a double take* are you serious? - my experience is that
pack is only good for simple single row or single column stuff.

- Hendrik
Well, I guess it depends on your viewpoint. I find "pack" flexible
because it allows me to think in terms of top, bottom, right, and left
in terms of arranging UI elements--it's an elegant way to do it in my
view. I tend to use "grid" if I have, say, a window with several
preference items: a label, an entry field, and a button, all arranged in
rows. "grid" is better for stuff like that, I agree.

See http://www.codebykevin.com/blosxom/business/phynchronicity-new.png:
this is an application I develop. The layout is all handled by "pack"
and paned windows. Where you you use "grid" in a layout like this?
 
K

Kevin Walzer

Kevin said:
Well, I guess it depends on your viewpoint. I find "pack" flexible
because it allows me to think in terms of top, bottom, right, and left
in terms of arranging UI elements--it's an elegant way to do it in my
view. I tend to use "grid" if I have, say, a window with several
preference items: a label, an entry field, and a button, all arranged in
rows. "grid" is better for stuff like that, I agree.

See http://www.codebykevin.com/blosxom/business/phynchronicity-new.png:
this is an application I develop. The layout is all handled by "pack"
and paned windows. Where you you use "grid" in a layout like this?

That should be:

http://www.codebykevin.com/blosxom/business/phynchronicity-new.png

Sorry.
 
E

Eric Brunel

I agree with Hendrik, here...
Well, I guess it depends on your viewpoint. I find "pack" flexible
because it allows me to think in terms of top, bottom, right, and left
in terms of arranging UI elements--it's an elegant way to do it in my
view. I tend to use "grid" if I have, say, a window with several
preference items: a label, an entry field, and a button, all arranged in
rows. "grid" is better for stuff like that, I agree.

See http://www.codebykevin.com/blosxom/business/phynchronicity-new.png:
this is an application I develop. The layout is all handled by "pack"
and paned windows. Where you you use "grid" in a layout like this?

I personally use grid almost everywhere, except for basic rows or columns
of widgets with no resizing policy. I find pack confusing, for the very
same reason you find it flexible apparently; for me, this
top/bottom/left/right stuff is not clear at all, and even after all the
explanations I read (including yours), I still don't get the resize policy
with pack. The single 'sticky' option with the row & column weights with
grid is just clearer for me. And there are layouts that you just can't do
with pack (canvas or text with vertical and horizontal scrollbars, for
example...).

But apparently, even this is - again - a matter of taste. So I guess I'll
have to stop telling everyone that grid is better... ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top