How To Use Multiple .ui Files In The Main Python File
Solution 1:
You have the following errors:
There can only be one object of the
QApplication
, you are creating one for each window and that is incorrect.Avoid using global variables, they are considered a bad practice and few cases are necessary. For more information read: Why are global variables evil?
Qt Designer does not offer a widget but a class that serves to fill a widget so PyQt recommend to create a suitable widget class and fill it with that class, for more information read: http://pyqt.sourceforge.net/Docs/PyQt5/designer.html
Going to the point, you can not use a flag because the process is asynchronous, in Qt you work with signals, that's the correct way to inform that there is a change, for example a way to keep the classes separate is to create a signal that indicates that you have logged in, close the window and open the other window.
from LoginWindowUI import Ui_LoginWindow
from MainWindowUI import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class Main_Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Main_Window, self).__init__(parent)
self.setupUi(self)
class LoginWindow(QtWidgets.QMainWindow, Ui_LoginWindow):
logged = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(LoginWindow, self).__init__(parent)
self.setupUi(self)
self.login_btn.clicked.connect(self.authenticate)
@QtCore.pyqtSlot()
def authenticate(self):
db_user = self.username_ldt.text()
db_pass = self.password_ldt.text()
if db_user == 'admin' and db_pass=='admin':
self.logged.emit()
self.close()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
login = LoginWindow()
w = Main_Window()
login.logged.connect(w.show)
login.show()
sys.exit(app.exec_())
if __name__ == '__main__': main()
Post a Comment for "How To Use Multiple .ui Files In The Main Python File"