本文介绍了如何在PyQt5中使用全局键盘快捷键显示QMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 PyQt5 通过热键(例如"F1")显示QMenu实例,然后我发现了此包键盘.

I was trying to show a QMenu instance by an hotkey(e.g. "F1") by PyQt5, then I found this package keyboard.

尝试像这样使用它: keyboard.add_hotkey('F1',self.show_menu,prevent = True)

然后我得到了这些代码:

Then I got these code:

import sys

import keyboard

from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__(flags=Qt.WindowStaysOnTopHint)

        self.menu = QMenu('Menu')
        self.menu.addAction(QAction('menu1', self.menu))
        self.menu.addAction(QAction('menu2', self.menu))
        self.menu.addAction(QAction('menu3', self.menu))

        self.show_menu()  # this works well

        keyboard.add_hotkey('F1', self.show_menu, suppress=True)  # this hotkey works but not showing the menu

    def show_menu(self):
        print('111')
        self.menu.popup(QCursor().pos())
        print('222')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)
    win = MainWindow()
    # win.show()
    sys.exit(app.exec_())

实际上,在 __ init __ 中对方法 self.show_menu 的调用工作良好,可以按预期弹出菜单.

Actually, the calling to method self.show_menu in __init__ works well, a menu can popup as expected.

但是问题是,当我按下热键"F1"时,将打印"111"和"222",但菜单不会出现.

But the QUESTION is that, when I press the hotkey "F1", "111" and "222" would be printed, but the menu, won't appear.

有什么问题吗,或者我可以通过其他方式做到这一点?请告诉我,谢谢.

Is there anything wrong, or I can make it in some other ways? Please tell me, thank you.

推荐答案

与add_hotkey关联的回调在辅助线程中执行,并且在OP代码中,该回调是show_menu方法,该方法修改了Qt禁止的GUI.解决方案是使用信号:

The callback associated with add_hotkey is executed in a secondary thread, and in the OP code the callback is the show_menu method that modifies the GUI which is prohibited by Qt. The solution is to use signals:

import sys

import keyboard

from PyQt5.QtCore import Qt, QObject, pyqtSignal
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow, QMenu


class KeyBoardManager(QObject):
    F1Signal = pyqtSignal()

    def start(self):
        keyboard.add_hotkey("F1", self.F1Signal.emit, suppress=True)


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__(flags=Qt.WindowStaysOnTopHint)

        self.menu = QMenu("Menu")
        self.menu.addAction(QAction("menu1", self.menu))
        self.menu.addAction(QAction("menu2", self.menu))
        self.menu.addAction(QAction("menu3", self.menu))

        manager = KeyBoardManager(self)
        manager.F1Signal.connect(self.show_menu)
        manager.start()

    def show_menu(self):
        print("111")
        self.menu.popup(QCursor.pos())
        print("222")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)
    win = MainWindow()
    # win.show()
    sys.exit(app.exec_())

这篇关于如何在PyQt5中使用全局键盘快捷键显示QMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:54