Clicked.connect () QFormLayout üzerinde QPushButton [duplicate] yapamıyorum

Dec 22 2020

bu tam kod, aklımdaki tıklanan bağlantıyı neden kullanamadığımı bilmiyorum, bu hala mantık '-' ama neden !!! ???

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QScrollArea, QVBoxLayout, QGroupBox, QLabel, QPushButton, QFormLayout
import sys


class Window(QWidget):
    def __init__(self, val):
        super().__init__()
        self.title = "PyQt5 Scroll Bar"
        self.top = 200
        self.left = 500
        self.width = 400
        self.height = 300
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

    formLayout = QFormLayout()
    groupBox = QGroupBox("This Is Group Box")
    labelLis = []
    comboList = []
    for i in range(val):
        labelLis.append(QLabel("Label"))
        comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))
        formLayout.addRow(labelLis[i], comboList[i])
    groupBox.setLayout(formLayout)

    scroll = QScrollArea()
    scroll.setWidget(groupBox)
    scroll.setWidgetResizable(True)
    scroll.setFixedHeight(400)
    layout = QVBoxLayout(self)
    layout.addWidget(scroll)
    self.show()


App = QApplication(sys.argv)
window = Window(30)
sys.exit(App.exec())

ve burada hata aldım, her öğenin tıklanabilmesini istiyorum, ancak bunun neden olduğunu bilmiyorum: "D

for i in range(val):
        labelLis.append(QLabel("Label"))
        comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))
        formLayout.addRow(labelLis[i], comboList[i])
    groupBox.setLayout(formLayout)

bu kod çalışmıyor, comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))

Yanıtlar

dudulu Dec 22 2020 at 12:48

Neden comboList.append(QPushButton("Click Me").clicked.connect(self.hello)hata oluştuğunu bilmiyorum

Ama yapacağım

qbutton = QPushButton("Click Me")
qbutton.clicked.connect(self.hello)

sonuç

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QScrollArea, QVBoxLayout, QGroupBox, QLabel, QPushButton, QFormLayout
import sys


class Window(QWidget):
    def __init__(self, val):
        super().__init__()
        self.title = "PyQt5 Scroll Bar"
        self.top = 200
        self.left = 500
        self.width = 400
        self.height = 300
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        formLayout = QFormLayout()
        groupBox = QGroupBox("This Is Group Box")
        labelLis = []
        comboList = []

        for i in range(val):
            qbutton = QPushButton("Click Me")
            qbutton.clicked.connect(self.hello)
            labelLis.append(QLabel("Label"))
            comboList.append(qbutton)
            formLayout.addRow(labelLis[i], comboList[i])

        groupBox.setLayout(formLayout)

        scroll = QScrollArea()
        scroll.setWidget(groupBox)
        scroll.setWidgetResizable(True)
        scroll.setFixedHeight(400)
        layout = QVBoxLayout(self)
        layout.addWidget(scroll)
        self.show()

    def hello(self):
        print('hello')


App = QApplication(sys.argv)
window = Window(30)
sys.exit(App.exec())