I am new to Python. My problem is the "click" function that shows/opens the numpad - "lambda: self.show_NumPad(i)" in the loop logic does not give the right index number for "i".
This program has three QLineEdit boxes on the main window that when clicked open a NumPad window to allow user the enter a number with a mouse. Hitting the "Enter" button closes the NumPad window and puts the entered number into the correct (the one clicked) QLineEdit box. The reason I want to use a loop/array of widgets is because I am going to have 160 edit box in the final solution to enter numbers with the mouse/NumPad.
The commented out ## straight line code works and the click sends the 0, 1 or 2 into parm1 then the QLineEdit box is clicked. But when I try to do this in a loop, the clicked only passes 2 (the max number of boxes) into parm1, and I can't figure out why or a solution. I want the loop code to work like the straight line code.
Any help would be greatly appreciated, thanks.
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_())