How do you execute pyqt .ui code in python?


Question

I am making a GUI for a script I am making, after researching I found pyqt would be helpful. I have now made a GUI with no working buttons as of yet in pyqt but wanted to look at the code.

I opened cmd.exe and typed in:

pyuic4 project.ui -o python.py

It created me a python.py script which showed me all the script I needed, however when I run it via IDLE or command prompt it shows it as running but no GUI opens up. Am I doing something wrong?

Also on a side note, is pyqt suitable to use to create a simple GUI for windows, or is it for mobile development?

Here is a snippet of code that i want to compile and run

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_FileHash(object):
    def setupUi(self, FileHash):
1
3
3/12/2013 2:36:53 PM

Accepted Answer

Check this tutorial on how to use the generated code by the pyuic4 command;

Basically, you need to do this:

import sys
from PyQt4.QtGui import QApplication, QDialog
from ui_project import Ui_Name  # here you need to correct the names

app = QApplication(sys.argv)
window = QDialog()
ui = Ui_Name()
ui.setupUi(window)

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

Pyqt is completely suitable to create GUIs on Windows.

6
3/12/2013 1:47:48 PM

As a notable addition, pyuic4 accepts a "-p" or "--preview" parameter which will preview the UI file by running it out of the box:

pyuic4 -p project.ui

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon