pyuic4 ui to py problems

Post here if you need help with creating a Graphical User Interface in Python.

pyuic4 ui to py problems

Postby TheFunk » Tue Sep 10, 2013 2:22 pm

I recently converted a .ui file (generated using QT Designer) to a .py file. The .ui file was then deleted and the .py file refuses to run within IDLE. Is there something I'm missing?

The contents of the .py file:
Code: Select all
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'metricsgui.ui'
# Created: Tue Sep 10 09:10:44 2013
#      by: PyQt4 UI code generator 4.10.3
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

    _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 Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(241, 340)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Preferred)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.okcancel = QtGui.QDialogButtonBox(self.centralwidget)
        self.okcancel.setGeometry(QtCore.QRect(70, 250, 156, 31))
        self.box1 = QtGui.QCheckBox(self.centralwidget)
        self.box1.setGeometry(QtCore.QRect(50, 50, 121, 17))
        self.box2 = QtGui.QCheckBox(self.centralwidget)
        self.box2.setGeometry(QtCore.QRect(50, 90, 141, 17))
        self.box3 = QtGui.QCheckBox(self.centralwidget)
        self.box3.setGeometry(QtCore.QRect(50, 130, 131, 17))
        self.box4 = QtGui.QCheckBox(self.centralwidget)
        self.box4.setGeometry(QtCore.QRect(50, 170, 121, 17))
        self.box5 = QtGui.QCheckBox(self.centralwidget)
        self.box5.setGeometry(QtCore.QRect(50, 210, 121, 17))
        self.titlelabel = QtGui.QLabel(self.centralwidget)
        self.titlelabel.setGeometry(QtCore.QRect(70, 0, 91, 41))
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 241, 21))
        self.statusbar = QtGui.QStatusBar(MainWindow)

        QtCore.QObject.connect(self.box1, QtCore.SIGNAL(_fromUtf8("pressed()")), MainWindow.ckbutton1)
        QtCore.QObject.connect(self.box2, QtCore.SIGNAL(_fromUtf8("pressed()")), MainWindow.ckbutton2)
        QtCore.QObject.connect(self.box3, QtCore.SIGNAL(_fromUtf8("pressed()")), MainWindow.ckbutton3)
        QtCore.QObject.connect(self.box4, QtCore.SIGNAL(_fromUtf8("pressed()")), MainWindow.ckbutton4)
        QtCore.QObject.connect(self.box5, QtCore.SIGNAL(_fromUtf8("pressed()")), MainWindow.ckbutton5)
        QtCore.QObject.connect(self.okcancel, QtCore.SIGNAL(_fromUtf8("accepted()")), MainWindow.checkvars)
        QtCore.QObject.connect(self.okcancel, QtCore.SIGNAL(_fromUtf8("rejected()")), MainWindow.close)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Metrics", None))
        self.box1.setText(_translate("MainWindow", "Signal Wait Percent", None))
        self.box2.setText(_translate("MainWindow", "SQL Compilation Percent", None))
        self.box3.setText(_translate("MainWindow", "Page Lookups/Second", None))
        self.box4.setText(_translate("MainWindow", "Average Task Count", None))
        self.box5.setText(_translate("MainWindow", "Buffer Pool IO Rate", None))
        self.titlelabel.setText(_translate("MainWindow", "SQL Server Tests", None))

Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: pyuic4 ui to py problems

Postby TheFunk » Wed Sep 11, 2013 1:48 pm

Ok. I know now why the file wouldn't run. I've transferred the generated code to a misc folder within Python's libraries now, and am attempting to generate a window with the following script:

Code: Select all
import sys
from PyQt4.QtGui import QApplication, QDialog
from misc import metricsgui  # here you need to correct the names

app = QApplication(sys.argv)
window = QDialog()
ui = metricsgui.Ui_MainWindow()

Can anyone explain why it's so angry still? I took the code in this post from stack overflow by the way.
Credit where credit is due.

Also the error I'm receiving seems to indicate a few things as well.
Code: Select all
File "C:\Python33\lib\misc\", line 59, in setupUi
AttributeError: 'QDialog' object has no attribute 'setCentralWidget'
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: pyuic4 ui to py problems

Postby TheFunk » Wed Sep 11, 2013 5:46 pm

Making more progress. I realize that the python file that starts the GUI needs revised as my main class is QMainWindow and not QDialog (figured that out all on my own! how cute is that?). In addition, I'm fairly certain that said script (see post above) is looking for a .ui file and not a .py

The code now reads:
Code: Select all
import sys
from PyQt4.QtGui import QApplication, QMainWindow
from misc import metricsgui # metricsgui.ui is my ui file

app = QApplication(sys.argv)
window = QMainWindow() # changed from QDialog
ui = metricsgui()

However I receive this error:
Code: Select all
Traceback (most recent call last):
  File "C:\Users\funk\Documents\", line 7, in <module>
    ui = metricsgui()
TypeError: 'module' object is not callable

Because my metricsgui file is a .py and not a .ui

At least this is my assumption now. I'll redo my work in designer, save a metricsgui.ui file in that misc folder and see if that solves the problem.
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: pyuic4 ui to py problems

Postby TheFunk » Thu Sep 12, 2013 12:10 pm

Got it! I just needed to change that start script. I added the following script directly to the end of the class. I know, probably not best practice, but it works and I don't have to import anything I've written as a module.

Code: Select all
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()

I looked at another example with dialog. So I still kinda cheated, but I think I understand how it works at least.

The first line creates a new GUI application, and the second creates the actual main window. The third creates a Ui_MainWindow object, from the class created by QT Designer. Then the setupUi function is ran against this object to size the window and add buttons and labels, etc. It does as the name suggests and sets up the UI. Lastly the show() function is used to make everything appear on the screen. Is that about right?
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests