我是Python的新手。我的问题是显示/打开数字键盘的“单击”功能-循环逻辑中的“ lambda:self.show_NumPad(i)”没有为“ i”提供正确的索引号。
该程序在主窗口上具有三个QLineEdit框,单击这些框可打开NumPad窗口,以允许用户使用鼠标输入数字。点击“ Enter”按钮将关闭NumPad窗口,并将输入的数字放入正确的QLineEdit框中。我想使用小部件的循环/数组的原因是,在最终解决方案中,我将具有160个编辑框,以便使用鼠标/数字键盘输入数字。
注释掉的##直线代码有效,单击将0、1或2发送到parm1中,然后单击QLineEdit框。但是,当我尝试循环执行此操作时,单击仅将2(最大盒子数)传递到parm1中,我不知道原因或解决方案。我希望循环代码像直线代码一样工作。
任何帮助将不胜感激,谢谢。
import sys
from pprint import pprint
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import pyqtSignal
class extQLineEdit(QLineEdit):
clicked=pyqtSignal()
def __init__(self,parent):
QLineEdit.__init__(self,parent)
def mousePressEvent(self,QMouseEvent):
self.clicked.emit()
class MainWidget(QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.setFixedSize(400,300)
self.move(600,400)
self.numpad = NumPad(self)
self.pick = QLineEdit(self)
self.pick.move(250,0)
# Loop to create 3 QLineEdit's
self.lVal = {}
for i in range(3):
self.lVal[(i)] = extQLineEdit(self)
self.lVal[(i)].move(20,((i*50)+20))
self.lVal[(i)].clicked.connect(lambda: self.show_NumPad(i))
# Straight line code to create 3 QLineEdit's
## self.lVal0 = extQLineEdit(self)
## self.lVal0.move(20,20)
## self.lVal0.clicked.connect(lambda: self.show_NumPad(0))
##
## self.lVal1 = extQLineEdit(self)
## self.lVal1.move(20,70)
## self.lVal1.clicked.connect(lambda: self.show_NumPad(1))
##
## self.lVal2 = extQLineEdit(self)
## self.lVal2.move(20,120)
## self.lVal2.clicked.connect(lambda: self.show_NumPad(2))
def show_NumPad(self, parm1):
pprint(str(parm1))
self.pick.setText(str(parm1))
self.numpad.move(700,500)
self.numpad.show()
def close(self):
self.numpad.close()
super(MainWidget, self).close()
class NumPad(QWidget):
def __init__(self, parm1):
super(NumPad, self).__init__()
grid_layout = QGridLayout()
self.setLayout(grid_layout)
values = [
'1', '2', '3',
'4', '5', '6',
'7', '8', '9',
'-', '0', 'Enter'
]
positions = [(i, j) for i in range(1,5) for j in range(3)]
# position is an array of tuples
#pprint("positions = "+str(positions))
for position, value in zip(positions, values):
#print("position = " + str(position))
#print("value = " + str(value))
if value == '':
continue
button = QPushButton(value)
grid_layout.addWidget(button, *position)
button.clicked.connect(self.btnclick)
self.setWindowTitle('Num Pad')
verticalLayout = QVBoxLayout()
self.lineEdit = QLineEdit()
verticalLayout.addWidget(self.lineEdit)
grid_layout.addLayout(verticalLayout, 0, 0, 1, 3)
def btnclick(self):
sender = self.sender()
#pprint("You Pressed: " + sender.text())
if sender.text() in ['0','1','2','3','4','5','6','7','8','9']:
self.lineEdit.setText(self.lineEdit.text() + sender.text())
if sender.text() in ['-']:
if self.lineEdit.text()[:1] in ['-']:
self.lineEdit.setText(self.lineEdit.text()[-
(len(self.lineEdit.text())-1):])
else:
self.lineEdit.setText(sender.text() + self.lineEdit.text())
if sender.text() in ["Enter"]:
# For loop 3 QLineEdit's
if mw.pick.text() in ["0"]:
mw.lVal[(0)].setText(self.lineEdit.text())
if mw.pick.text() in ["1"]:
mw.lVal[(1)].setText(self.lineEdit.text())
if mw.pick.text() in ["2"]:
mw.lVal[(2)].setText(self.lineEdit.text())
# For straight line code 3 QLineEdit's
## if mw.pick.text() in ["0"]:
## mw.lVal0.setText(self.lineEdit.text())
## if mw.pick.text() in ["1"]:
## mw.lVal1.setText(self.lineEdit.text())
## if mw.pick.text() in ["2"]:
## mw.lVal2.setText(self.lineEdit.text())
self.lineEdit.clear()
mw.numpad.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWidget()
mw.show()
sys.exit(app.exec_())