使用 PyGTK/win32,如何将新窗口放置在其父窗口附近但不离开屏幕?

发布于 2024-09-17 00:01:12 字数 2025 浏览 2 评论 0原文

Windows 上运行 PyGTK 时,我希望在父窗口附近弹出一个新窗口,但永远不会离开屏幕。

我在 Windows 7Windows XP 中的 Microsoft Wordpad 中观察到了我想要的行为。如果您将窗口做得非常小并将其移动到桌面右下角,右键单击文本字段,然后打开段落菜单,则弹出的对话框完全可见。即使写字板窗口部分不在屏幕上,也会发生这种情况。子对话框不会在相对于主窗口的固定位置弹出。它只是突然弹出并完全可见。

我的应用程序由一个主屏幕组成,它会生成子窗口,这些子窗口会阻止应用程序的其余部分,直到用户使用完它们为止。用户可能必须按顺序打开和关闭许多子窗口,因此我希望它们出现在单击按钮的位置附近,这样用户就不必在整个屏幕上移动鼠标。

我尝试使用gtk.WIN_POS_MOUSE,但是当主菜单靠近屏幕边缘时;生成的子窗口通常会部分离开屏幕。

我希望在子窗口上调用 set_transient_for(main_menu) 应该通知 Windows 我希望我的窗口靠近其父窗口。然而,Windows只是将其放置在桌面的左上角——甚至不一定与主菜单位于同一屏幕上。

下面的代码将通过在屏幕左下角弹出一个窗口来演示该问题,其中包含一个按钮,单击该按钮会生成一个子窗口:

import gtk

class MyWindow(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.connect("delete-event",self.on_delete_event)
        button = gtk.Button("Open Subwindow")
        button.connect("clicked",self.open_sub_window)
        self.add(button)
        self.set_gravity(gtk.gdk.GRAVITY_SOUTH_EAST)
        self.show_all()
        width, height = self.get_size()
        self.move(gtk.gdk.screen_width() - width, gtk.gdk.screen_height() - height)

    def on_delete_event(self, widget=None, data=None):
        gtk.main_quit()

    def open_sub_window(self, widget=None, data=None):
        w = gtk.Window()
        w.set_size_request(200,200)
        w.set_transient_for(self)
        w.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
        w.show()

if __name__=="__main__":
    MyWindow()
    gtk.main()

如您所见,sub_window 部分显示在屏幕外。 如果您注释掉 w.set_position(gtk.WIN_POS_CENTER_ON_PARENT) 行,您将看到 Windows 窗口管理器只是将子窗口放置在桌面的最左上角。不是很有用!

有没有办法通过检查窗口最终的位置然后将窗口移动到完全离开屏幕的位置来获得所需的行为,而无需手动管理窗口定位?

更新: 我于 2011 年 2 月 23 日提交了 pyGTK 的错误报告: http://bugzilla.gnome .org/show_bug.cgi?id=643138

尚未收到开发人员或其他用户的任何回复。如果您遇到此问题,请指出该错误,以便它得到修复(或者以便我们可以找到解决方法)。

With PyGTK running on Windows, I want to have a new window pop up near a parent window, but never off-screen.

I observed the behavior I want in Microsoft Wordpad in both Windows 7 and Windows XP. If you make the window very small and move it to the bottom right of the desktop, right click in the text field, and open the Paragraph menu, the dialog pops up fully visible. This happens even if the Wordpad window is partially off-screen. The child dialog does not pop up in a fixed position relative to the main window. It just pops up close, and fully visible.

My application consists of a main screen which spawns child windows that block the rest of the application until the user is finished using them. The user may have to open and close many child windows in sequence, so I want them to appear near where they click on the button, so the user doesn't have to move the mouse all over the screen.

I tried using gtk.WIN_POS_MOUSE, but when the main menu is near an edge of the screen; the child window that spawns often ends up partially off-screen.

I would expect that a call to set_transient_for(main_menu) on a child window should inform Windows that I want my window to be near its parent. However, Windows just places it at the top left of the desktop -- not even necessarily on the same screen as the main menu.

The following code will demonstrate the problem by popping up a window at the bottom left of your screen that contains a button which spawns a subwindow when clicked:

import gtk

class MyWindow(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.connect("delete-event",self.on_delete_event)
        button = gtk.Button("Open Subwindow")
        button.connect("clicked",self.open_sub_window)
        self.add(button)
        self.set_gravity(gtk.gdk.GRAVITY_SOUTH_EAST)
        self.show_all()
        width, height = self.get_size()
        self.move(gtk.gdk.screen_width() - width, gtk.gdk.screen_height() - height)

    def on_delete_event(self, widget=None, data=None):
        gtk.main_quit()

    def open_sub_window(self, widget=None, data=None):
        w = gtk.Window()
        w.set_size_request(200,200)
        w.set_transient_for(self)
        w.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
        w.show()

if __name__=="__main__":
    MyWindow()
    gtk.main()

As you can see, the sub_window shows up partially off-screen.
If you comment out the line w.set_position(gtk.WIN_POS_CENTER_ON_PARENT) you will see that the Windows window manager just places the subwindow at the very top left of the desktop. Not very useful!

Is there a way to get the desired behavior without resorting to manually managing Window positioning by checking what location the window ends up at and then moving the window to a fully off-screen position?

UPDATE:
I filed a bug report for pyGTK on Feb 23, 2011: http://bugzilla.gnome.org/show_bug.cgi?id=643138

It has not yet had any responses from the devs or other users. If you are having this problem please chime in on the bug so it gets fixed (or so that we might get a workaround).

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文