何时以及如何使用 Tk 设置窗口大小 [英] When and how to set window size using Tk

查看:62
本文介绍了何时以及如何使用 Tk 设置窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来将内容输出到一个窗口,该窗口包含一个 Tkhtml 小部件和一个在 ttk::panedwindow 中显示在彼此上方的文本小部件.两个小部件都可以垂直和水平滚动.还有一个按钮允许用户清除文本小部件.

I'm writing a script to output things to a window comprising a Tkhtml widget and a text widget displayed above each other in a ttk::panedwindow. Both widgets are scrollable both vertically and horizontally. There's also a button allowing the user to clear the text widget.

我正在运行 Ubuntu 的笔记本电脑上进行一些开发.窗口管理器允许以 2x2 阵列的形式提供四个桌面工作区,当第一次显示 GUI 时,底部的五分之一左右从我正在使用的工作区渗入下面的工作区.不得不调整窗口大小以使其足够短以适应屏幕,这很令人恼火,所以我想调整它的大小.

I'm doing some of the development on a laptop running Ubuntu. The window manager allows for four desktop workspaces in a 2x2 array, and when the GUI is first displayed, the bottom fifth or so bleeds from the workspace I'm using into the one below. It's irritating to have to muck around resizing the window to make it short enough to fit onto the screen so I want to resize it.

(我认为)我大致知道如何执行此操作,即绑定到适当的事件,以便我可以在首次显示窗口时运行脚本.(从脚本中重置绑定,使其只触发一次.)我认为适当的事件"是 ,但是当第一次映射窗口时,两个小部件为零高度(由 [wininfo height] 报告).我尝试绑定到 <Expose> 并且这似乎有效,([winfo height] 返回合理的数字,)所以:

(I think) I know approximately how to do this, i.e bind to an appropriate event so that I can run a script when the window is first displayed. (Reset the binding from within the script so that it only fires once.) I believed that the "appropriate event" was <Map>, but when the window was first mapped, the two widgets had zero height (as reported by [winfo height]). I tried binding to <Expose> and this seems to work, ([winfo height] returns sensible numbers,) so:

绑定触发时,[wm geometry] 报告几何图形为 815x1029+49+24,[winfo height] 报告两个小部件的两个高度为 600和 366,并且 [wm screenheight] 返回 800 的高度.我知道 GUI 中还有其他各种零碎的部分,所以我对初始布局中有 63 个像素下落不明并不感到惊讶.我假设在调整大小后我需要相同数量的空间,所以我应该请求 815x737+49+24 的几何图形,但是当我这样做时,一个条子(大约是较低的水平滚动条)仍然流到下一个工作区.

When the binding fires, [wm geometry] reports the geometry as 815x1029+49+24, [winfo height] reports the two heights of the two widgets as 600 and 366, and [wm screenheight] returns a height of 800. I know there are various other bits and pieces in the GUI so I'm not surprised that there are 63 pixels unaccounted for in the initial layout. I assume that I need the same amount of space after resizing, so I should request a geometry of 815x737+49+24, but when I do, a sliver (approximately the lower horizontal scrollbar) still bleeds onto the next workspace.

通过手动处理,我知道当一切都很好地适合屏幕时,几何图形应该是 815x717+49+24,所以我在允许其他位和图形用户界面的碎片".这工作正常,但似乎有点不雅(大量的英国轻描淡写:-)),所以:

By mucking around manually, I know that when everything fits nicely onto the screen the geometry should be 815x717+49+24, so I've added a fudge factor of 20 to the amount of space I allow for the "other bits and pieces of the GUI". This works fine but seems a little inelegant (massive British understatement :-) ), so:

我在 Ubuntu 12.04.5 LTS 上使用 Tk 8.6.1.我使用的是 Compiz 窗口管理器的 0.9.7.12 版.

I'm using Tk 8.6.1 on Ubuntu 12.04.5 LTS. I'm using version 0.9.7.12 of the Compiz window manager.

让我震惊的是,我应该找到各种窗格的高度,而不是 Tkhtml 和文本小部件,因为这将解释滚动条和清除"按钮.窗格高度最初是 615 和 409,但这只是意味着我必须将我的软糖因子从 20 增加到 78 才能使请求高度达到我想要的值 717.有没有办法预测顶层请求的高度包含我的窗格窗口以便让顶层填充屏幕?

It struck me that I ought to find the height of the various panes, rather than the Tkhtml and text widgets as that would account for the scrollbars and the "Clear" button. The pane heights are 615 and 409 initially, but this just means that I have to increase my fudge factor from 20 to 78 to get the request height to my desired value of 717. Is there any way to predict what height to request for the toplevel containing my paned window in order to get that toplevel to fill the screen?

推荐答案

我建议绑定到 .在尝试调整高度之前,我会等待所有:平移窗口、主 tkhtml 窗口、文本窗口和滚动条被映射.

I recommend binding to <Map>. I would wait for all of: the paned window, the main tkhtml window, the text window and the scrollbars to be mapped before trying to adjust heights.

对于 Linux,[winfo screenheight .] 不知道任何已配置的面板,因此您必须减去这些面板.您可以通过创建一个窗口,执行 wm attributes 来获取实际可用的屏幕高度.-zoomed 1,然后获取窗口的几何形状.

For Linux, the [winfo screenheight .] does not know about any panels that are configured, so you have to subtract those. You can get the actual screenheight available by creating a window, doing a wm attributes . -zoomed 1, then getting the geometry of the window.

窗口的标题栏和边框占用空间.这些所需的空间可以通过比较 [winfo geometry .][wm geometry .] 的输出来计算.

The title bar and borders of the window take up space. The space needed for these can be calculated by comparing the output from [winfo geometry .] and [wm geometry .].

还有 panedwindow 窗扇高度以及任何边距和填充.您可以通过从 [winfo height .] 中减去窗格窗口高度来计算这些总数.

There is also the panedwindow sash height and any margins and padding. You can calculate the total of these by subtracting the panedwindow heights from [winfo height .].

这是一种总是有效的方法,但是它不是很吸引人,因为用户会看到窗口随着大小的变化而闪烁.

Here is one method that would always work, however, it is not very attractive, as the user would see the windows flashing as they change size.

  • 最大化窗口(wm attributes .-zoomed 1)
  • 获取窗口几何图形并解析出屏幕高度.
  • 去最大化窗口(wm attributes .-zoomed 0)
  • 获取窗口几何图形并替换屏幕高度使用最大化的屏幕高度.
  • 设置窗口几何形状.

这篇关于何时以及如何使用 Tk 设置窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆