Henrique Miranda Henrique Miranda - 1 month ago 20
Python Question

Using Qt Designer files in python script

I need to open a dialog from login.py, then if successful, the dialog will close and open a main-window from home.py. I need to do this with a file created by Qt Designer with pyuic4. In summary, I need to call login.py and home.py though main.py.

Code of main.py:

from PyQt4 import QtGui, QtCore
import sqlite3, time
from login import Ui_Dialog
from home import Ui_MainWindow

# Here I need to know how to call login.py, and
# after logged in, how to change to home.py

class RunApp():
pass

if __name__ == '__main__':

app = QtGui.QApplication(sys.argv)
window = RunApp()
sys.exit(app.exec_())


Code of login.py:

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(194, 156)


Code of home.py:

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(635, 396)
MainWindow.setAutoFillBackground(False)


Update:
Thanks my friends ;) Worked for me with this code:

# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
from login import Ui_Dialog
from home import Ui_MainWindow
import sqlite3, time, sys, os

class MyLogin(QtGui.QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.buttonBox.accepted.connect(self.openHome)
def openHome(self):
ui2 = MyHome()
ui2.show()
ui2.exec_()


class MyHome(QtGui.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui2 = Ui_MainWindow()
self.ui2.setupUi(self)

if __name__=='__main__':
root = QtGui.QApplication(sys.argv)
app = MyLogin()
app.show()
root.exec_()

Answer

Create a class that derives from QWidget, and in its __init__ instantiate the ui class, then call setupUi(self) on it.

class RunApp():
    pass  

class MyDialog(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

# do same for Ui_MainWindow via class MyMainWindow(QWidget)
...

This is explained in