gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:30:13 25 4
gpt4 key购买 nike

所以我有以下代码(python 2.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()

最佳答案

这是一个测试按 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()

关于python - 如何在python中正确显示和隐藏GTK窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com