这是我整个的代码:
import sys
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
QApplication,
QHBoxLayout,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
v = QVBoxLayout()
h = QHBoxLayout()
for a in range(10):
button = QPushButton(str(a))
button.clicked.connect(lambda checked, a=a: self.button_clicked(a)) # error here
h.addWidget(button)
v.addLayout(h)
self.label = QLabel("")
v.addWidget(self.label)
w = QWidget()
w.setLayout(v)
self.setCentralWidget(w)
def button_clicked(self, n):
self.label.setText(str(n))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
当我运行这段代码时,会得到如下窗口:。按钮下面有一个QLabel标签,我想要点击任何一个按钮后,该按钮的标签将引用此QLabel标签,但在终端中却出现了一堆令人困惑的错误。我的代码哪里出了问题,请帮帮我,谢谢。