尝试在PyQT5中查看数据时Python崩溃

2024-09-28 22:41:43 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我的标题不清楚,我很抱歉;我正在尝试从SQLLite数据库(正在工作)中检索一些数据,并在PyQt5中的窗口中显示给用户。到目前为止,我已经成功地检索到结果并用Python打印,但是当试图在窗口中添加/查看结果时,Python就崩溃了。 我插入了下面不起作用的代码。(复制时缩进有点滑稽,但在我的文件中它是完美的)。在

class Profile(QWidget):
def __init__(self, Type):
    super().__init__()
    self.window = QWidget()
    self.window.setGeometry(100,100, 350, 400)
    self.window.setWindowTitle("Welcome")
    self.window.show()
    self.Choices()

def Choices(self):
    self.layout = QGridLayout()
    c.execute('''SELECT username, teamname FROM Users WHERE username = ?''',
    (user,))
    result = c.fetchone()
    print(result)
    print(user)

    self.TeamInfo = QLabel(result)
    self.layout.addWidget(self.TeamInfo)
    self.window.setLayout(self.layout)
    self.window.show()

user是前一个窗口(登录页面)中的全局变量,以避免用户必须重新输入用户名。本节不包括在内,因为这不是问题所在。这个类中所有其他的按钮都起作用了-只是这个特殊的部分。任何关于如何解决这个问题的帮助都是非常感谢的,我认为问题是self.TeamInfo = QLabel(result),但我不知道如何解决这个问题。在

编辑:我已经包含了一个错误消息的截图。在

Error


Tags: 用户selfinitdefshowusernameresultwindow
2条回答

如果我理解正确的话,你想在QLabel中添加一个文本,对吗? 根据文档,向其添加文本的操作是:QLabel().setText(result)。在

编辑:你能试试这个,告诉我编译器在抱怨什么吗?在

label = QLabel()
label.setText(result)
self.layout.addWidget(label)

试试看:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

import sqlite3

class Profile(QWidget):
    def __init__(self, Type=None):
        super().__init__()
        self.window = QWidget()
        self.window.setGeometry(100,100, 350, 400)
        self.window.setWindowTitle("Welcome")
        self.Choices()

    def Choices(self):
        self.layout = QGridLayout()

#        c.execute('''SELECT username, teamname FROM Users WHERE username = ?''',
#                  (user,))
#        result = c.fetchone()
#

        user = "Nick" 
        try:
            self.conn = sqlite3.connect("database.db")
            self.c = self.conn.cursor()
#            result = self.c.execute("SELECT * from students WHERE name='{}'".format(user) )   
            result = self.c.execute("SELECT * from students WHERE name=?", (user, ))

            row = result.fetchone()                                               # < -
            print("\nrow->", type(row), row)
            serachresult = "Rollno : "+str(row[0])+'\n'+"Name : "+str(row[1])+'\n'+"Branch : "+str(row[2])+'\n'+"Sem : "+str(row[3])+'\n'+"Address : "+str(row[4])
            QMessageBox.information(QMessageBox(), 'Successful', serachresult)
            self.conn.commit()
            self.c.close()
            self.conn.close()
        except Exception:
            QMessageBox.warning(QMessageBox(), 'Error', 'Could not Find student from the database.')

        print(result)
        print(user)

        self.TeamInfo = QLabel(", ".join([ str(i) for i in row ]))                # < -
        self.layout.addWidget(self.TeamInfo)
        self.window.setLayout(self.layout)
        self.window.show()

if __name__ == '__main__':
     app = QApplication(sys.argv)
     main = Profile()
     main.show()
     sys.exit(app.exec_()) 

enter image description here

相关问题 更多 >