PyQt5 虚拟键盘(TypeError:缺少 1 个必需的位置参数)

Posted

技术标签:

【中文标题】PyQt5 虚拟键盘(TypeError:缺少 1 个必需的位置参数)【英文标题】:PyQt5 virtual keyboard (TypeError: missing 1 required positional argument) 【发布时间】:2017-03-07 11:36:04 【问题描述】:

我正在尝试在 PyQt5 中为虚拟键盘编写代码。这是我的代码。

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class InputState:
    LOWER = 0
    CAPITAL = 1


class KeyButton(QPushButton):
    sigKeyButtonClicked = pyqtSignal()

    def __init__(self, key):
        super(KeyButton, self).__init__()

        self._key = key
        self._activeSize = QSize(50,50)
        self.clicked.connect(self.emitKey)
        self.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed))


    def emitKey(self):
        self.sigKeyButtonClicked.emit()

    def enterEvent(self, event):
        self.setFixedSize(self._activeSize)

    def leaveEvent(self, event):
        self.setFixedSize(self.sizeHint())

    def sizeHint(self):
        return QSize(40, 40)

class VirtualKeyboard(QWidget):
    sigInputString = pyqtSignal()
    sigKeyButtonClicked = pyqtSignal()

    def __init__(self):
        super(VirtualKeyboard, self).__init__()

        self.globalLayout = QVBoxLayout(self)
        self.keysLayout = QGridLayout()
        self.buttonLayout = QHBoxLayout()

        self.keyListByLines = [
                    ['a', 'z', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
                    ['q', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm'],
                    ['w', 'x', 'c', 'v', 'b', 'n', '_', '.', '/', ' '],
                ]
        self.inputString = ""
        self.state = InputState.LOWER

        self.stateButton = QPushButton()
        self.stateButton.setText('Maj.')
        self.backButton = QPushButton()
        self.backButton.setText('<-')
        self.okButton = QPushButton()
        self.okButton.setText('OK')
        self.cancelButton = QPushButton()
        self.cancelButton.setText("Cancel")

        self.inputLine = QLineEdit()


        for lineIndex, line in enumerate(self.keyListByLines):
            for keyIndex, key in enumerate(line):
                buttonName = "keyButton" + key.capitalize()
                self.__setattr__(buttonName, KeyButton(key))
                self.keysLayout.addWidget(self.getButtonByKey(key), self.keyListByLines.index(line), line.index(key))
                self.getButtonByKey(key).setText(key)
                self.getButtonByKey(key).sigKeyButtonClicked.connect(self.addInputByKey)
                self.keysLayout.setColumnMinimumWidth(keyIndex, 50)
            self.keysLayout.setRowMinimumHeight(lineIndex, 50)

        self.stateButton.clicked.connect(self.switchState)
        self.backButton.clicked.connect(self.backspace)
        self.okButton.clicked.connect(self.emitInputString) 
        self.cancelButton.clicked.connect(self.emitCancel)


        self.buttonLayout.addWidget(self.cancelButton)
        self.buttonLayout.addWidget(self.backButton)
        self.buttonLayout.addWidget(self.stateButton)
        self.buttonLayout.addWidget(self.okButton)

        self.globalLayout.addWidget(self.inputLine)
        self.globalLayout.addLayout(self.keysLayout)

        self.globalLayout.addLayout(self.buttonLayout)
        self.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed))


    def getButtonByKey(self, key):
        return getattr(self, "keyButton" + key.capitalize())

    def getLineForButtonByKey(self, key):
        return [key in keyList for keyList in self.keyListByLines].index(True)

    def switchState(self):
        self.state = not self.state

    def addInputByKey(self, key):
        self.inputString += (key.lower(), key.capitalize())[self.state]
        self.inputLine.setText(self.inputString)

    def backspace(self):
        self.inputLine.backspace()
        self.inputString = self.inputString[:-1]

    def emitInputString(self):
        self.sigInputString.emit(self.inputString)

    def emitCancel(self):
        self.sigInputString.emit()

    def sizeHint(self):
        return QSize(480,272)


class Test(QWidget):
    def __init__(self):
        super(Test, self).__init__()

        self.b1 = KeyButton("1")
        self.b2 = KeyButton("2")
        self.b3 = KeyButton("3")
        self.b4 = KeyButton("4")

        self.layout = QGridLayout(self)
        self.layout.addWidget(self.b1,0,0)
        self.layout.addWidget(self.b2,0,1)
        self.layout.addWidget(self.b3,1,0)
        self.layout.addWidget(self.b4,1,1)

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    win = VirtualKeyboard()
    win.show()
    app.exec_()

我收到 ERROR: TypeError: addInputByKey() missing 1 required positional argument: 'key'

有什么帮助吗?

【问题讨论】:

请向我们展示堆栈跟踪,而不仅仅是异常。 @Alfe 我只得到这个错误,但是当我运行代码时,我得到了键盘,但是在单击某个键后,程序崩溃并显示该错误 【参考方案1】:

解决办法是改变这个:

self.getButtonByKey(key).sigKeyButtonClicked.connect(self.addInputByKey)

到这里:

self.getButtonByKey(key).sigKeyButtonClicked.connect(lambda v=key: self.addInputByKey(v))

【讨论】:

【参考方案2】:

你没有传递值?

from functools import partial
...

self.getButtonByKey(key).sigKeyButtonClicked.connect(partial(self.addInputByKey, key))

【讨论】:

以上是关于PyQt5 虚拟键盘(TypeError:缺少 1 个必需的位置参数)的主要内容,如果未能解决你的问题,请参考以下文章

用于 openPersitentEditor 的 PyQt5 TypeError

Qt Designer 和 PyQt5 自定义插件加载 TypeError

如何修复 QtWidgets.QMessageBox 中的“TypeError ......”以在 PyQT5 中弹出消息

PyQt5 TabWidget tabBarClicked TypeError: native Qt signal is not callable

TypeError:缺少 1 个必需的位置参数:'self'

TypeError:AuthMiddlewareStack() 缺少 1 个必需的位置参数:'inner'