如何将SQL数据显示到GUI列表小部件中?

时间:2018-03-28 08:50:10

标签: python mysql python-3.x sqlite pyqt4

我想知道如何将数据库表中的信息显示到listwidget或类似的东西上。这是一个flashcard应用程序,一个问题应该一次显示。然后,当单击“显示单击”按钮时,应显示相应的答案,但此时列表窗口小部件中不显示任何内容。 我不明白为什么GUI中没有显示任何内容。

这是我的代码 -

from PyQt4 import QtGui 
from PyQt4 import QtCore
import sys 
import flashcard
import os
import sqlite3 

class FlashCardApp(QtGui.QMainWindow, flashcard.Ui_MainWindow):

    def __init__(self):
        super(self.__class__,self).__init__()
        self.setupUi(self)

        self.questions = []
        self.answers = []
        self.currentQ = 0

        self.RevealAnswerBtn.clicked.connect(self.disA)

    def dispQ(self):
        print("display question {}".format(self.currentQ+1))

        self.listQuestionWidget.clear()
        if self.questions:
            self.listQuestionWidget.addItem(self.questions[self.currentQ])


    def disA(self):
        self.listAnswerWidget.clear()
        if self.answers:
            self.listAnswerWidget.addItem(self.answers[self.currentQ])

    def setData (self, questions, answers):
        self.questions = questions
        self.answers = answers

    def run(self):
        print ("start")

        self.currentQ = 0
        self.dispQ()




def main():
    questions = []
    answers = []

    connection = sqlite3.connect("login.db")
    c = connection.cursor()
    c.execute ("SELECT Question FROM Flashcards")
    resultq = c.fetchall()
    questions.append(resultq)
    c.execute ("SELECT Answer FROM Flashcards")
    resulta = c.fetchall()
    answers.append(resulta)
    connection.close()

    app = QtGui.QApplication(sys.argv)
    form = FlashCardApp()
    form.setData(questions,answers)
    form.run()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

但后来我收到了这个错误 -

Traceback (most recent call last):
  File "C:/Users/joe gorsuch/OneDrive/A-Level/Computer Science/Computer Science Project/Program/Login Form/Ui/1.py", line 68, in <module>
    main()
  File "C:/Users/joe gorsuch/OneDrive/A-Level/Computer Science/Computer Science Project/Program/Login Form/Ui/1.py", line 63, in main
    form.run()
  File "C:/Users/joe gorsuch/OneDrive/A-Level/Computer Science/Computer Science Project/Program/Login Form/Ui/1.py", line 41, in run
    self.dispQ()
  File "C:/Users/joe gorsuch/OneDrive/A-Level/Computer Science/Computer Science Project/Program/Login Form/Ui/1.py", line 25, in dispQ
    self.listQuestionWidget.addItem(self.questions[self.currentQ])
TypeError: arguments did not match any overloaded call:
  QListWidget.addItem(QListWidgetItem): argument 1 has unexpected type 'list'
  QListWidget.addItem(str): argument 1 has unexpected type 'list'

0 个答案:

没有答案