Search code examples
pythonpyqt5pyuic

How do i action a button in PyQt5?


This this the code:

from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow, QMessageBox
import Editor
import sys

class MyApp(QMainWindow):
    def __init__(self, parent = None):
        QWidget.__init__(self, parent)
        self.ui = Editor.Ui_Form()
        self.ui.setupUi(self)
        super(MyApp, self).__init__()

        self.pushButton.clicked.connect(self.infoGet)

    def infoGet(self, any, mode):
        vFN = self.lineEdit.text()
        vMN = self.lineEdit_2.text()
        vLN = self.lineEdit_3.text()
        vID = self.lineEdit_4.text()
        print(vFN)

Hope you guys figure it out!!

So i use self.pushButton.clicked.connect(self.infoGet) as a action code. pushButton is my button object. And I get... File "d:\**--CENSORED--**\edui.py", line 25, in <module> mapp = MyApp() File "d:\folder visead\RAIN FORREST\Data Base\Citizen record\edui.py", line 12, in __init__ self.pushButton.clicked.connect(self.infoGet) #!TES{PIGOFSDOPPOIHBSJ} AttributeError: 'MyApp' object has no attribute 'pushButton'

I don't know why I got attibute error btw.


Solution

  • It seems that your pushButton is inside self.ui = Editor.Ui_Form(). Therefore, to access pushButton you have to use self.ui instead of self. Following code might be the solution to your problem:

    class MyApp(QMainWindow):
        def __init__(self, parent = None):
            QWidget.__init__(self, parent)
            self.ui = Editor.Ui_Form()
            self.ui.setupUi(self)
            super(MyApp, self).__init__()
    
            self.ui.pushButton.clicked.connect(self.infoGet) # Update this
    
        def infoGet(self, any, mode):
            vFN = self.ui.lineEdit.text() # Update this
            vMN = self.ui.lineEdit_2.text() # Update this
            vLN = self.ui.lineEdit_3.text() # Update this
            vID = self.ui.lineEdit_4.text() # Update this
            print(vFN)