pygtk:如何强制消息对话框显示在屏幕中央?

发布于 2024-08-16 04:53:23 字数 184 浏览 5 评论 0原文

我有一个 Glade GUI,我正在使用用 pygtk 创建的 dome gtk.MessageDialog 小部件进行用户交互。我的问题是,每当我在屏幕上抛出对话框消息时,它们就会出现在各处。一个可能出现在右上角,下一个可能出现在左下角,左上角,左中角等......

有没有一种方法可以强制这些东西显示在屏幕中央或父级所在的位置窗口在?

I have a glade GUI and i'm using dome gtk.MessageDialog widgets created with pygtk for user interaction. My problem is that whenever I throw a dialog message on the screen, they show up all over the place. One might show up on the top right corner, the next on the bottom left, top left, mid left etc...

Is there a way to force these things to show up in the center of the screen or at the position where the parent window is at?

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

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

发布评论

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

评论(4

深府石板幽径 2024-08-23 04:53:23

没关系。找到了解决方案。

对于其他可能会思考同样问题的人来说,此问题的解决方案在于为 gtk.MessageDialog 构造指定父值。
如果您在类中使用glade gui,并且您的glade xml被加载到名为“gui”的变量中,则它看起来像这样:

#!/usr/bin/env/python
par = self.gui.get_widget('your_parent_window')

msg = gtk.MessageDialog(type=gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK, parent=par)
if msg.run():
    msg.destroy()
    return None

Never mind. Found the solution.

For others who might wander about the same thing, the solution to this problem lies in specifying a parent value to the gtk.MessageDialog construct.
If you are using a glade gui, in your class, and your glade xml is loaded in to a variable named 'gui', it would look like this:

#!/usr/bin/env/python
par = self.gui.get_widget('your_parent_window')

msg = gtk.MessageDialog(type=gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK, parent=par)
if msg.run():
    msg.destroy()
    return None
美羊羊 2024-08-23 04:53:23

查看参考资料 PyGTK 2.0 参考手册

我还没有机会试试这个,但 MessageDialog 似乎是从 Window 派生的,它有一个 set_position 方法。

此方法接受以下其中一项:

# No influence is made on placement.
gtk.WIN_POS_NONE 

# Windows should be placed in the center of the screen.
gtk.WIN_POS_CENTER 

# Windows should be placed at the current mouse position.
gtk.WIN_POS_MOUSE 

# Keep window centered as it changes size, etc.
gtk.WIN_POS_CENTER_ALWAYS

# Center the window on its transient parent
# (see the gtk.Window.set_transient_for()) method.
gtk.WIN_POS_CENTER_ON_PARENT 

Check out the reference material at PyGTK 2.0 Reference Manual

I have not had a chance to try this but MessageDialog seems to be derived from Window which has a set_position method.

This method accepts one of the following:

# No influence is made on placement.
gtk.WIN_POS_NONE 

# Windows should be placed in the center of the screen.
gtk.WIN_POS_CENTER 

# Windows should be placed at the current mouse position.
gtk.WIN_POS_MOUSE 

# Keep window centered as it changes size, etc.
gtk.WIN_POS_CENTER_ALWAYS

# Center the window on its transient parent
# (see the gtk.Window.set_transient_for()) method.
gtk.WIN_POS_CENTER_ON_PARENT 
单身情人 2024-08-23 04:53:23

如果您的父窗口尚未显示,即如果要在类(您的类,而不是“父”窗口类)实例化期间显示消息对话框,则所提供的解决方案都不起作用。在此期间,Gtk 尚未放置窗口,即使 messagedialog 的代码位于显示窗口的代码之后。这意味着您的对话框将在某种程度上“无父”,并且消息对话框将出现在任何它喜欢的地方...

我对这个问题的天真的解决方案...

GObject.timeout_add(interval=50, function=self.stupid_dialog_1)

以及

def stupid_dialog_1(self):
    par = self.gui.get_widget('your_parent_window')
    msg = gtk.MessageDialog(type=gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK, parent=par)
    # do anything here...
    return False #stop the timer...

None of the provided solutions will work if your parent window is not yet shown, that is if the messagedialog is to be shown during the instantiation of a class (your class, not the "parent" window class). During this time Gtk has not yet placed the window, even if code for messagedialog is after the code that shows the window. Which means your dialog box will be somehow "parentless" and the message dialog will appear wherever it likes...

My naive solution for that problem...

GObject.timeout_add(interval=50, function=self.stupid_dialog_1)

and

def stupid_dialog_1(self):
    par = self.gui.get_widget('your_parent_window')
    msg = gtk.MessageDialog(type=gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK, parent=par)
    # do anything here...
    return False #stop the timer...
青衫负雪 2024-08-23 04:53:23

对于其他在 GTK3 中遇到 AttributeError: 'gi.repository.Gtk' object has no attribute 'GTK_WIN_POS_CENTER_ALWAYS' 问题的人,这里是似乎可以代替这些常量的数值(例如 self.set_position(position=3) 将对话框置于用户鼠标位置的中心)。

  0  WIN_POS_NONE   
  1  WIN_POS_CENTER  
  2  WIN_POS_MOUSE  
  3  WIN_POS_CENTER_ALWAYS  
  4  WIN_POS_CENTER_ON_PARENT   

For anyone else struggling with AttributeError: 'gi.repository.Gtk' object has no attribute 'GTK_WIN_POS_CENTER_ALWAYS' with GTK3 here are the numeric values that seem to work in place of those constants (for example self.set_position(position=3) centers the dialog on the users mouse position).

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