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

2024-10-17 08:30:53 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有以下代码(python2.7),它检查按键的组合,并显示/隐藏一个带有几个gtk输入框的gtk窗口。“显示/隐藏”会一直工作,直到窗口锁定并停止响应,并且窗口内部变为黑色。我必须停止进程,然后在窗口变黑后重新启动它。我尝试了show_all()的所有不同组合,并在隐藏窗口无效后返回True。我不知道我做错了什么。enter image description here

最后,我希望能按这个组合键,显示/隐藏这个窗口,而不让内容消失。

#!/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()

Tags: andoftoselfboxeventtruegtk
1条回答
网友
1楼 · 发布于 2024-10-17 08:30:53

这里有一个小的黑客测试窗口显示/隐藏时按F5。当你按下它,第二个窗口就会显示或隐藏,我已经测试了一段时间没有问题。也许是HookManager在某种程度上搞乱了Gtk/Glib主循环。尽管如此,我的方法只有在有一个窗口来抓取按键时才有效,因此,如果您的目标是没有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() 

相关问题 更多 >