Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help to run my sample PyQt program [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Help to run my sample PyQt program [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
pyqtqtcreatorpyside
5 Posts 3 Posters 5.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John R
    wrote on last edited by John R
    #1

    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]

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wlof
      wrote on last edited by wlof
      #2

      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.

      J 1 Reply Last reply
      1
      • W wlof

        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.

        J Offline
        J Offline
        John R
        wrote on last edited by
        #3

        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?

        W 1 Reply Last reply
        0
        • J John R

          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?

          W Offline
          W Offline
          wlof
          wrote on last edited by wlof
          #4

          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!

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mohammed Abu ghneim
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved