Help to run my sample PyQt program [SOLVED]



  • I have created a UI file in Qtcreator and converted to python using pyuic.
    After conversion I added following code blocks into this new python file for PySide execution
    I had added number of widgets like buttons, radio buttons, textboxes etc. and it run in Qt creator fine with XML but after pyuic conversion I could see only plain blank screen!!, could anyone please tell me, where I have made wrong code. I have tried to run in Tkinter and QtCreator (via Tools --> External --> Python ---> RunPy), both Tkinter and Qt creator
    showing output only blank GUI

    Please note the modified code blocks

    Code Block Added 1:

    import sys
    from PySide import QtCore, QtGui
    

    Code Block Added 2:

    class ControlMainWindow(QtGui.QMainWindow):
     def init(self, parent=None):
         super(ControlMainWindow, self).init(parent)
         self.ui = Ui_MainWindow()
         self.ui.setupUi(self) 
    
    if __name__ == '__main__':     
        app = QtGui.QApplication(sys.argv)
        mySW = ControlMainWindow()
        mySW.show()
        sys.exit(app.exec_())
    

    Full code is below

    import sys
    from PySide import QtCore, QtGui
    
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
        
    except AttributeError:
        def _fromUtf8(s):
            return s
    
    try:
        _encoding = QtGui.QApplication.UnicodeUTF8
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig)
    
    		
    class MainWindow (QtGui.QMainWindow):
     
        def __init__ (self, parent = None):
            super (MainWindow, self).__init__ ()
            self.ui = Ui_MainWindow () 
            self.ui.setupUi (self)
    
    		
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName(_fromUtf8("MainWindow"))
            MainWindow.resize(615, 338)
            MainWindow.setAutoFillBackground(False)
            MainWindow.setStyleSheet(_fromUtf8("background-color: rgb(0,200,21);"))
            self.centralWidget = QtGui.QWidget(MainWindow)
            self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
            self.pushButton = QtGui.QPushButton(self.centralWidget)
            self.pushButton.setGeometry(QtCore.QRect(270, 80, 61, 23))
            self.pushButton.setObjectName(_fromUtf8("pushButton"))
            self.checkBox = QtGui.QCheckBox(self.centralWidget)
            self.checkBox.setGeometry(QtCore.QRect(120, 40, 70, 17))
            self.checkBox.setObjectName(_fromUtf8("checkBox"))
            self.radioButton = QtGui.QRadioButton(self.centralWidget)
            self.radioButton.setGeometry(QtCore.QRect(230, 40, 82, 17))
            self.radioButton.setObjectName(_fromUtf8("radioButton"))
            self.radioButton_2 = QtGui.QRadioButton(self.centralWidget)
            self.radioButton_2.setGeometry(QtCore.QRect(290, 40, 82, 17))
            self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
            self.label = QtGui.QLabel(self.centralWidget)
            self.label.setGeometry(QtCore.QRect(20, 40, 61, 16))
            self.label.setObjectName(_fromUtf8("label"))
            self.formLayoutWidget = QtGui.QWidget(self.centralWidget)
            self.formLayoutWidget.setGeometry(QtCore.QRect(120, 130, 261, 131))
            self.formLayoutWidget.setObjectName(_fromUtf8("formLayoutWidget"))
            self.formLayout = QtGui.QFormLayout(self.formLayoutWidget)
            self.formLayout.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow)
            self.formLayout.setMargin(0)
            self.formLayout.setObjectName(_fromUtf8("formLayout"))
            self.radioButton_3 = QtGui.QRadioButton(self.formLayoutWidget)
            self.radioButton_3.setObjectName(_fromUtf8("radioButton_3"))
            self.formLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.radioButton_3)
            self.checkBox_2 = QtGui.QCheckBox(self.formLayoutWidget)
            self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
            self.formLayout.setWidget(1, QtGui.QFormLayout.FieldRole, self.checkBox_2)
            self.toolButton = QtGui.QToolButton(self.formLayoutWidget)
            self.toolButton.setObjectName(_fromUtf8("toolButton"))
            self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.toolButton)
            MainWindow.setCentralWidget(self.centralWidget)
            self.menuBar = QtGui.QMenuBar(MainWindow)
            self.menuBar.setGeometry(QtCore.QRect(0, 0, 615, 21))
            self.menuBar.setObjectName(_fromUtf8("menuBar"))
            MainWindow.setMenuBar(self.menuBar)
            self.mainToolBar = QtGui.QToolBar(MainWindow)
            self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
            MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
            self.statusBar = QtGui.QStatusBar(MainWindow)
            self.statusBar.setObjectName(_fromUtf8("statusBar"))
            MainWindow.setStatusBar(self.statusBar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
            self.pushButton.setText(_translate("MainWindow", "Submit", None))
            self.checkBox.setText(_translate("MainWindow", "Scale", None))
            self.radioButton.setText(_translate("MainWindow", "Mesh", None))
            self.radioButton_2.setText(_translate("MainWindow", "Plot", None))
            self.label.setText(_translate("MainWindow", "Enter Values", None))
            self.radioButton_3.setText(_translate("MainWindow", "RadioButton", None))
            self.checkBox_2.setText(_translate("MainWindow", "CheckBox", None))
            self.toolButton.setText(_translate("MainWindow", "...", None))
    
    
    class ControlMainWindow(QtGui.QMainWindow):
     def init(self, parent=None):
         super(ControlMainWindow, self).init(parent)
         self.ui = Ui_MainWindow()
         self.ui.setupUi(self) 
    
    if __name__ == '__main__':     
        app = QtGui.QApplication(sys.argv)
        mySW = ControlMainWindow()
        mySW.show()
        sys.exit(app.exec_())
    

    [edit: added missing coding tags ```before and after SGaist]



  • There are a number of errors in your code. Most notably, the constructor in your ControlMainWindow class is never called, because it's wrongly named. Constructors in Python are named "__init__", not "init".

    Generally speaking, it's bad practice to modify generated code. What you should do instead is create another file, and import the generated code from there.

    For example, let's say your UI file is named ui_test.ui. From this, you'd generate a ui_test.py file. Then, you'd create a test.py file with something like this:

    import sys
    from PySide import QtCore, QtGui
    
    from ui_test import Ui_MainWindow
    
    class ControlMainWindow(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(ControlMainWindow, self).__init__(parent)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self) 
    
    if __name__ == '__main__':     
        app = QtGui.QApplication(sys.argv)
        mySW = ControlMainWindow()
        mySW.show()
        sys.exit(app.exec_())
    

    This is much better because it lets you separate the control logic of your application from the UI logic. Also, should you need to modify something in your UI file, you can then regenerate the Python code without overwriting your changes.



  • Dear wlof,

    you said exactly correct, Thanks a lot for helping me!!

    Replacement of Init keyword and import sys will do the job, great!!!

    My only problem was why nobody answered to this thread for a long time. Did I ask anything not up-to the mark?



  • I'm not sure why nobody replied before me. Maybe most users here are C++ devs and not too familiar with Python? I found your post because you tagged it with "pyqt" and I have some experience with it, so I thought I'd try helping someone while waiting for a reply to my own problem ;)

    Anyway, I'm glad your issue is solved. If you can, mark the topic as solved, by editing the subject of your first post and adding [SOLVED] in front of it!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.