Home > Enterprise >  AttributeError: 'Ui_Dialog' object has no attribute 'textEdit_2'
AttributeError: 'Ui_Dialog' object has no attribute 'textEdit_2'

Time:03-05

I am having trouble setting the inheritance. I want to activate the generator function when the pushButton_3 is clicked but I keep getting the error in the title. My full code:

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_())

CodePudding user response:

self.pushButton_3.clicked.connect(self.generate()) at this line you are not connecting the generate function but you are calling it by adding () to function name, so change it to self.pushButton_3.clicked.connect(self.generate) and self.generate should accept one argument x which is callback event of pushbutton

so either change definition of self.generate which accepts one argument or make a lambda function which accepts one argument and calls self.generate by placing this line self.pushButton_3.clicked.connect(lambda x: self.generate())

  • Related