Frage

I wrote global shortcut example for hide and show my windows with key like 'F12', I used python-xlib and some script named 'pyxhook' everything works fine except when i want to hide() and show() window few times my proccess turning a zombie, but same code working with hiding and showing just button.

#!/usr/bin/python
# -*- coding: utf-8; -*-
from gi.repository import Gtk, GObject
from pyxhook import HookManager
GObject.threads_init()
class Win(Gtk.Window):

    def __init__(self):
        super(Win, self).__init__()
        self.connect('destroy', Gtk.main_quit)
        self.button = Gtk.Button()
        self.add(self.button)
        self.resize(200,150)
        self.show_all()

    def handle_global_keypress(self, event):
        if event.Key == 'F12':
            if self.get_visible():
                self.hide()
            else:
                self.show()

            ### this part works fine with button

            #if self.button.get_visible():
            #    self.button.hide()
            #else:
            #    self.button.show()

def main():
    app = Win()
    hm = HookManager()
    hm.HookKeyboard()
    hm.KeyDown = app.handle_global_keypress
    hm.start()
    Gtk.main()

    hm.cancel()

if __name__ == "__main__":    
    main()

edit: i solved my problem using Keybinder library instead of coding pure python keybinder. http://kaizer.se/wiki/keybinder/

War es hilfreich?

Lösung

I'm unable to answer your specific question but I might suggest another option. Guake console implements this very same behavior but using dbus:

http://guake.org/

In the dbusiface.py file you can find:

import dbus
import dbus.service
import dbus.glib
import gtk
import guake.common
dbus.glib.threads_init()

DBUS_PATH = '/org/guake/RemoteControl'
DBUS_NAME = 'org.guake.RemoteControl'

class DbusManager(dbus.service.Object):
    def __init__(self, guakeinstance):
        self.guake = guakeinstance
        self.bus = dbus.SessionBus()
        bus_name = dbus.service.BusName(DBUS_NAME, bus=self.bus)
        super(DbusManager, self).__init__(bus_name, DBUS_PATH)

    @dbus.service.method(DBUS_NAME)
    def show_hide(self):
        self.guake.show_hide()

Among others methods. This is worth to explore. Please also note that Guake is developed using PyGtk and not PyGObject, but anyway you can get some ideas.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top