Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can a toplevel auto size to contain packed entry widgets

39 views
Skip to first unread message

two...@gmail.com

unread,
Jun 13, 2018, 2:44:06 PM6/13/18
to
I'm using windows and am creating a variable number of entry widgets that I pack thus, with n incrementing as I loop:

entry .te$n -textvariable myvar$n -font {courier 12}
pack .te$n -fill both -expand true -padx 10 -pady 5

Depending on how many entry's and the font size, these may or may not fit within the toplevel window, and I have to manually resize.

Is there a way to have the toplevel (. in this case) resize to fit. I've looked at the wm command but the various sizing commands don't seem to do what I want. I don't want to fix the size with "wm geom...".

Brad Lanam

unread,
Jun 13, 2018, 3:38:36 PM6/13/18
to
Please show the command(s) you are using to create and cofigure the
toplevel window.

In general everything auto-sizes, so we need to see everything you are
doing.

two...@gmail.com

unread,
Jun 13, 2018, 3:44:05 PM6/13/18
to
Not creating a new toplevel, just using the default . that it starts up with

Brad Lanam

unread,
Jun 13, 2018, 3:51:27 PM6/13/18
to
Works for me.

package require Tk

for {set n 0} {$n < 5} {incr n} {
set myvar$n hello\ world
entry .te$n -textvariable myvar$n -font {courier 12}
pack .te$n -fill both -expand true -padx 10 -pady 5
}

You need to supply a minimal verifiable example.

two...@gmail.com

unread,
Jun 13, 2018, 4:02:41 PM6/13/18
to
I took yours but just made the entry text bigger, and it does not autosize to fit. Maybe it's something with the text entry???

package require Tk

for {set n 0} {$n < 5} {incr n} {
set myvar$n "hello\ world 1111111111111 22222222222222 333333333333"

Brad Lanam

unread,
Jun 13, 2018, 4:10:30 PM6/13/18
to
If you want the entry widget to be able to display the
entire string, the entry widget's size must be set.

http://www.tcl-lang.org/man/tcl/TkCmd/ttk_entry.htm

entry .te$n -textvariable myvar$n -font {courier 12} -width 60

two...@gmail.com

unread,
Jun 13, 2018, 4:11:37 PM6/13/18
to
To be more precise, it doesn't autofit horizontally, but does seem to expand vertically.

two...@gmail.com

unread,
Jun 13, 2018, 4:14:48 PM6/13/18
to
If I add the -width, then it will fit, but does not autosize, so I would have to set up a maximum value, which is what I want to avoid, because if the text is shorter, then the window is still huge.

Brad Lanam

unread,
Jun 13, 2018, 4:17:57 PM6/13/18
to
On Wednesday, June 13, 2018 at 1:14:48 PM UTC-7, two...@gmail.com wrote:
> If I add the -width, then it will fit, but does not autosize, so I would have to set up a maximum value, which is what I want to avoid, because if the text is shorter, then the window is still huge.

An entry widget would never default to autosizing like that because the width
of the entry widget would bounce around between different sizes and it would
look awful.

You can trace the variable and when it changes adjust the width of the
entry widget. I would recommend only increasing the width of the entry widget
and never decreasing it.

two...@gmail.com

unread,
Jun 13, 2018, 4:31:23 PM6/13/18
to
On Wednesday, June 13, 2018 at 1:17:57 PM UTC-7, Brad Lanam wrote:
> An entry widget would never default to autosizing like that because the width
> of the entry widget would bounce around between different sizes and it would
> look awful.
>
> You can trace the variable and when it changes adjust the width of the
> entry widget. I would recommend only increasing the width of the entry widget
> and never decreasing it.

Thanks, I was only trying to create a small demo to show someone how easy it was to do something like this in tcl/tk from a small querry to sqlite.

I didn't even consider a case where the entrys were of different sizes. Maybe a second loop after computing the maximum text length of all the entrys, and then setting all of them to that width. No biggy, just tinkering.

Robert Heller

unread,
Jun 13, 2018, 4:51:45 PM6/13/18
to
It might also depend on the window manager and the O/S being used.

>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

Robert Heller

unread,
Jun 13, 2018, 4:59:29 PM6/13/18
to
One can also stick a scrollbar (horizontal) on an entry widget. Probably
overkill, but doable if one must.

two...@gmail.com

unread,
Jun 13, 2018, 5:15:13 PM6/13/18
to
On Wednesday, June 13, 2018 at 1:59:29 PM UTC-7, Robert Heller wrote:
> One can also stick a scrollbar (horizontal) on an entry widget. Probably
> overkill, but doable if one must.

It turns out that the window will autosize to the largest -width of all the entrys. So, I only needed to track the max width and set any one of them to this max size. Then the other entrys with a smaller value will still expand, probably due to the -fill or -expand settings in the pack.

Thanks, I'm learning some cool stuff here.

Gerald Lester

unread,
Jun 13, 2018, 5:47:35 PM6/13/18
to
I would strongly suggest doing:
entry .te$n -textvariable myvar($n) -font {courier 12}
instead of:
entry .te$n -textvariable myvar$n -font {courier 12}

--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Schelte Bron

unread,
Jun 14, 2018, 11:31:02 AM6/14/18
to
two...@gmail.com wrote:
> I took yours but just made the entry text bigger, and it does not
> autosize to fit. Maybe it's something with the text entry???
>

You can achieve the requested result by creating a label for each
entry and configure it with the same font and text variable. Pack
the labels, so they will influence the size of the toplevel. Then
place the entry on top of the label. Add some padding to the labels
to account for the border and highlight ring of the entry. Like
this:

for {set n 0} {$n < 5} {incr n} {
set myvar$n \
"hello world 1111111111111 22222222222222 333333333333"
label .tl$n -textvariable myvar$n -font {courier 12} \
-padx 3 -pady 3
entry .te$n -textvariable myvar$n -font {courier 12}
pack .tl$n -fill both -expand true -padx 10 -pady 5
place .te$n -in .tl$n -relwidth 1 -relheight 1
}

Note: This will fail miserably if you put newlines or tabs into the
variables.


Schelte.

0 new messages