AttributeError: 'Ui_Dialog' object has no attribute 'textEdit_2'
AttributeError: 'Ui_Dialog' object has no attribute 'textEdit_2'
我在设置继承时遇到问题。我想在单击 pushButton_3
时激活 generator
功能,但我一直在标题中收到错误消息。我的完整代码:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow
import pyqrcode
import png
from pyqrcode import QRCode
class MainWindow(QMainWindow):
def setupUi(self):
self.setObjectName("QR Generator")
self.setFixedSize(591, 381)
self.pushButton = QtWidgets.QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(340, 320, 241, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self)
self.pushButton_2.setGeometry(QtCore.QRect(340, 260, 241, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self)
self.pushButton_3.setGeometry(QtCore.QRect(10, 200, 321, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.generate())
self.pushButton_4 = QtWidgets.QPushButton(self)
self.pushButton_4.setGeometry(QtCore.QRect(10, 260, 321, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.graphicsView = QtWidgets.QGraphicsView(self)
self.graphicsView.setGeometry(QtCore.QRect(340, 10, 241, 241))
self.graphicsView.setObjectName("graphicsView")
self.textEdit = QtWidgets.QTextEdit(self)
self.textEdit.setGeometry(QtCore.QRect(10, 150, 321, 41))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setBold(True)
font.setWeight(75)
self.textEdit.setFont(font)
self.textEdit.setObjectName("textEdit")
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(40, 20, 261, 61))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.textEdit_2 = QtWidgets.QTextEdit(self)
self.textEdit_2.setGeometry(QtCore.QRect(10, 100, 321, 41))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setBold(True)
font.setWeight(75)
self.textEdit_2.setFont(font)
self.textEdit_2.setObjectName("textEdit_2")
self.retranslateUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("Dialog", "QR Code Generator"))
self.pushButton.setText(_translate("Dialog", "Exit"))
self.pushButton_2.setText(_translate("Dialog", "Save As Picture"))
self.pushButton_3.setText(_translate("Dialog", "Generate QR Code"))
self.pushButton_4.setText(_translate("Dialog", "Clear"))
self.textEdit.setPlaceholderText(_translate("Dialog", "Paste link here"))
self.label.setText(_translate("Dialog", "QR Code Generator"))
self.textEdit_2.setPlaceholderText(_translate("Dialog", "Give a name to the QR"))
def save1(self):
pass
def generate(self):
link = self.textEdit_2.text()
qrname = self.textEdit.text()
qrcode = pyqrcode.create(link)
qrcode.png(f"{qrname}.png", scale = 6)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
self = QtWidgets.QDialog()
ui = MainWindow()
ui.setupUi()
self.show()
sys.exit(app.exec_())
self.pushButton_3.clicked.connect(self.generate())
在此行中,您没有连接 generate
函数,而是通过将 ()
添加到函数名称来调用它,因此将其更改为 self.pushButton_3.clicked.connect(self.generate)
和self.generate
应该接受一个参数 x
是 pushbutton
的回调事件
所以要么改变接受一个参数的 self.generate
的定义
或者制作一个 lambda 函数,它接受一个参数并通过放置此行 self.pushButton_3.clicked.connect(lambda x: self.generate())
来调用 self.generate
我在设置继承时遇到问题。我想在单击 pushButton_3
时激活 generator
功能,但我一直在标题中收到错误消息。我的完整代码:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow
import pyqrcode
import png
from pyqrcode import QRCode
class MainWindow(QMainWindow):
def setupUi(self):
self.setObjectName("QR Generator")
self.setFixedSize(591, 381)
self.pushButton = QtWidgets.QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(340, 320, 241, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self)
self.pushButton_2.setGeometry(QtCore.QRect(340, 260, 241, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self)
self.pushButton_3.setGeometry(QtCore.QRect(10, 200, 321, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.generate())
self.pushButton_4 = QtWidgets.QPushButton(self)
self.pushButton_4.setGeometry(QtCore.QRect(10, 260, 321, 51))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.graphicsView = QtWidgets.QGraphicsView(self)
self.graphicsView.setGeometry(QtCore.QRect(340, 10, 241, 241))
self.graphicsView.setObjectName("graphicsView")
self.textEdit = QtWidgets.QTextEdit(self)
self.textEdit.setGeometry(QtCore.QRect(10, 150, 321, 41))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setBold(True)
font.setWeight(75)
self.textEdit.setFont(font)
self.textEdit.setObjectName("textEdit")
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(40, 20, 261, 61))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.textEdit_2 = QtWidgets.QTextEdit(self)
self.textEdit_2.setGeometry(QtCore.QRect(10, 100, 321, 41))
font = QtGui.QFont()
font.setFamily("Bahnschrift SemiBold")
font.setBold(True)
font.setWeight(75)
self.textEdit_2.setFont(font)
self.textEdit_2.setObjectName("textEdit_2")
self.retranslateUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("Dialog", "QR Code Generator"))
self.pushButton.setText(_translate("Dialog", "Exit"))
self.pushButton_2.setText(_translate("Dialog", "Save As Picture"))
self.pushButton_3.setText(_translate("Dialog", "Generate QR Code"))
self.pushButton_4.setText(_translate("Dialog", "Clear"))
self.textEdit.setPlaceholderText(_translate("Dialog", "Paste link here"))
self.label.setText(_translate("Dialog", "QR Code Generator"))
self.textEdit_2.setPlaceholderText(_translate("Dialog", "Give a name to the QR"))
def save1(self):
pass
def generate(self):
link = self.textEdit_2.text()
qrname = self.textEdit.text()
qrcode = pyqrcode.create(link)
qrcode.png(f"{qrname}.png", scale = 6)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
self = QtWidgets.QDialog()
ui = MainWindow()
ui.setupUi()
self.show()
sys.exit(app.exec_())
self.pushButton_3.clicked.connect(self.generate())
在此行中,您没有连接 generate
函数,而是通过将 ()
添加到函数名称来调用它,因此将其更改为 self.pushButton_3.clicked.connect(self.generate)
和self.generate
应该接受一个参数 x
是 pushbutton
所以要么改变接受一个参数的 self.generate
的定义
或者制作一个 lambda 函数,它接受一个参数并通过放置此行 self.pushButton_3.clicked.connect(lambda x: self.generate())
self.generate