在PyGTK中用F11切换全屏的简单方法 [英] Simple way to toggle fullscreen with F11 in PyGTK

查看:198
本文介绍了在PyGTK中用F11切换全屏的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是专业程序员,但我经常使用PyGTK和Cairo进行数据可视化测试和原型设计。



我有一个标准的PyGTK模板, web,这是GTK需要工作的标准东西:

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


很多东西


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

我通常会看到大多数使用F11切换全屏的应用程序,因此我想知道是否有一种简单的方法将此功能添加到我的脚本中。我想这可能意味着将事件信号连接到gtk函数或方法或类似的东西,但我的n00bness阻止我知道如何做到这一点。



任何帮助将不胜感激, 谢谢。 (链接首选,以便我可以自己做作业; o) 解决方案

我们先来看看如何选择按键:我们需要连接到 按键事件 信号。当然,我们需要将它连接到



这个东西应该跟踪窗口状态,所以使用它是有意义的一个连接到的类 window-state-event 信号并跟踪窗口是否全屏显示。



因此,我们需要一个对象:


  1. 跟踪特定窗口的全屏/非全屏状态,以及

  2. 检测一个按键事件,并找出如何处理它

我们如何切换全屏状态虽然?很简单,使用 window.fullscreen() / window.unfullscreen() 函数。 如:

  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_wind ow_state)

def toggle(self,event):
if event.keyval == self.keysym:
如果self.window_is_fullscreen:
self.window.unfullscreen ()
else:
self.window.fullscreen()

一个窗口和可选的键盘常量(默认为F11)。像这样使用它:

  toggler = FullscreenToggler(window)
window.connect_object('key-press-event' ,FullscreenToggler.toggle,toggler)

请注意使用 connect_object 而不是 connect ,这样可以节省我们添加一个未使用的参数的空间。

附注:如果有一个简单的方法来检查窗口是否全屏显示,我们可以避免使用这种基于类的方法,并使用一个函数,如下所示:

  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'))
如果全屏:
window.unfullscreen()
否则:
window.fullscreen()

...然后... p>

  window.connect('key-press-event',fullscreen_toggler)

但我找不到这个属性。


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)

解决方案

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.

这篇关于在PyGTK中用F11切换全屏的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆