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

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 Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(241, 340)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setFocusPolicy(QtCore.Qt.NoFocus)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.okcancel = QtGui.QDialogButtonBox(self.centralwidget)
        self.okcancel.setGeometry(QtCore.QRect(70, 250, 156, 31))
        self.okcancel.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.okcancel.setObjectName(_fromUtf8("okcancel"))
        self.box1 = QtGui.QCheckBox(self.centralwidget)
        self.box1.setGeometry(QtCore.QRect(50, 50, 121, 17))
        self.box1.setObjectName(_fromUtf8("box1"))
        self.box2 = QtGui.QCheckBox(self.centralwidget)
        self.box2.setGeometry(QtCore.QRect(50, 90, 141, 17))
        self.box2.setObjectName(_fromUtf8("box2"))
        self.box3 = QtGui.QCheckBox(self.centralwidget)
        self.box3.setGeometry(QtCore.QRect(50, 130, 131, 17))
        self.box3.setObjectName(_fromUtf8("box3"))
        self.box4 = QtGui.QCheckBox(self.centralwidget)
        self.box4.setGeometry(QtCore.QRect(50, 170, 121, 17))
        self.box4.setObjectName(_fromUtf8("box4"))
        self.box5 = QtGui.QCheckBox(self.centralwidget)
        self.box5.setGeometry(QtCore.QRect(50, 210, 121, 17))
        self.box5.setObjectName(_fromUtf8("box5"))
        self.titlelabel = QtGui.QLabel(self.centralwidget)
        self.titlelabel.setGeometry(QtCore.QRect(70, 0, 91, 41))
        self.titlelabel.setObjectName(_fromUtf8("titlelabel"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 241, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(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)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    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))

TheFunk
 
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()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())


Can anyone explain why it's so angry still? I took the code in this post from stack overflow by the way. http://stackoverflow.com/questions/15362624/how-do-you-execute-pyqt-ui-code-in-python
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\metricsgui.py", line 59, in setupUi
    MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'QDialog' object has no attribute 'setCentralWidget'
TheFunk
 
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()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())


However I receive this error:
Code: Select all
Traceback (most recent call last):
  File "C:\Users\funk\Documents\guirunner.py", 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.
TheFunk
 
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 metricsgui.py 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()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())


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?
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm


Return to GUI

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron