pygtk:如何强制消息对话框显示在屏幕中央?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没关系。找到了解决方案。
对于其他可能会思考同样问题的人来说,此问题的解决方案在于为 gtk.MessageDialog 构造指定父值。
如果您在类中使用glade gui,并且您的glade xml被加载到名为“gui”的变量中,则它看起来像这样:
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:
查看参考资料 PyGTK 2.0 参考手册
我还没有机会试试这个,但 MessageDialog 似乎是从 Window 派生的,它有一个 set_position 方法。
此方法接受以下其中一项:
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:
如果您的父窗口尚未显示,即如果要在类(您的类,而不是“父”窗口类)实例化期间显示消息对话框,则所提供的解决方案都不起作用。在此期间,Gtk 尚未放置窗口,即使 messagedialog 的代码位于显示窗口的代码之后。这意味着您的对话框将在某种程度上“无父”,并且消息对话框将出现在任何它喜欢的地方...
我对这个问题的天真的解决方案...
以及
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...
and
对于其他在 GTK3 中遇到 AttributeError: 'gi.repository.Gtk' object has no attribute 'GTK_WIN_POS_CENTER_ALWAYS' 问题的人,这里是似乎可以代替这些常量的数值(例如
self.set_position(position=3)
将对话框置于用户鼠标位置的中心)。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 exampleself.set_position(position=3)
centers the dialog on the users mouse position).