如何在 Perl Tkx 下重新定位主窗口?

发布于 2024-07-29 04:51:05 字数 925 浏览 7 评论 0原文

或多或少将许多旧的 Tk 脚本转换为 Tkx 后,我陷入了以下函数的端口困境,该函数将作为参数传入的窗口重新定位在屏幕中央。 我曾经在调用 MainLoop 之前调用它,此时 Tk 显然已经决定了 reqwidth 和 reqheight 值。

sub CenterWindow
{
    # Args: (0) window to center
    # (1) [optional] desired width
    # (2) [optional] desired height

    my($window, $width, $height) = @_;

    $window->idletasks;
    $width = $window->reqwidth unless $width;
    $height = $window->reqheight unless $height;

    my $x = int(($window->screenwidth / 2) - ($width / 2));
    my $y = int(($window->screenheight / 2) - ($height / 2));

    $window->geometry($width . "x" . $height . "+" . $x . "+" . $y);
}

如有必要,idletasks 可以更改为 Tkx::update(),但我找不到这个旧 Tk 例程的窗口特定部分的任何明显翻译。 Tkx 似乎没有可通过 cget() 检索的 reqwidth、reqheight、screenwidth 或 screenheight 的等效项。

我现在在 Tkx 中使用网格布局,而不是 Tk 中的包布局,这一事实是否有任何相关性?

顺便说一句,如果这有什么区别的话,我正在 Windows Vista 上运行 ActivePerl 5.10。

Having more or less converted a lot of old Tk scripts over to Tkx I'm stuck for an port for the following function which repositions the window passed in as a parameter in the centre of the screen. I used to call this just before calling MainLoop, by which point Tk had obviously decided on the reqwidth and reqheight values.

sub CenterWindow
{
    # Args: (0) window to center
    # (1) [optional] desired width
    # (2) [optional] desired height

    my($window, $width, $height) = @_;

    $window->idletasks;
    $width = $window->reqwidth unless $width;
    $height = $window->reqheight unless $height;

    my $x = int(($window->screenwidth / 2) - ($width / 2));
    my $y = int(($window->screenheight / 2) - ($height / 2));

    $window->geometry($width . "x" . $height . "+" . $x . "+" . $y);
}

idletasks can be changed to Tkx::update() if necessary, but I am at a loss to find any obvious translation for the window specific parts of this old Tk routine. Tkx doesn't seem to have an equivalent for reqwidth, reqheight, screenwidth or screenheight retrieveable by cget().

Does the fact that I'm now using a grid layout in Tkx, rather than a pack layout in Tk have any relevance?

BTW I'm running ActivePerl 5.10 on Windows Vista if that makes any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2024-08-05 04:51:05

几何管理器并不重要;重要的是。 它仅控制小部件在框架内的布局方式。 您需要的数据可通过 winfo命令:

sub CenterWindow {
    # Args: (0) window to center
    # (1) [optional] desired width
    # (2) [optional] desired height

    my ($window, $width, $height) = @_;

    Tkx::update('idletasks');
    $width  ||= Tkx::winfo('reqwidth',  $window);
    $height ||= Tkx::winfo('reqheight', $window);

    my $x = int((Tkx::winfo('screenwidth',  $window) / 2) - ($width / 2));
    my $y = int((Tkx::winfo('screenheight', $window) / 2) - ($height / 2));

    $window->g_wm_geometry($width . "x" . $height . "+" . $x . "+" . $y);
}

据我所知,你必须直接调用 winfo,不能使用 OO 语法。

您可能已经弄清楚了这一点,但是在使用 Tkx 进行开发时,您需要参考 Tcl Tk 文档Tkx 文档仅描述了(非常薄的)翻译层的工作原理。 另外,usenet 群组 comp.lang.perl.tk(或ptk 邮件列表 桥接它)可能是最好的资源对于 Tkx 问题,至少可以在那里找到 Tkx 背后的 ActiveState 人员之一。

The geometry manager doesn't matter; that only controls how widgets are laid out within a frame. The data you're after is available via the winfo command:

sub CenterWindow {
    # Args: (0) window to center
    # (1) [optional] desired width
    # (2) [optional] desired height

    my ($window, $width, $height) = @_;

    Tkx::update('idletasks');
    $width  ||= Tkx::winfo('reqwidth',  $window);
    $height ||= Tkx::winfo('reqheight', $window);

    my $x = int((Tkx::winfo('screenwidth',  $window) / 2) - ($width / 2));
    my $y = int((Tkx::winfo('screenheight', $window) / 2) - ($height / 2));

    $window->g_wm_geometry($width . "x" . $height . "+" . $x . "+" . $y);
}

As far as I can tell, you have to invoke winfo directly, you can't use the OO syntax.

You've probably figured this out already, but when developing with Tkx you need to refer to the Tcl Tk documentation. The Tkx documentation just describes how the (very thin) translation layer works. Also, the usenet group comp.lang.perl.tk (or the ptk mailing list bridge to it) is probably the best resource for Tkx questions as at least one of the ActiveState guys behind Tkx can be found there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文