TransWikia.com

Ошибка QWidget: Must construct a QApplication before a QWidget

Stack Overflow на русском Asked by jeremy greenwoods on November 7, 2021

Код ниже выдаёт ошибку:

QWidget: Must construct a QApplication before a QWidget

Как её исправить?


from PyQt5 import uic
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout,QMessageBox)

import sys
import mydesign

with open("login_data.txt", "r") as login_file: 
    try:
        users = json.load(login_file)
    except:
        users = {}

win = uic.loadUi("C:/mydesign.ui")

class SignInWindow(QWidget):
    def __init__(self):
        super().__init__()
        layout = QGridLayout()
        self.setLayout(layout)
        loginbutton.clicked.connect(self.check)

    def check(self):
        uname = self.usernameline.text()
        pword=  self.passwordline.text()
        if uname in users and users[uname] == pword:
            self.label.setText("Welcome back")
        else:
            self.label.setText("No user with username/password has been been found")
            
if __name__ == '__main__':

    app = QApplication(sys.argv)

    si = SignInWindow()
    si.show()
    sys.exit(app.exec_())

One Answer

попробуйте так:

from PyQt5 import uic
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout,QMessageBox)

import sys
import mydesign

with open("login_data.txt", "r") as login_file: 
    try:
        users = json.load(login_file)
    except:
        users = {}

# win = uic.loadUi("C:/mydesign.ui")              # ---

class SignInWindow(QWidget):
    def __init__(self):
        super().__init__()

        uic.loadUi("C:/mydesign.ui", self)               # +++

        layout = QGridLayout()
        self.setLayout(layout)
        loginbutton.clicked.connect(self.check)

    def check(self):
        uname = self.usernameline.text()
        pword=  self.passwordline.text()
        if uname in users and users[uname] == pword:
            self.label.setText("Welcome back")
        else:
            self.label.setText("No user with username/password has been been found")
            
if __name__ == '__main__':

    app = QApplication(sys.argv)

    si = SignInWindow()
    si.show()
    sys.exit(app.exec_())

Update

Выдаёт ошибку Traceback (most recent call last): TypeError: ('Wrong base class of toplevel widget', (<class 'main.SignInWindow'>, 'QMainWindow'))

замените class SignInWindow(QWidget): на class SignInWindow(QMainWindow):

Answered by S. Nick on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP