Linking a qtDesigner .ui file to python/pyqt?

134,765

Solution 1

Another way to use .ui in your code is:

from PyQt4 import QtCore, QtGui, uic
class MyWidget(QtGui.QWidget)
    ...
    #somewhere in constructor:
    uic.loadUi('MyWidget.ui', self)

both approaches are good. Do not forget, that if you use Qt resource files (extremely useful) for icons and so on, you must compile it too:

pyrcc4.exe -o ui/images_rc.py ui/images/images.qrc

Note, when uic compiles interface, it adds 'import images_rc' at the end of .py file, so you must compile resources into the file with this name, or rename it in generated code.

Solution 2

Combining Max's answer and Shriramana Sharma's mailing list post, I built a small working example for loading a mywindow.ui file containing a QMainWindow (so just choose to create a Main Window in Qt Designer's File-New dialog).

This is the code that loads it:

import sys
from PyQt4 import QtGui, uic

class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('mywindow.ui', self)
        self.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

Solution 3

You need to generate a python file from your ui file with the pyuic tool (site-packages\pyqt4\bin)

pyuic form1.ui > form1.py

with pyqt4

pyuic4.bat form1.ui > form1.py

Then you can import the form1 into your script.

Solution 4

I found this article very helpful.

http://talk.maemo.org/archive/index.php/t-43663.html

I'll briefly describe the actions to create and change .ui file to .py file, taken from that article.

  1. Start Qt Designer from your start menu.
  2. From "New Form" window, create "Main Window"
  3. From "Display Widgets" towards the bottom of your "Widget Box Menu" on the left hand side
    add a "Label Widget". (Click Drag and Drop)
  4. Double click on the newly added Label Widget to change its name to "Hello World"
  5. at this point you can use Control + R hotkey to see how it will look.
  6. Add buttons or text or other widgets by drag and drop if you want.
  7. Now save your form.. File->Save As-> "Hello World.ui" (Control + S will also bring up
    the "Save As" option) Keep note of the directory where you saved your "Hello World" .ui
    file. (I saved mine in (C:) for convenience)

The file is created and saved, now we will Generate the Python code from it using pyuic!

  1. From your start menu open a command window.
  2. Now "cd" into the directory where you saved your "Hello World.ui" For me i just had to "cd\" and was at my "C:>" prompt, where my "Hello World.ui" was saved to.
  3. When you get to the directory where your file is stored type the following.
  4. pyuic4 -x helloworld.ui -o helloworld.py
  5. Congratulations!! You now have a python Qt4 GUI application!!
  6. Double click your helloworld.py file to run it. ( I use pyscripter and upon double click
    it opens in pyscripter, then i "run" the file from there)

Hope this helps someone.

Solution 5

You can also use uic in PyQt5 with the following code.

from PyQt5 import uic, QtWidgets
import sys

class Ui(QtWidgets.QDialog):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('SomeUi.ui', self)
        self.show()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Ui()
    sys.exit(app.exec_())
Share:
134,765

Related videos on Youtube

Chris
Author by

Chris

Updated on November 09, 2020

Comments

  • Chris
    Chris over 3 years

    So if I go into QtDesigner and build a UI, it'll be saved as a .ui file. How can I make this as a python file or use this in python?

  • Jonathan
    Jonathan over 10 years
    Could you add how to access a variable in once you've done? Such as pushButton?
  • muammar
    muammar about 9 years
    I was compiling using: pyuic4 helloworld.ui > helloworld.py. One really has to pass -x and -o arguments to pyuic4. Thanks for the information!.
  • Maxim Popravko
    Maxim Popravko over 8 years
    @JonathanLeaders If there is a button defined in .ui file, you just call it like this: self.pushButton. You can also load ui to any class attribute you want: uic.loadUi('uifile.ui', self.ui) and call widgets and stuff like self.ui.pushButton.
  • Francesco Pasa
    Francesco Pasa over 7 years
    This should be the best answer, because it is a complete example.
  • Ahsanul Haque
    Ahsanul Haque over 7 years
    self.ui.okButton.clicked.connect(self.function_name) gives me TypeError: event() takes 1 positional argument but 2 were given. How did you solve it?
  • Not a machine
    Not a machine about 5 years
    FYI If you omit the -x option you will not get the portion of the code that creates and runs the application. I have seen a few answers which omit "-x". This answer is the most complete.
  • Valmond
    Valmond almost 4 years
    Great explanation, especially that the "generation of the GUI"-code is actually in the generated file helped me out, cheers!