0

I wrote a password generator and built a GUI for it using the PyQt5 designer. The script calls the .ui in its initiator and both are in the same folder.

def __init__(self, parent=None):
    super().__init__(parent)
    self.ui = uic.loadUi('Generator.ui', self)

However, after converting both to an .exe file via PyInstaller, after extracting the .exe file from its dist folder and executing it, a console pops up and closes immediately, without showing the GUI.

How can I fix this without manually adding the .ui code to the Generator.py script??

Thank you

Seneo
  • 87
  • 1
  • 11
  • 1
    If you run the executable in a command prompt, you can capture the error, then people might be able to assist with a solution. My first guess would be some problem with the path to the .ui file. Do you explicitly include that in your executable? – import random Jan 04 '18 at 22:23
  • You can use the pyuic tool to make the .ui file into a .py file, then subclass the created Ui_whatever class into your GUI class. This is different than the approach you posted, but I have used this method in many applications created with pyinstaller. – MalloyDelacroix Jan 04 '18 at 22:28
  • @Eric how do I include the .ui file in the executable path? I just use 'pyinstaller.exe --onefile Generator.py' – Seneo Jan 04 '18 at 22:38
  • @MalloyDelacroix I will try this approach as well. So just convert the .ui file into a .py file and then import the generated .py in the main script, which I then convert to the .exe? – Seneo Jan 04 '18 at 22:40
  • Basically. In the created .py file there will be a class generated that has the name of whatever you named the widget in QtDesigner. Import this class and subclass it. – MalloyDelacroix Jan 04 '18 at 22:41
  • See the [PyInstaller](https://pythonhosted.org/PyInstaller/spec-files.html#adding-data-files) documentation on adding data files. – import random Jan 04 '18 at 22:46
  • @Eric I added the *GUI.ui* in the pyinstaller call (`pyinstaller.exe --add-data "GUI.ui;GUI.ui" Generator.py`) so there's also a GUI.ui folder in the dist folder with the .exe in it. But if I call Generator.exe in the PowerShell, it throws *PermissionError: [Errno 13] Permission denied: 'GUI.ui'* – Seneo Jan 05 '18 at 01:02

1 Answers1

1

I made this an answer show I could show an example. Use the pyuic tool to convert the .ui file to a .py file. The .py file this creates will have a class in it that is the name of the widget built in Qt Designer. Import and subclass this class into the GUI class that you are creating.

from designer_file import Ui_Gui  # Designer file is the converted .ui file and Ui_Gui is the ui class it created

class GUIWindow(QtWidgets.QWidget, Ui_Gui):

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)  # This is necessary to setup the ui when using this method
        # Code here...
MalloyDelacroix
  • 2,193
  • 1
  • 13
  • 17
  • I'm sorry but I seem to be stuck somehow. If I try this solution, PyCharm tells me *Unresolved reference 'py'* And also, if I import the GUI file, would the *Generator.exe* even work as soon as it is generated, cause the *GUI.py* isn't part of the standard libraries? – Seneo Jan 04 '18 at 23:14
  • Sorry, my mistake. Remove the .py from designer_file.py. Yes it should work as soon as it's generated. Pyinstaller searches the imports in each file and pulls in _almost_ everything thats needed. It should catch that import fine. – MalloyDelacroix Jan 04 '18 at 23:31
  • Now the import worked and I added the rest to the initiator. However, when I run this code in PyCharm, I receive *AttributeError: 'Dialog' object has no attribute 'setup_ui'* – Seneo Jan 04 '18 at 23:38
  • Sorry, another typo. self.setupUi(self) – MalloyDelacroix Jan 04 '18 at 23:40
  • I moved *Generator.py* and *GUI.py* into a new Folder and ran `pyinstaller.exe -F Generator.py`. It compiles, but when i run the .exe I get a console which closes immediately which has written in it; *This application failed to start, because it could not find or load the Qt platform plugin "windows" in "". Reinstalling the application may fix this problem* – Seneo Jan 04 '18 at 23:51
  • This is the same issue as in this questions [https://stackoverflow.com/q/48007100/6594646](https://stackoverflow.com/q/48007100/6594646). That user found a temporary solution that worked on their computer. I have not encountered this error before and am not sure how to completely fix it. – MalloyDelacroix Jan 04 '18 at 23:58
  • This link solved the previous error but now I've got the same problem like in the beginning again. The .exe just pops up a command window and closes again – Seneo Jan 05 '18 at 00:08
  • Seems like there is a problem somewhere else in the code. As Eric suggested above, if you run it from the command prompt you can see the error. – MalloyDelacroix Jan 05 '18 at 00:15
  • The error seems to be in the code. In *Generator.py* I use `self.ui = uic.loadUi('GUI.ui', self)` and later I refer to self.ui for examples for the buttons (`self.ui.buttonGenerate.clicked.connect(self.onGenerate)`). How do i solve this reference? if i comment out the load.ui and replace self.ui with `Ui_Generator.buttonGenerate.clicked.connect(self.onGenerate)` it keeps telling me, *'Ui_Generator' has no attribute 'buttonGenerate'* even though it's there, also in the GUI.py – Seneo Jan 05 '18 at 00:29
  • Since you are subclassing the Ui_Gui class you don't need to declare self.ui. To access the buttons or any other widget items just use self. `self.buttonGenerate.clicked.connect(self.onGenerate)` – MalloyDelacroix Jan 05 '18 at 02:50
  • So I removed the .ui from every self.ui and commented the out the declaration of self.ui. Now the program runs and if I copy *GUI.ui* and *the platforms folder* (of dist/Generator/PyQt5/Qt/plugins) both *into the Generator) it works. However it just shows a blank form without any controls visible on it – Seneo Jan 05 '18 at 11:34
  • solved it, i still had the `uper().__init__(parent)` in it. Now it works, thanks a ton! – Seneo Jan 05 '18 at 14:49