或多或少地将许多旧的 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。