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

问题描述 投票:0回答:1

我想创建一个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文件
  • 运行从.py转换的.exe
python-3.x pyqt5 pyinstaller cx-freeze
1个回答
1
投票
  1. ... \ Python \ Scripts \ pyuic5.exe progress.ui -o progress.py -x
  2. +(Progress.pi)# - (Progress.w)
  3. pyinstaller --onefile --noconsole main.py
  4. MAIN.EXE

卖弄.朋友

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_())
© www.soinside.com 2019 - 2024. All rights reserved.