Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

can a toplevel auto size to contain packed entry widgets

瀏覽次數:39 次
跳到第一則未讀訊息

two...@gmail.com

未讀,
2018年6月13日 下午2:44:062018/6/13
收件者:
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

未讀,
2018年6月13日 下午3:38:362018/6/13
收件者:
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

未讀,
2018年6月13日 下午3:44:052018/6/13
收件者:
Not creating a new toplevel, just using the default . that it starts up with

Brad Lanam

未讀,
2018年6月13日 下午3:51:272018/6/13
收件者:
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

未讀,
2018年6月13日 下午4:02:412018/6/13
收件者:
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

未讀,
2018年6月13日 下午4:10:302018/6/13
收件者:
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

未讀,
2018年6月13日 下午4:11:372018/6/13
收件者:
To be more precise, it doesn't autofit horizontally, but does seem to expand vertically.

two...@gmail.com

未讀,
2018年6月13日 下午4:14:482018/6/13
收件者:
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

未讀,
2018年6月13日 下午4:17:572018/6/13
收件者:
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

未讀,
2018年6月13日 下午4:31:232018/6/13
收件者:
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

未讀,
2018年6月13日 下午4:51:452018/6/13
收件者:
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

未讀,
2018年6月13日 下午4:59:292018/6/13
收件者:
One can also stick a scrollbar (horizontal) on an entry widget. Probably
overkill, but doable if one must.

two...@gmail.com

未讀,
2018年6月13日 下午5:15:132018/6/13
收件者:
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

未讀,
2018年6月13日 下午5:47:352018/6/13
收件者:
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

未讀,
2018年6月14日 上午11:31:022018/6/14
收件者:
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 則新訊息