Simple way to toggle fullscreen with F11 in PyGTK

2019-06-22 16:52发布

I am not a professional programmer but am regularly using PyGTK and Cairo for data visualization testing and prototyping.

I have a standard template of PyGTK that I took from the web, which does the "standard things" GTK needs to work:

import pygtk
pygtk.require('2.0')
import gtk

"""
lots of stuff
"""

if __name__ == "__main__":
    mainWindow = gtk.Window()
    mainWindow.set_title(some_title)
    mainWindow.connect("delete-event", gtk.main_quit)
    #mainWindow.set_position(gtk.WIN_POS_CENTER)
    #mainWindow.fullscreen()
    mainWindow.show_all()
    gtk.main()

I usually see most apps using F11 to toggle fullscreen, so I wonder if there is a simple way to add this functionality to my script. I suppose it probably means connecting event signals to gtk functions or methods or something like that, but my n00bness prevents me from knowing how to do that.

Any help would be appreciated, thanks. (links preferred, so that I could do the homework myself ;o)

1条回答
甜甜的少女心
2楼-- · 2019-06-22 17:22

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:

  1. Keeps track of the fullscreen/not-fullscreen state of a particular window, and
  2. 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:

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

    def on_window_state_change(self, event):
        self.window_is_fullscreen = bool(
            gtk.gdk.WINDOW_STATE_FULLSCREEN & event.new_window_state)

    def toggle(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):
    if event.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.

查看更多
登录 后发表回答