DrKhan
DrKhan

Reputation: 313

How do you execute pyqt .ui code in python?

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

Upvotes: 4

Views: 14385

Answers (3)

Mehrdad
Mehrdad

Reputation: 1

If you check the pyuic4 options, -x option is the one you are missing. It adds the required code for executing the UI to your python code. You only need to add -x:

pyuic4 project.ui -x -o python.py

Upvotes: 0

FichteFoll
FichteFoll

Reputation: 792

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

Upvotes: 1

Fábio Diniz
Fábio Diniz

Reputation: 10363

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.

Upvotes: 7

Related Questions