Simple Way To Toggle Fullscreen With F11 In Pygtk
Solution 1:
Let's start with how to pick up on the keypress: we need to connect to the key-press-event
signal. But we need something to connect it to, of course.
This something should keep track of the window state, so it makes sense to use a class that connects to the window-state-event
signal and keeps track of whether the window is full screen or not.
So we need an object that:
- Keeps track of the fullscreen/not-fullscreen state of a particular window, and
- Detects a keypress event and figures out what to do with it
How do we actually toggle the fullscreen state though? Easy, use the window.fullscreen()
/ window.unfullscreen()
functions.
So we have something like:
classFullscreenToggler(object):
def__init__(self, window, keysym=gtk.keysyms.F11):
self.window = window
self.keysym = keysym
self.window_is_fullscreen = False
self.window.connect_object('window-state-event',
FullscreenToggler.on_window_state_change,
self)
defon_window_state_change(self, event):
self.window_is_fullscreen = bool(
gtk.gdk.WINDOW_STATE_FULLSCREEN & event.new_window_state)
deftoggle(self, event):
if event.keyval == self.keysym:
if self.window_is_fullscreen:
self.window.unfullscreen()
else:
self.window.fullscreen()
This takes a window and optional keysym constant (defaulting to F11). Use it like so:
toggler = FullscreenToggler(window)
window.connect_object('key-press-event', FullscreenToggler.toggle, toggler)
Note the use of connect_object
instead of connect
, which saves us adding an unused parameter.
Side-note: If there was an easy way to check if the window was fullscreen, we could avoid this class-based approach and use a function, that did something like:
def fullscreen_toggler(window, event, keysym=gtk.keysyms.F11):
ifevent.keyval == keysym:
fullscreen = bool(
gtk.gdk.WINDOW_STATE_FULLSCREEN &
window.get_property('window-state'))
if fullscreen:
window.unfullscreen()
else:
window.fullscreen()
...and then...
window.connect('key-press-event', fullscreen_toggler)
But I couldn't find a property for this.
Post a Comment for "Simple Way To Toggle Fullscreen With F11 In Pygtk"