可调整大小的 Tk 窗口做得很好
我组装 Tk 窗口的方式有问题(在 Win XP 下使用 R tcltk 和 tcltk2)
library(tcltk)
library(tcltk2)
expandTk <- function() {
root <- tktoplevel()
# textbox with scroll bars
textbox <- tk2frame(root)
scr <- tkscrollbar(textbox, repeatinterval=5, command=function(...) tkyview(txt,...))
txt <- tktext(textbox, bg="white", font="courier", wrap="word", yscrollcommand=function(...)tkset(scr,...))
tkpack(txt, side="left", fill="both", expand=TRUE)
tkpack(scr, side="right", fill="y")
tkmark.set(txt,"insert","0.0")
tkpack(textbox, fill="both", expand=TRUE)
# status bar and size grip
statusText <- tclVar("")
f <- tk2frame(root, relief="sunken")
l <- tk2label(f, textvariable=statusText)
tkpack(l, side="left", pady=2, padx=5, expand=0, fill="x")
tkpack(f, side="left", expand=1, fill="x", anchor="s")
sg <- ttksizegrip(root)
tkpack(sg, side="left", expand=0, anchor="se")
}
窗口看起来不错,但是一旦我调整它的大小(即使其变小),滚动条和状态栏就会消失。我很确定这是一个用户错误,我见过其他 Tk 应用程序可以正确调整大小,但我不知道应该使用哪个选项...
任何提示表示赞赏, 卡斯滕
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 Tk
pack
几何管理器的标准行为。以下是pack
手册页的相关部分:因此,如果将整个窗口缩小到小于文本小部件请求的空间,则不会为其他小部件留下任何空间,并且它们将被取消映射。
最好的解决方案是使用
grid
几何管理器,而不是pack
。一般来说,我发现grid
比pack
更容易使用且功能强大,尽管您的情况可能会有所不同。特别是,它消除了对许多多余框架小部件的需要,这可以大大简化您的代码。我认为你可以使用这样的东西:
有一些关于使用 R 此处。
That's standard behavior for the Tk
pack
geometry manager. Here's a relevant section of thepack
man page:So if you shrink the overall window to be smaller than the space requested for the text widget, you leave no space for the other widgets, and they get unmapped.
The best solution is to use the
grid
geometry manager, instead ofpack
. Generally speaking, I findgrid
to be much easier to use and capable thanpack
anyway, although your mileage may vary. In particular, it eliminates the need for many superfluous frame widgets, which can simplify your code a lot.I think you can use something like this:
There's some more information about using the
grid
geometry manager from R here.如果您坚持打包小部件,则应该注意,如果没有足够的空间为所有小部件提供它们所要求的空间,则空间将优先分配给第一个打包的小部件(在特定容器)。首先放置状态栏,然后是滚动条,最后才是主小部件。 (您可能需要更改将特定小部件打包到哪一侧,以使其全部正常工作。)此外,如果它变得太复杂,请记住您可以将其打包在框架内;这为您提供了很多的灵活性。
但这就是使用网格几何管理器才有意义的一点。当您想要应用程序外观的最后 10% 时,它可以为您提供更精细的控制,并且需要更少的小部件嵌套来实现它。
If you're insistent on packing the widgets, you should be aware that if there isn't enough space to give all widgets the room they asked for, space is given preferentially to the first widgets packed (within a particular container). Put the statusbar in first, then the scrollbars, and only then the main widget. (You may need to alter which side you're packing particular widgets on to make it all work right.) Also, if it is getting too complicated, remember that you can pack inside frames packed inside frames; that gives you lots of flexibility.
But that's the point when using the grid geometry manager just makes sense. It gives you a lot more fine control when you're going for that last 10% of the look of your app, and needs less nesting of widgets to achieve it.
正如 Eric 指出的那样,网格绝对是最好的,但如果您确实想使用 pack,那么调整扩展 txt 小部件的大小(如下所示)将得到您想要的东西。有一些尺寸启发法可以改进。
将其添加到代码末尾:
Definitely grid is best, as Eric points out, but if you really want to use pack then resizing the expanding txt widget, like below, will get you what you are looking for. There are some size heuristics that could be improved.
Add this to the end of your code: