Skip to content

Instantly share code, notes, and snippets.

@jamil666
Created February 26, 2020 13:36
Show Gist options
  • Save jamil666/69a2d49c8383059e1f7772b573597315 to your computer and use it in GitHub Desktop.
Save jamil666/69a2d49c8383059e1f7772b573597315 to your computer and use it in GitHub Desktop.
Convert Python to EXE file
from PyQt5 import QtCore, QtGui, QtWidgets
import os
class Ui_MainForm(object):
# Select Python file
def open_python_file(self):
directory = QtWidgets.QFileDialog.getOpenFileName(directory="C:\\", filter="*.py")
self.InputFile.setText(directory[0])
# Select save dir
def save_exe_file(self):
fsave = QtWidgets.QFileDialog.getSaveFileName(directory="C:\\")
self.SaveFile.setText(fsave[0])
# Select Icon file
def open_icon_file(self):
iconfile = QtWidgets.QFileDialog.getOpenFileName(directory="C:\\", filter="*.ico")
self.IconFile.setText(iconfile[0])
# Start export to EXE process
def StartExport(self):
# If Console checked, but Icon not selected
if self.ShowConsole.isChecked() == True and self.IconFile.text() == 'Icon File (Optional)':
os.system('pyinstaller --console --onefile {} --distpath {}'.format(self.InputFile.text(), self.SaveFile.text()))
# If Console not checked, but Icon not selected
if self.ShowConsole.isChecked() == False and self.IconFile.text() == 'Icon File (Optional)':
os.system('pyinstaller --noconsole --onefile {} --distpath {}'.format(self.InputFile.text(), self.SaveFile.text()))
# If Console checked and Icon file selected
if self.ShowConsole.isChecked() == True and self.IconFile.text() != 'Icon File (Optional)':
os.system('pyinstaller -i="{}" --console --onefile {} --distpath {}'.format(self.IconFile.text(),self.InputFile.text(), self.SaveFile.text()))
# If Console not checked and Icon not selected
if self.ShowConsole.isChecked() == False and self.IconFile.text() != 'Icon File (Optional)':
os.system('pyinstaller -i="{}" --noconsole --onefile {} --distpath {}'.format(self.IconFile.text(),self.InputFile.text(), self.SaveFile.text()))
def setupUi(self, MainForm):
MainForm.setObjectName("MainForm")
MainForm.resize(333, 133)
MainForm.setMinimumSize(QtCore.QSize(333, 133))
MainForm.setMaximumSize(QtCore.QSize(333, 133))
self.ShowConsole = QtWidgets.QRadioButton(MainForm)
self.ShowConsole.setGeometry(QtCore.QRect(10, 100, 71, 16))
self.ShowConsole.setObjectName("ShowConsole")
self.StartButton = QtWidgets.QPushButton(MainForm)
self.StartButton.setGeometry(QtCore.QRect(90, 100, 231, 20))
self.StartButton.setObjectName("StartButton")
self.StartButton.clicked.connect(self.StartExport)
self.widget = QtWidgets.QWidget(MainForm)
self.widget.setGeometry(QtCore.QRect(10, 10, 311, 81))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.InputFile = QtWidgets.QLineEdit(self.widget)
self.InputFile.setObjectName("InputFile")
self.gridLayout.addWidget(self.InputFile, 0, 0, 1, 1)
self.InoutFileButton = QtWidgets.QToolButton(self.widget)
self.InoutFileButton.setObjectName("InoutFileButton")
self.gridLayout.addWidget(self.InoutFileButton, 0, 1, 1, 1)
self.InoutFileButton.clicked.connect(self.open_python_file)
self.SaveFile = QtWidgets.QLineEdit(self.widget)
self.SaveFile.setObjectName("SaveFile")
self.gridLayout.addWidget(self.SaveFile, 1, 0, 1, 1)
self.SaveFileButton = QtWidgets.QToolButton(self.widget)
self.SaveFileButton.setObjectName("SaveFileButton")
self.gridLayout.addWidget(self.SaveFileButton, 1, 1, 1, 1)
self.SaveFileButton.clicked.connect(self.save_exe_file)
self.IconFile = QtWidgets.QLineEdit(self.widget)
self.IconFile.setObjectName("IconFile")
self.gridLayout.addWidget(self.IconFile, 2, 0, 1, 1)
self.IconFileButton = QtWidgets.QToolButton(self.widget)
self.IconFileButton.setObjectName("IconFileButton")
self.gridLayout.addWidget(self.IconFileButton, 2, 1, 1, 1)
self.IconFileButton.clicked.connect(self.open_icon_file)
self.retranslateUi(MainForm)
QtCore.QMetaObject.connectSlotsByName(MainForm)
def retranslateUi(self, MainForm):
_translate = QtCore.QCoreApplication.translate
MainForm.setWindowTitle(_translate("MainForm", "Py to EXE"))
self.ShowConsole.setText(_translate("MainForm", "Console"))
self.StartButton.setText(_translate("MainForm", "Start"))
self.InputFile.setText(_translate("MainForm", "Python File"))
self.InoutFileButton.setText(_translate("MainForm", "..."))
self.SaveFile.setText(_translate("MainForm", "Save to Directory"))
self.SaveFileButton.setText(_translate("MainForm", "..."))
self.IconFile.setText(_translate("MainForm", "Icon File (Optional)"))
self.IconFileButton.setText(_translate("MainForm", "..."))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainForm = QtWidgets.QDialog()
ui = Ui_MainForm()
ui.setupUi(MainForm)
MainForm.show()
sys.exit(app.exec_())
@jamil666
Copy link
Author

Requirements:

  1. PyQT5
  2. Pyinstaller

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment