Binding a .ui file with qtDesigner with python / pyqt?

So, if I log in to QtDesigner and create a user interface, it will be saved as a .ui file. How can I do this as a python file or use it in python?

+70
python user-interface qt pyqt qt-designer
Mar 08 '10 at 1:27
source share
11 answers

Another way to use .ui in your code:

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

both approaches are good. Remember that if you use Qt resource files (extremely useful) for icons, etc., you should also compile it:

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

Note that when uic compiles the interface, it adds "import images_rc" to the end of the .py file, so you must compile the resources into a file with that name or rename it to the generated code.

+53
Mar 23 '10 at 14:59
source share

Combining the Max response and the message on the Shriramana Sharma mailing list , I created a small working example to download the mywindow.ui file containing QMainWindow (so just select the main window in the File-New Qt Designer 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_()) 
+36
Jun 16 '13 at 11:51
source share

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

 pyuic form1.ui > form1.py 

with pyqt4

 pyuic4.bat form1.ui > form1.py 

Then you can import form1 into your script.

+28
Mar 08 '10 at 6:19 06:19
source share

I found this article very helpful.

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

I will briefly describe the steps for creating and modifying a .ui file into a .py file taken from this article.

  • Launch Qt Designer in the Start menu.
  • In the "New form" window, create a "Main window"
  • From “Show widgets” at the bottom of your “Widget Menu” on the left | add a shortcut widget. (Click "Drag and Drop").
  • Double-click on the added shortcut label to change its name to "Hello World"
  • at this point, you can use the Control + R hotkey to see how it will look.
  • Add buttons or text or other widgets by dragging them if you want.
  • Now save the form. File-> Save As-> "Hello World.ui" (Control + S will also open "Save As"). Pay attention to the directory in which you saved "Hello World" .ui
    file. (I saved my (C :) for convenience)

The file is created and saved, now we will generate Python code using pyuic!

  • From the Start menu, open a command window.
  • Now "cd" to 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.
  • When you get to the directory where your file is stored, enter the following.
  • pyuic4 -x helloworld.ui -o helloworld.py
  • Congratulations !! Now you have the python Qt4 GUI application!
  • Double-click the helloworld.py file to launch it. (I use pyscripter and double click
    it opens in pyscripter, then I "run" the file from there)

Hope this helps someone.

+24
Mar 02 '12 at 1:05
source share

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_()) 
+11
Jul 08 '15 at 3:18
source share

You can convert your .ui files to python executable with the following command.

 pyuic4 -x form1.ui > form1.py 

Now you can immediately execute the python file as

 python3(whatever version) form1.py 

You can import this file, and you can use it.

+7
Mar 08
source share

In my opinion, a cleaner way is to first export to .py as above:

 pyuic4 foo.ui > foo.py 

And then use it inside your code (e.g. main.py ), for example:

 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 method allows other people who do not use qt-designer to read code, and also saves your functionality code outside of foo.py , which can be rewritten by the designer. You simply reference the ui class via MyWindow , as shown above.

+7
Jun 19 '16 at 12:12
source share

you can compile ui files like this

 pyuic4 -x helloworld.ui -o helloworld.py 
+5
Feb 03 '14 at 10:22
source share

To compile .ui files into .py files, I did:

 python pyuic.py form1.ui > form1.py 

Att.

+4
Sep 25 '14 at 11:44
source share

in pyqt5 to convert from user interface file to .py file

pyuic5.exe youruifile.ui -o outputpyfile.py -x

+1
Jan 09 '19 at 22:31
source share

Using Anaconda3 (September 2018) and QT Designer 5.9.5. In QT Designer, save your file as a user interface. Open Anaconda. Find your file: cd C: .... (copy / paste the access path to your file). Then write: pyuic5 -x helloworld.ui -o helloworld.py (helloworld = your file name). To come in. Launch Spyder. Open your .py file.

0
Sep 12 '18 at 22:08
source share



All Articles