1

I am currently trying to make GUI in python. For this, the way I found is to use Qt Designer to generate a graphical interface .ui, and to convert it in .py with PyQt4, to edit functions in python.

The question I have is then how to modify the graphical interface without having to set up again the whole python code ?

Indeed, if I want for instance to add a button in the interface, with a particular function, I would simply like to add this function in the .py, without doing again the whole .py

To sum up, I would like to be able to update in the future my interface easily.

Do you have any idea ?

Thank you in advance ! :)

qcha
  • 523
  • 1
  • 5
  • 19
  • Not sure what you mean. If you don't like the whole Qt Designer > ui file > python file, you can directly write the layout in python. If you mean you're currently manually modifying the .ui/.py file made with the designer, you're not supposed to do that. You're suppose to load the ui file into your own python file. – Mel Jun 21 '16 at 15:40
  • For me, I thought that every time I modify the layout graphically (what I want absolutely) in QtDesigner, I have then to save it in a .ui file, and then to manage the functions called (by buttons pressed for instance) in a new .py file coming from a convertion of the last .ui file. But as this .py file is new, I have to write copy/paste a lot of part form the previous .py file. Maybe I didn't understand the way it works.. – qcha Jun 21 '16 at 16:12
  • This should be helpful: http://stackoverflow.com/questions/2398800/linking-a-qtdesigner-ui-file-to-python-pyqt – Mel Jun 21 '16 at 16:26
  • PyQt4 Docs: [Using Qt Designer](http://pyqt.sourceforge.net/Docs/PyQt4/designer.html). – ekhumoro Jun 21 '16 at 19:10

1 Answers1

2

Answer. If you're going to modify code from Qt Designer (via pyuic), understand that the process is one way:

.ui file -> pyuic -> .py

pyuic (and uic, the Qt/C++ equivalent) are one way code generators. You can't take existing Python code and make a .ui file for Qt Designer.

Addendum
That said, you have three choices:

  • Stick with .ui files. Make your changes in Qt Designer and have pyuic convert it.
  • Create the initial .ui file to look the way you want, convert to Python, and hand-code from there on. I occasionally do this, but will keep the .ui file around so I know what things should look like.
  • Write it all by hand. In my experience, with time and practice, coders find this comes more naturally and rely on Qt Designer less and less.

Personally, I will create a mockup of complex forms or dialogs in Qt Designer for visual reference, but do all of the coding myself.

jonspaceharper
  • 4,207
  • 2
  • 22
  • 42
  • Is it possible to use the .py file coming from pyuic only for making the layout, and another .py file to manage all functions of the ui (creating slot, if this button is pressed do that, etc..) ? That way, I could update easily the layout on Qt Designer without losing all the functions I have already coded.. If yes, how to do that ? – qcha Jun 22 '16 at 14:49
  • 1
    @qcha. Yes - that's exactly how `pyuic` is supposed to be used. Please see the [code examples](http://pyqt.sourceforge.net/Docs/PyQt4/designer.html#using-the-generated-code) in the link I gave earlier. The general idea is that you simply import the gui class generated by `pyuic` into your main application. That way, the gui and program logic are kept separate. Just remember to re-run `pyuic` every time you make changes in qt designer. – ekhumoro Jun 22 '16 at 15:52