Как обратиться к конкретной кнопке, созданной в цикле, PyQt6
Пишу калькулятор на PyQt6, создал клаву в цикле из словаря, но уже голову сломал, как обратиться к конкретной кнопке, чтобы ее значение вывести на табло.
Пытался присвоить имя атрибуту и передать в сигнале, однако передает исключительно последнее значение.
Как в событии нажатия конкретной кнопки обратиться к ее атрибуту?
from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QLCDNumber, QGridLayout
import sys
class Buttn(QPushButton):
def __init__(self, name):
super().__init__()
self.name = name
self.setText(name)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.setFixedSize(QSize(265, 370))
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
names = ["C", "DEL", "", "*",
"1", "2", "3", "/",
"4", "5", "6", "+",
"7", "8", "9", "-",
"+/-", "0", ".", "="]
positions = [(i, j) for i in range(2, 7) for j in range(4)]
self.LCD = QLCDNumber()
self.formula = "0"
self.LCD.display(self.formula)
self.LCD.setFixedHeight(40)
self.LCD.setDigitCount(12)
self.LCD.setDecMode()
grid.addWidget(self.LCD, 0, 0, 1, 4)
self.buttons = []
for position, name in zip(positions, names):
if name == '':
continue
button = Buttn(name)
button.setFixedSize(QSize(60, 60))
button.clicked.connect(lambda: self.the_button_was_clicked(name))
self.buttons.append(button)
grid.addWidget(button, *position)
print(name)
self.move(1200, 550)
self.setWindowTitle('Calculator')
self.show()
def the_button_was_clicked(self, value):
print("clicked", value)
self.LCD.display(value)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
import sys
'''
from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, \
QLCDNumber, QGridLayout
'''
from PyQt5.Qt import *
class Buttn(QPushButton):
def __init__(self, name):
super().__init__()
self.name = name
self.setText(name)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.setFixedSize(QSize(265, 370))
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
names = ["C", "DEL", "", "*",
"1", "2", "3", "/",
"4", "5", "6", "+",
"7", "8", "9", "-",
"+/-", "0", ".", "="]
positions = [(i, j) for i in range(2, 7) for j in range(4)]
self.LCD = QLCDNumber()
self.formula = "0"
self.LCD.display(self.formula)
self.LCD.setFixedHeight(40)
self.LCD.setDigitCount(12)
self.LCD.setDecMode()
grid.addWidget(self.LCD, 0, 0, 1, 4)
self.buttons = []
for position, name in zip(positions, names):
if name == '':
continue
button = Buttn(name)
button.setFixedSize(QSize(60, 60))
# button.clicked.connect(lambda : self.the_button_was_clicked(name))
button.clicked.connect(
# -------------------> vvvvvvvvvvvvv <---------------------------------------- # !!! +++
lambda ch, name=name: self.the_button_was_clicked(name))
self.buttons.append(button)
grid.addWidget(button, *position)
# print(name)
# ??? self.move(1200, 550)
# ??? self.show()
def the_button_was_clicked(self, value):
print("clicked", value)
self.LCD.display(value)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.setWindowTitle('Calculator')
window.show()
app.exec()
