如何在 python 中正确显示和隐藏 GTK window

How to properly show and hide GTK window in python

所以我有以下代码 (python 2.7) 检查按键和 shows/hides 带有几个 gtk 输入框的 gtk window 的组合。 show/hide 一直工作到 window 锁定并停止响应,并且 window 的内部变黑。一旦 window 变黑,我必须终止进程并重新启动它。我已经尝试了 show_all() 的所有不同组合,并在隐藏 window 后返回 True 无济于事。我不确定我在这里做错了什么。

最后我希望能够按下这个组合键和 show/hide 这个 window 而内容不会消失。

#!/usr/bin/python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import pyxhook

class MyWindow(Gtk.Window):

    def __init__(self):
    Gtk.Window.__init__(self, title="Configurator")

    self.box = Gtk.Box(spacing=6)
    self.add(self.box)

    self.ipEntry = Gtk.Entry()
    self.ipEntry.set_text("IP Address")

    self.maskEntry = Gtk.Entry()
    self.maskEntry.set_text("NetMask")

    self.gatewayEntry = Gtk.Entry()
    self.gatewayEntry.set_text("gatewayEntry")

    self.button1 = Gtk.Button(label="Save")
    self.button1.connect("clicked", self.on_button1_clicked)
    self.box.pack_start(self.ipEntry, True, True, 0)
    self.box.pack_start(self.maskEntry, True, True, 0)
    self.box.pack_start(self.gatewayEntry, True, True, 0)
    self.box.pack_start(self.button1, True, True, 0)

    #catch window destroy event and stop it from happening
    self.connect('delete-event', self.on_destroy)

    def on_button1_clicked(self, widget):
    print("Hello")
    def on_destroy(self, widget=None, *data):
    print("tried to destroy")
    self.hide()
    return True

#list of ascii keypresses to test if a certain combination of keys is pressed
keypresses = []

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.set_keep_above(True)

def OnKeyboardEvent(event):
#ascii 227 is l_control, ascii 225 is l_shift, ascii 120 is x
#bring the following external variables into the scope of this function
    global keypresses
    global win
#check if gtk window is hidden or visible
    isVisible = win.get_property("visible")

    #store our keypress if it is worthy of being stored (we started with a control character)
    if event.Ascii == 227 or ( len(keypresses) >= 1 and keypresses[0] == 227 ):
            print("here" + str(len(keypresses)))
            keypresses.append(event.Ascii)
    #check if we have three items in keypresses to compare, then see if it's the right combination
    if len(keypresses) == 3 and keypresses[0] == 227 and keypresses[1] == 225 and keypresses[2] == 120:
        #show/hide our window
            if isVisible:
            win.hide()
            del keypresses[:]
            keypresses = []
        else:
            win.show_all()
            #clear out our list to compare again
            del keypresses[:]
            keypresses = []
    elif len(keypresses) >= 3:
            del keypresses[:]
            keypresses = []

#create instance of hook manager
HookManager = pyxhook.HookManager()
#define our callback to fire when a key is pressed
HookManager.KeyDown = OnKeyboardEvent
#hook the keyboard
HookManager.HookKeyboard()
#start our listener
HookManager.start()

Gtk.main()
#close the hook listener when gtk main loop exits
HookManager.cancel()

这是一个小技巧,可以在按 F5 时测试 window show/hide。当你按下它时,第二个 window 将显示或隐藏,我已经测试了一段时间没有问题。也许是 HookManager 以某种方式扰乱了 Gtk/Glib 主循环。尽管如此,我的方法只有在有 window 来抓取按键时才有效,所以如果你的目标是没有 GUI,你确实需要某种形式的抓取按键:

#!/usr/bin/python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
#import pyxhook

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Configurator")

        self.box = Gtk.Box(spacing=6)
        self.add(self.box)

        self.ipEntry = Gtk.Entry()
        self.ipEntry.set_text("IP Address")

        self.maskEntry = Gtk.Entry()
        self.maskEntry.set_text("NetMask")

        self.gatewayEntry = Gtk.Entry()
        self.gatewayEntry.set_text("gatewayEntry")

        self.button1 = Gtk.Button(label="Save")
        self.button1.connect("clicked", self.on_button1_clicked)
        self.box.pack_start(self.ipEntry, True, True, 0)
        self.box.pack_start(self.maskEntry, True, True, 0)
        self.box.pack_start(self.gatewayEntry, True, True, 0)
        self.box.pack_start(self.button1, True, True, 0)

        #catch window destroy event and stop it from happening
        self.connect('delete-event', self.on_destroy)
        self.connect('key-press-event', self.on_keypress)

    def on_keypress(self, widget, event):
        global w
        if event.keyval == Gdk.KEY_F5:
            isVisible = w.get_property("visible")
            if (isVisible):
                w.hide()
            else:
                w.show()
        print("Keypress")

    def on_button1_clicked(self, widget):
        print("Hello")

    def on_destroy(self, widget=None, *data):
        print("tried to destroy")
        self.hide()
        return False

w = MyWindow()

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.set_keep_above(True)


Gtk.main()