Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPushButton exit my application when clicked



  • When i click on the QPushButtons i have created it closes my application. Below is my code, I would be glad if i get a helping hand. Thanks .

    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
    import sys

    import sqlite3

    from PyQt5.uic import loadUiType

    ui,_ = loadUiType('Conf_Reg_Manager.ui')

    class MainApp(QMainWindow, ui):
    def init(self, parent=None):
    QMainWindow.init(self)
    super(MainApp,self).init(parent)
    self.setupUi(self)
    self.Handle_UI_Changes()
    self.Handle_Buttons()

              ############################################################################
              ############### FUNCTIONS FOR UI CHANGES AND BUTTONS EFFECT ################
    
    def Handle_UI_Changes(self):
        self.Hide_Themes()
        self.tabWidget.tabBar().setVisible(False)
    
    
    
    
    
    
    def Handle_Buttons(self):
        self.pushButton_4.clicked.connect(self.Show_Themes)
        self.pushButton_20.clicked.connect(self.Hide_Themes)
    
    
    
        self.pushButton_6.clicked.connect(self.Open_Daily_Operations_Tab)
        self.pushButton_23.clicked.connect(self.Open_Infromation_Details_Tab)
        self.pushButton_2.clicked.connect(self.Open_Users_Tab)
        self.pushButton_3.clicked.connect(self.Open_Settings_Tab)
    
    
    
        self.pushButton.clicked.connect(self.GET_DATA)
    

    ############ CONNECT TO SQLITE3 DATABASE AND FILL GUI TABLE WITH DATA

    def GET_DATA(self):
    
        db=sqlite3.connect('Conf_Registry_Storage')
        cursor=db.cursor()
    
    
        command=''' SELECT * from Daily_Operations '''
    
        result=cursor.execute(command)
    
    
        self.tableWidget_6.setRowCount(0)
    
    
        for row_number, row_data in enumerate(result):
            self.tableWidget_6.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.tableWidget_6.setItem(row_number, column_number, QTableWidgetItem(str(data)))
    
    
    
        ##############################################
        ########## SHOW AND HIDE THEMES ######################
    
    
    
    def Show_Themes(self):
        self.groupBox_7.show()
    
    
    def Hide_Themes(self):
        self.groupBox_7.hide()
    
    
    
    
     ##############################################
     ########## OPENING TABS ######################
    
    
    def Open_Daily_Operations_Tab(self):
        self.tabWidget.setCurrentIndex(0)
    
    
    def Open_Infromation_Details_Tab(self):
        self.tabWidget.setCurrentIndex(1)
    
    
    def Open_Users_Tab(self):
        self.tabWidget.setCurrentIndex(2)
    
    
    def Open_Settings_Tab(self):
        self.tabWidget.setCurrentIndex(3)
    

    def main():
    app = QApplication(sys.argv)
    window = MainApp()
    window.show()
    app.exec_()

    if name == 'main':
    main()



  • @LT-K101
    Run it in a debugger and see where it crashes/how far you get. Or put print() statements in.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Which buttons triggers that ?

    By the way, please use coding tags around your code (the </> button) so it makes it easier to read.



  • @JonB I used pycharm community edition to run the program and i get no error message just that the application exits when i click on a refresh button to load data from sqlite3 database



  • @SGaist when i write a function for a button to either insert data into sqlite3 DB or load data from sqlite3 DB.



  • @LT-K101
    Did you step through in the debugger to watch which statements get executed and where it crashes? Or at least put in print() statements?



  • @JonB I did insert the print () statement and it did work perfectly. I think its the database connection.


  • Lifetime Qt Champion

    Out of curiosity, why not use Qt's SQL module ?



  • @SGaist I have not tried Qtsql but I'm familiar with sqlite3.


  • Lifetime Qt Champion

    Then as asked before: which exact method makes your application crash ?

    Which exception do you get ?

    Did you try to use pdb to see what is going on ?



  • @SGaist now i can run and display my application but the syntax for connecting to the db within the PyQT5 GUI class is my problem now.



  • @LT-K101
    PyQt5 does not come with its own Python documentation for Qt methods. They expect you to look at the standard C++ docs and adapt to Python. PySide does give some documentation, and should be same for PyQt5. So you should read through e.g. https://doc.qt.io/qtforpython-5/PySide2/QtSql/QSqlDatabase.html#detailed-description where there are Python examples of using QSqlDatabase, etc.


Log in to reply