在 PyGTK 中使用 F11 切换全屏的简单方法

发布于 2024-10-20 15:17:10 字数 619 浏览 0 评论 0原文

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

我有一个从网上获取的 PyGTK 标准模板,它执行 GTK 需要工作的“标准功能”:

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

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

任何帮助将不胜感激,谢谢。 (首选链接,这样我就可以自己做作业;o)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2024-10-27 15:17:10

让我们从如何获取按键开始:我们需要连接到 按键事件信号。但当然,我们需要一些东西来连接它。

这个东西应该跟踪窗口状态,因此使用连接到 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_window_state)

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

这需要一个窗口和可选的 keysym 常量(默认为 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'))
        if fullscreen:
            window.unfullscreen()
        else:
            window.fullscreen()

...然后...但

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

我找不到这个属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文