如何用PyGObject提升被最小化或覆盖的窗口?

7 投票
3 回答
4733 浏览
提问于 2025-04-17 11:53

我之前一直在使用PyGTK常见问题解答中提供的答案,但这个方法似乎在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 个回答

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)
3

我尝试用present来临时加薪,但没成功,不过这个方法有效:

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

在这个帖子中解释了两种选择:

临时把窗口提到最上面(这可能是你想要的):

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

永久把窗口提到最上面(或者直到你通过设置明确更改):

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

撰写回答