如何引发一个最小化或被PyGObject覆盖的窗口?

时间:2012-01-29 15:50:25

标签: python gtk pygtk gtk3 pygobject

我一直在使用PyGTK FAQ中提供的答案,但这似乎不适用于PyGObject。为方便起见,这里有一个与PyGTK一起使用的测试用例,然后是一个不能与PyGObject一起使用的翻译版本。

PyGTK版本:

import gtk

def raise_window(widget, w2):
    w2.window.show()

w1 = gtk.Window()
w1.set_title('Main window')
w2 = gtk.Window()
w2.set_title('Other window')

b = gtk.Button('Move something on top of the other window.\nOr, minimize the'
               'other window.\nThen, click this button to raise the other'
               'window to the front')
b.connect('clicked', raise_window, w2)

w1.add(b)

w1.show_all()
w2.show_all()

w1.connect('destroy', gtk.main_quit)
gtk.main()

PyGObject版本:

from gi.repository import Gtk

def raise_window(widget, w2):
    w2.window.show()

w1 = Gtk.Window()
w1.set_title('Main window')
w2 = Gtk.Window()
w2.set_title('Other window')

b = Gtk.Button('Move something on top of the other window.\nOr, minimize the'
               'other window.\nThen, click this button to raise the other'
               'window to the front')
b.connect('clicked', raise_window, w2)

w1.add(b)

w1.show_all()
w2.show_all()

w1.connect('destroy', Gtk.main_quit)
Gtk.main()

当我单击PyGObject版本中的按钮时,不会引发另一个窗口,我收到此错误:

Traceback (most recent call last):
  File "test4.py", line 4, in raise_window
    w2.window.show()
AttributeError: 'Window' object has no attribute 'window'

所以我想在PyGObject中必须有其他方法来获取Gdk.window?

或者是否有一些不同/更好的方法来实现同一目标?

有什么想法吗?

3 个答案:

答案 0 :(得分:7)

正如本post中所述,有两种选择:

暂时提升窗口(可能是您正在寻找的):

def raise_window(widget, w2):
    w2.present()

永久提升窗口(或直到通过配置明确更改):

def raise_window(widget, w2):
    w2.set_keep_above(True)

答案 1 :(得分:1)

present对我来说暂时加薪不起作用,但确实如此:

win.set_keep_above(True)
win.set_keep_above(False)

答案 2 :(得分:1)

这是最适合我的:

在应用程序启动时将窗口置于最前面,然后正常运行

win.set_keep_above(True)  # if used alone it will cause window permanently on top
win.show_all()  # show your window, should be in the middle between these 2 calls
win.set_keep_above(False) # disable always on top

使用这些将无效

win.show_all()
win.set_keep_above(True)
win.set_keep_above(False)