Search code examples
python-3.xpyqt5pyinstallercx-freeze

Running the converted py to exe file shows a console and not the designed GUI


I want to create a GUI EXE file I am creating a a GUI using python 3.6 and PyQt5, After running .py file I get the design that I worked on. However when i convert the .py file to an exe using Cx_Freeze or pyinstaller, then I run the EXE file, GUI doesn't show and a console shows instead.

import sys 
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi

completed = 0
accumulate = 0
class Main(QMainWindow):
    def __init__(self):
        super(Main, self).__init__()
        loadUi('progress.ui',self)
        self.setWindowTitle('Greeting')
        self.progressBar.setValue(0)
        self.increase.clicked.connect(self.increase_by10)
        self.reset.clicked.connect(self.resetprogress)

    @pyqtSlot()
    def increase_by10(self):
        global accumulate
        accumulate += 10
        if accumulate <= 100:
                self.progressBar.setValue(accumulate)

    @pyqtSlot()
    def resetprogress(self):
        global accumulate
        accumulate = 0
        self.progressBar.setValue(accumulate)
        
      
              
app = QApplication(sys.argv)
widget = Main()
widget.show()
sys.exit(app.exec_())

design

running exe file shows a console

  • running .py file
  • running .exe converted from .py

Solution

    1. ...\Python\Scripts\pyuic5.exe progress.ui -o progress.py -x
    2. + (progress.py) # - (progress.ui)

    3. pyinstaller --onefile --noconsole main.py
    4. main.exe

    main.py

    import sys 
    from PyQt5.QtCore import pyqtSlot, QThread       #++++++++ QThread
    from PyQt5.QtWidgets import QApplication, QMainWindow
    #from PyQt5.uic import loadUi                     #--------
    
    import progress                                  #++++++++
    
    completed = 0
    accumulate = 0
    class Main(QMainWindow, progress.Ui_MainWindow): #++++++++ progress.Ui_MainWindow
        def __init__(self):
            super(Main, self).__init__()
    
            self.setupUi(self)                       #+++++++++
            #loadUi('progress.ui',self)              #---------
    
            self.setWindowTitle('Greeting')
            self.progressBar.setValue(0)
            self.increase.clicked.connect(self.increase_by10)
            self.reset.clicked.connect(self.resetprogress)
    
        @pyqtSlot()
        def increase_by10(self):
            global accumulate
            #accumulate += 10                              #-----
            while accumulate <= 100:                       #+++++ 
                self.progressBar.setValue(accumulate)
                QThread.msleep(1000)                       #+++++
                accumulate += 10                           #+++++
    
    
        @pyqtSlot()
        def resetprogress(self):
            global accumulate
            accumulate = 0
            self.progressBar.setValue(accumulate)
    
    app = QApplication(sys.argv)
    widget = Main()
    widget.show()
    sys.exit(app.exec_())