我不能 clicked.connect() QFormLayout 上的 QPushButton [重复]
Posted
技术标签:
【中文标题】我不能 clicked.connect() QFormLayout 上的 QPushButton [重复]【英文标题】:i can't clicked.connect() QPushButton on QFormLayout [duplicate] 【发布时间】:2020-12-22 05:02:05 【问题描述】:这是完整的代码,我不知道为什么我不能使用 clicked.connect 在我的脑海中,这仍然是逻辑'-'但是为什么!!!???
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())
我在这里出错了,我想让每个项目都可以点击,但我不知道为什么会这样:“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)
此代码不起作用,
comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))
【问题讨论】:
信号需要一个 callable 作为参数。print()
返回None
,这显然不是可调用的。标记为 [***.com/q/16696684] 的重复项另外,您不是将按钮附加到comboList
,而是将connect()
的结果附加到None
。
【参考方案1】:
不知道为什么会出现comboList.append(QPushButton("Click Me").clicked.connect(self.hello)
错误
但我会做的
qbutton = QPushButton("Click Me")
qbutton.clicked.connect(self.hello)
结果
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())
【讨论】:
区别很明显:print('hello')
返回None
,而self.hello
(不带括号)是对可调用对象的引用。以上是关于我不能 clicked.connect() QFormLayout 上的 QPushButton [重复]的主要内容,如果未能解决你的问题,请参考以下文章
PyQt4 - clicked.connect 记住以前的连接?