填充QTableWidget后Python停止工作?

时间:2017-07-30 09:04:08

标签: python pyqt pyqt5

我正在尝试创建一个学生数据库系统,我在其中定义了一个名为showStudent的函数,该函数从构造函数中传递的列表中填充QTableWidget。我正在使用SQLite作为数据库并且数据正确传递但是一旦到达table.show(),程序就会崩溃。请帮忙,即使调试太多,我也没能解决问题。

def showStudent(list):
    app = QApplication(sys.argv)
    roll=0
    gender = ""
    branch = ""
    year = ""
    name = ""
    address = ""
    mobile = -1
    academic_year = -1

    roll=list[0]
    name=list[1]

    if list[2]==0:
        gender="Male"
    else:
        gender="Female"

    if list[3]==0:
        branch="Mechanical Engineering"
    elif list[3]==1:
        branch="Civil Engineering"
    elif list[3]==2:
        branch="Electrical Engineering"
    elif list[3]==3:
        branch="Electronics and Communication Engineering"
    elif list[3]==4:
        branch="Computer Science and Engineering"
    elif list[3]==5:
        branch="Information Technology"

    if list[4]==0:
        year="1st"
    elif list[4]==1:
        year="2nd"
    elif list[4]==2:
        year="3rd"
    elif list[4]==3:
        year="4th"

    academic_year=list[5]
    address=list[6]
    mobile=list[7]

    table=QTableWidget()
    table.setWindowTitle("Student Details")
    table.resize(500, 300)
    table.setRowCount(8)
    table.setColumnCount(2)

    table.setItem(0, 0, QTableWidgetItem("Roll"))
    table.setItem(0, 1, QTableWidgetItem(str(roll)))
    table.setItem(1, 0, QTableWidgetItem("Name"))
    table.setItem(1, 1, QTableWidgetItem(str(name)))
    table.setItem(2, 0, QTableWidgetItem("Gender"))
    table.setItem(2, 1, QTableWidgetItem(str(gender)))
    table.setItem(3, 0, QTableWidgetItem("Branch"))
    table.setItem(3, 1, QTableWidgetItem(str(branch)))
    table.setItem(4, 0, QTableWidgetItem("Year"))
    table.setItem(4, 1, QTableWidgetItem(str(year)))
    table.setItem(5, 0, QTableWidgetItem("Academic Year"))
    table.setItem(5, 1, QTableWidgetItem(str(academic_year)))
    table.setItem(6, 0, QTableWidgetItem("Address"))
    table.setItem(6, 1, QTableWidgetItem(str(address)))
    table.setItem(7, 0, QTableWidgetItem("Mobile"))
    table.setItem(7, 1, QTableWidgetItem(str(mobile)))


    table.show()
    return app.exec()

这是我的DBHelper类,它包含调用我上面函数的函数searchStudent。

class DBHelper():
    def __init__(self):
        self.conn=sqlite3.connect("sdms.db")
        self.c=self.conn.cursor()
        self.c.execute("CREATE TABLE IF NOT EXISTS students(roll INTEGER,name TEXT,gender INTEGER,branch INTEGER,year INTEGER,academic_year INTEGER,address TEXT,mobile INTEGER)")
        self.c.execute("CREATE TABLE IF NOT EXISTS genders(id INTEGER,name TEXT)")
        self.c.execute("CREATE TABLE IF NOT EXISTS branches(id INTEGER,name TEXT)")
    def addStudent(self,roll,name,gender,branch,year,academic_year,address,mobile):
        try:
            self.c.execute("INSERT INTO students (roll,name,gender,branch,year,academic_year,address,mobile) VALUES (?,?,?,?,?,?,?,?)",(roll,name,gender,branch,year,academic_year,address,mobile))
            self.conn.commit()
            self.c.close()
            self.conn.close()
            QMessageBox.information(QMessageBox(),'Successful','Student is added successfully to the database.')
        except Exception:
            QMessageBox.warning(QMessageBox(), 'Error', 'Could not add student to the database.')

    def searchStudent(self,roll):
        self.c.execute("SELECT * from students WHERE roll="+str(roll))
        self.data=self.c.fetchone()
        self.list=[]
        for i in range(0,8):
            self.list.append(self.data[i])
        self.c.close()
        self.conn.close()
        showStudent(self.list)

下面是我的DBHelper类的实例化。

def showStudent(self):
    showstudent=DBHelper()
    showstudent.searchStudent(14101106001)

1 个答案:

答案 0 :(得分:1)

在pyqt中,应该只有一个QApplication对象,只应调用一次QApplication.exec_()

因此,我建议您创建一个QDialog并将QTableWidget放在我所在的位置:

def showStudent(list):
        ...
        dialog = QDialog()
        dialog.setLayout(QVBoxLayout())
        table=QTableWidget()
        dialog.layout().addWidget(table)
        ...
        dialog.exec_()