简体   繁体   English

尝试将 4x4 键盘与 pyqt5 gui 应用程序一起使用

[英]trying to use 4x4 keypad with pyqt5 gui application

i am trying to write code which take input from 4x4 keypad(Mechanical) using pad4pi module with pyqt5 based GUI application.我正在尝试使用带有基于 pyqt5 的 GUI 应用程序的 pad4pi 模块编写从 4x4 键盘(机械)输入的代码。

when i try to click button it work properly but when i try to genrate some event i get error message:当我尝试单击按钮时,它可以正常工作,但是当我尝试生成某些事件时,我收到错误消息:

QObject::startTimer: Timers can only be used with threads started with QThread
class DigitalClock(QWidget,QThread):
    def __init__(self):
        super().__init__()
        SetupKeyboard.keypad.registerKeyPressHandler(self.printKey)
        self.setWindowTitle("OM SAI RAM")
        self.showFullScreen() 
        #self.setCursor(Qt.BlankCursor)
        button = QPushButton("Click", self) 
        button.clicked.connect(self.change)
        button.move(10,10)    
        button.show()

    def change(self):
        self.newpage = Authentication_page()
        self.close()

    def printKey(self, key):
        if key == 'A':
            self.newpage = Authentication_page()
            self.close()

class Authentication_page(QWidget):
    """
    Class to validate authentication.
    """
    def __init__(self):
        super().__init__()
        self.showFullScreen() 
        self.maindesign()

    def maindesign(self):
        """Method to design main page"""
        ####Label###
        self.admin_header = QLabel("Admin Panel", self)
        self.admin_header.setStyleSheet("font-size:40px")
        self.admin_header.move(130, 10)
        self.admin_header.show() 

when i click button code work fine but when i push mechanical button, code freeze with error message.当我单击按钮代码工作正常但当我按下机械按钮时,代码冻结并显示错误消息。

The handler assigned by registerKeyPressHandler is executed in the thread where the keys are monitored, in your case printKey is executed in a secondary thread where you try to create a widget but that is forbidden by Qt. registerKeyPressHandler 分配的处理程序在监视键的线程中执行,在您的情况下,printKey 在您尝试创建小部件的辅助线程中执行,但 Qt 禁止这样做。

The solution is to create a QObject and emit a signal (since the signals are thread-safe) by sending the key pressed, then connect to a slot where you receive the information:解决方案是创建一个 QObject 并通过发送按下的键来发出信号(因为信号是线程安全的),然后连接到接收信息的插槽:

from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject
from PyQt5.QtWidgets import QLabel, QPushButton, QWidget

from pad4pi import rpi_gpio


class KeypadManager(QObject):
    keyPressed = pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)
        factory = rpi_gpio.KeypadFactory()
        self._keypad = factory.create_4_by_4_keypad()
        self._keypad.registerKeyPressHandler(self._key_press_handler)

    def _key_press_handler(self, key):
        self.keyPressed.emit(key)


class DigitalClock(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("OM SAI RAM")
        self._keypad_manager = KeypadManager()
        self._keypad_manager.keyPressed.connect(self.printKey)

        # self.setCursor(Qt.BlankCursor)
        button = QPushButton("Click", self)
        button.clicked.connect(self.show_authentication_page)
        button.move(10, 10)

        self.showFullScreen()

    def show_authentication_page(self):
        self.newpage = Authentication_page()
        self.close()

    @pyqtSlot(object)
    def printKey(self, key):
        if key == "A":
            self.show_authentication_page()


class Authentication_page(QWidget):
    # ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM