如果我使用pynput global hotkeys,kivy应用程序无法正常工作

发布于 2025-01-19 15:55:17 字数 953 浏览 3 评论 0原文

我正在尝试创建一个默认情况下隐藏的应用程序,按下组合键时会显示窗口。

我的代码:

from kivy.app import App
from kivy.uix.label import Label
from pynput import keyboard


class MyApp(App):
    def open_window(self):
        print(f"Key pressed...")
        self.root_window.show()

    def build(self):
        return Label(text="Hello")

    def on_start(self):
        self.root_window.hide()

        with keyboard.GlobalHotKeys({"<ctrl>+<alt>+h": self.open_window}) as h:
            h.join()


if __name__ == "__main__":
    MyApp().run()

当我运行应用程序时,一切看起来都很好,但没有显示标签,并且我无法通过单击关闭按钮来关闭应用程序。多次尝试关闭窗口后,我收到 python 未响应错误。

谢谢。

输入图片此处描述输入图片此处描述

I am trying to create an application which is hidden by default and windows is shows when key combination is pressed.

My code:

from kivy.app import App
from kivy.uix.label import Label
from pynput import keyboard


class MyApp(App):
    def open_window(self):
        print(f"Key pressed...")
        self.root_window.show()

    def build(self):
        return Label(text="Hello")

    def on_start(self):
        self.root_window.hide()

        with keyboard.GlobalHotKeys({"<ctrl>+<alt>+h": self.open_window}) as h:
            h.join()


if __name__ == "__main__":
    MyApp().run()

When I run the app everything looks fine but no label is shown and I am unable to close the app in single click of close button. After multiple attempts of closing the window I get python not responding error.

Thanks.

enter image description here
enter image description here

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

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

发布评论

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

评论(1

他不在意 2025-01-26 15:55:17

使用这个

from kivy.core.window import Window, Keyboard
from kivy.logger import Logger

class MyApp(App):
def build(self):
    Window.bind(on_key_down=self._keydown)
    Window.bind(on_key_up=self._keyup)
    return Autoclicker()

def _keydown(self, window, key, scancode, codepoint, modifiers):
    print('keydown:')
    print('\tkey:',key)
    print('\tscancode:', scancode)
    print('\tcodepoint:', codepoint)
    print('\tmodifiers:', modifiers)
def _keyup(self, window, key, scancode):
    print('keyup:')
    print('\tkey:',key)
    print('\tscancode:', scancode)
    # call anything
    if key == 287:
        print("yessssssssssssssssssssssssssssssssssssssss")

use this

from kivy.core.window import Window, Keyboard
from kivy.logger import Logger

or

class MyApp(App):
def build(self):
    Window.bind(on_key_down=self._keydown)
    Window.bind(on_key_up=self._keyup)
    return Autoclicker()

def _keydown(self, window, key, scancode, codepoint, modifiers):
    print('keydown:')
    print('\tkey:',key)
    print('\tscancode:', scancode)
    print('\tcodepoint:', codepoint)
    print('\tmodifiers:', modifiers)
def _keyup(self, window, key, scancode):
    print('keyup:')
    print('\tkey:',key)
    print('\tscancode:', scancode)
    # call anything
    if key == 287:
        print("yessssssssssssssssssssssssssssssssssssssss")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文