运行转换后的py到exe文件显示控制台而不是设计的GUI

时间:2018-03-09 14:17:52

标签: python-3.x pyqt5 pyinstaller cx-freeze

我想创建一个GUI EXE文件 我正在使用python 3.6和PyQt5创建一个GUI。在运行.py文件后,我得到了我工作的设计。 但是,当我使用Cx_Freeze或pyinstaller将.py文件转换为exe时,我运行EXE文件,GUI不显示,而控制台则显示。

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

  • 正在运行.py文件
  • 运行.exe从.py转换。

1 个答案:

答案 0 :(得分:1)

  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
  5. 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_())