How do I raise a window that is minimized or covered with PyGObject?
Asked Answered
A

3

7

I'd been using the answer provided in the PyGTK FAQ, but that doesn't seem to work with PyGObject. For your convenience, here is a test case that works with PyGTK, and then a translated version that doesn't work with PyGObject.

PyGTK Version:

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 version:

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

When I click the button in the PyGObject version, the other window isn't raised, and I get this error:

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

So I guess there must be some other way to get the Gdk.window in PyGObject?

Or is there some different/better way of accomplishing the same goal?

Any ideas?

Anticoagulant answered 29/1, 2012 at 15:50 Comment(0)
L
10

As explained in this post, there are two options:

Raise the window temporarily (probably what you're looking for):

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

Raise the window permanently (or until explicitly changed by configuration):

def raise_window(widget, w2):
    w2.set_keep_above(True)
Lindsylindy answered 29/1, 2012 at 16:29 Comment(1)
Awesome, that seems even more correct than the way I was doing it previously.Anticoagulant
O
4

present didn't work for me for a temporary raise, but this did:

win.set_keep_above(True)
win.set_keep_above(False)
Overpay answered 14/1, 2015 at 13:8 Comment(0)
P
1

This is what works best for me:

To bring window to front when the application start and then behave normally

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

using these won't work

win.show_all()
win.set_keep_above(True)
win.set_keep_above(False)
Psychobiology answered 16/5, 2020 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.