我正在尝试在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_()
我收到错误消息:TypeError:addInputByKey()缺少1个必需的位置参数:'key'
有什么帮助吗?
最佳答案
解决方案是更改此:
self.getButtonByKey(key).sigKeyButtonClicked.connect(self.addInputByKey)
对此:
self.getButtonByKey(key).sigKeyButtonClicked.connect(lambda v=key: self.addInputByKey(v))
关于python - PyQt5虚拟键盘(TypeError:缺少1个必需的位置参数),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42647295/