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?
-
1I have used the PySide QUiLoader to achieve just that - see post [4442286][1] [1]: http://stackoverflow.com/questions/4442286 – Sven Jan 04 '12 at 23:02
-
The following method is even more practical: http://stackoverflow.com/questions/14892713/how-do-you-load-ui-files-onto-python-classes-with-pyside/14894550#14894550 – iled Dec 23 '14 at 17:53
12 Answers
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.

- 16,753
- 12
- 73
- 90

- 4,100
- 3
- 29
- 43
-
2Could you add how to access a variable in once you've done? Such as pushButton? – Jonathan Jan 13 '14 at 02:06
-
4@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. – Maxim Popravko Sep 14 '15 at 13:33
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_())

- 1
- 1

- 21,267
- 15
- 86
- 95
-
3This should be the best answer, because it is a complete example. – Francesco Pasa Oct 25 '16 at 09:15
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.

- 41,928
- 25
- 127
- 172
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.
- Start Qt Designer from your start menu.
- From "New Form" window, create "Main Window"
- From "Display Widgets" towards the bottom of your "Widget Box Menu" on the left hand side
add a "Label Widget". (Click Drag and Drop) - Double click on the newly added Label Widget to change its name to "Hello World"
- at this point you can use Control + R hotkey to see how it will look.
- Add buttons or text or other widgets by drag and drop if you want.
- 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!
- From your start menu open a command window.
- 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.
- When you get to the directory where your file is stored type the following.
- pyuic4 -x helloworld.ui -o helloworld.py
- Congratulations!! You now have a python Qt4 GUI application!!
- 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.
-
1I was compiling using: pyuic4 helloworld.ui > helloworld.py. One really has to pass -x and -o arguments to pyuic4. Thanks for the information!. – muammar Mar 23 '15 at 16:22
-
Great explanation, especially that the "generation of the GUI"-code is actually in the generated file helped me out, cheers! – Valmond Jul 18 '20 at 17:38
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_())

- 2,622
- 1
- 38
- 71
The cleaner way in my opinion is to first export to .py as aforementioned:
pyuic4 foo.ui > foo.py
And then use it inside your code (e.g main.py
), like:
from foo import Ui_MyWindow
class MyWindow(QtGui.QDialog):
def __init__(self):
super(MyWindow, self).__init__()
self.ui = Ui_MyWindow()
self.ui.setupUi(self)
# go on setting up your handlers like:
# self.ui.okButton.clicked.connect(function_name)
# etc...
def main():
app = QtGui.QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
This way gives the ability to other people who don't use qt-designer to read the code, and also keeps your functionality code outside foo.py
that could be overwritten by designer. You just reference ui
through MyWindow
class as seen above.

- 2,679
- 5
- 31
- 41
-
`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? – Ahsanul Haque Dec 13 '16 at 10:06
You can convert your .ui files to an executable python file using the below command..
pyuic4 -x form1.ui > form1.py
Now you can straightaway execute the python file as
python3(whatever version) form1.py
You can import this file and you can use it.

- 963
- 4
- 12
- 25
-
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. – Not a machine Mar 15 '19 at 00:52
you can compile the ui files like this
pyuic4 -x helloworld.ui -o helloworld.py

- 189
- 2
- 10
In order to compile .ui files to .py files, I did:
python pyuic.py form1.ui > form1.py
Att.

- 41
- 1
in pyqt5 to convert from a ui file to .py file
pyuic5.exe youruifile.ui -o outputpyfile.py -x

- 123
- 1
- 13
(November 2020) This worked for me (UBUNTU 20.04):
pyuic5 /home/someuser/Documents/untitled.ui > /home/someuser/Documents/untitled.py

- 1,936
- 5
- 24
- 33
Using Anaconda3 (September 2018) and QT designer 5.9.5. In QT designer, save your file as ui. Open Anaconda prompt. Search for your file: cd C:.... (copy/paste the access path of your file). Then write: pyuic5 -x helloworld.ui -o helloworld.py (helloworld = name of your file). Enter. Launch Spyder. Open your file .py.

- 1