简体   繁体   English

使用PyGTK / win32,如何在其父窗口附近放置一个新窗口,但不在屏幕外?

[英]With PyGTK/win32, how do I place a new window near its parent but not off-screen?

With PyGTK running on Windows , I want to have a new window pop up near a parent window, but never off-screen. Windows上运行PyGTK的情况下 ,我希望在父窗口附近弹出一个新窗口,但不要在屏幕外显示。

I observed the behavior I want in Microsoft Wordpad in both Windows 7 and Windows XP . 我在Windows 7Windows XP的 Microsoft Wordpad中都观察到了我想要的行为。 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; 我尝试使用gtk.WIN_POS_MOUSE ,但是当主菜单靠近屏幕边缘时; 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. 我希望在子窗口上调用set_transient_for(main_menu)会通知Windows我希望我的窗口位于其父窗口附近。 However, Windows just places it at the top left of the desktop -- not even necessarily on the same screen as the main menu. 但是, Windows只是将其放在桌面的左上方-甚至不一定与主菜单位于同一屏幕上。

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. 如您所见,sub_window在屏幕外部分显示。 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. 如果注释掉w.set_position(gtk.WIN_POS_CENTER_ON_PARENT)行,您将看到Windows窗口管理器仅将子窗口放在桌面的左上角。 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 更新:我于2011年2月23日提交了pyGTK的错误报告: 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). 如果您遇到此问题,请及时解决该错误,以便对其进行修复(或使我们获得解决方法)。

On my Windows 7 system the popup window appears in the center of the main window. 在我的Windows 7系统上,弹出窗口出现在主窗口的中央。 I have one question: why are you using a window instead of a dialog? 我有一个问题:您为什么要使用窗口而不是对话框?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM