- 通过QT5-python设计界面
import os
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFormLayout, QRadioButton
from PyQt5.QtWidgets import QFileDialog
from create_lut import LutGenerator
class MyWindow(QtWidgets.QWidget):
def __init__(self):
super(MyWindow, self).__init__()
# 设计两个QPushButton按钮
self.chose_lut_button = QtWidgets.QPushButton()
self.chose_lut_button.setObjectName("GetPathButton")
self.chose_lut_button.setText("选取.cube文件")
self.running_button = QtWidgets.QPushButton()
self.running_button.setObjectName('runningButton')
self.running_button.setText('运行')
# 用于输出代码运行信息
self.log_TextEdit = QtWidgets.QTextEdit()
# 用于保存选取文件路径
self.src_lut_path = None
def main_window(self):
self.setWindowTitle("选取文件")
form_layout = QFormLayout()
# 通过槽函数对于按钮进行响应
form_layout.addRow(self.chose_lut_button)
self.chose_lut_button.setCheckable(True)
self.chose_lut_button.clicked.connect(lambda: self.click_find_file_path(self.chose_lut_button))
form_layout.addRow("日志信息:", self.log_TextEdit)
self.setLayout(form_layout)
# 通过槽函数对于按钮进行响应
form_layout.addRow(self.running_button)
self.running_button.setCheckable(True)
self.running_button.clicked.connect(lambda: self.click_running(self.running_button))
def click_find_file_path(self, button):
# parent,caption为弹出框名字,directory为弹出框路径,filter为选区的文件格数
# 同一个类型的不同格式如xlsx和xls 用空格隔开
filename, filetype = QFileDialog.getOpenFileName(self, "选择lut文件", "./",
"lut Files (*.cube)")
if button.objectName() == "GetPathButton" and button.isChecked():
self.src_lut_path = filename
self.log_TextEdit.append("lut路径为:" + filename)
button.toggle()
def click_running(self, button):
if button.objectName() == 'runningButton' and button.isChecked():
if self.src_lut_path is not None:
print('开始运行')
self.log_TextEdit.append('\n开始运行!')
cwd = os.getcwd()
lut_name = os.path.split(self.src_lut_path)[-1].split('.')[0]
dst_lut_path = os.path.join(cwd, lut_name+'_reverse.cube')
lut_generator = LutGenerator(src_lut_path=self.src_lut_path, dst_lut_path=dst_lut_path)
lut_generator.running()
self.log_TextEdit.append(f'运行成功,生成lut路径为:{dst_lut_path}')
else:
self.log_TextEdit.append('\n请先选择lut文件!')
button.toggle()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main = MyWindow()
main.main_window()
main.show()
sys.exit(app.exec_())
- 通过pyinstaller编译为.exe可执行文件
- 通过pyinstaller进行编译前最好通过anaconda创建一个新的虚拟环境
- 在新的虚拟环境下打开Tenminal,输入命令:
打包为文件夹:
pyinstaller -D -w main.py
打包为一个文件:
pyinstaller -F -w main.py
当终端中出现以下提示说明代码编译成功了!
- 运行成功后在文件夹中会生成build、dst以及后缀为.spec的文件,可执行文件在dst文件夹中。
参考: