4

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

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

my file is currently project.ui, and i would like it to become project.py where you said i should correct names however I dont have any ui_project or ui_name. I tried to put 'import project.ui' but that did not work.
@DrKhan The command you executed generated a .py file, you need to put its name instead of "ui_project"
I must be getting my file names mixed up, i have added a code snippet, i hope this shows what i am trying to do.
I have got it working now, i typed in: pyuic4 -x project.ui -o python.py This gave me a .pyc file which opens fully and i can look at the code by typing the first command in my origonal code.
1

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

Comments

0

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

1 Comment

I used this and I get the hello.py. When I'm trying to execute this like python hello.py I see nothing. The widget is show and closes very quick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.