本帖最后由 xptk9074 于 2024-9-18 16:32 编辑
本地用调试正常,可以用,但是编译后就无法打开
以下是代码,求指点哪里错了。
pyinstaller --onefile --hidden-import=PyQt5.QtCore --hidden-import=PyQt5.QtWidgets --hidden-import=PyQt5.QtGui --hidden-import=sqlite3 目的样.py
pyinstaller --onefile 目的样.py
配置 Visual Studio Code 的任务(tasks.json
[JavaScript] 纯文本查看 复制代码 {
"version": "2.0.0",
"tasks": [
{
"label": "Compile to exe",
"type": "shell",
"command": "pyinstaller --onefile your_script.py",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
编译成功后都打不开
[Python] 纯文本查看 复制代码 import sqlite3
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTableWidget, QTableWidgetItem, QPushButton, QHBoxLayout, QLabel, QLineEdit, QComboBox, QScrollArea, QMessageBox
from PyQt5.QtCore import Qt
page_size =28
current_page = 0
def query_data(page):
try:
conn = sqlite3.connect('C:/Users/TPM/Desktop/VCMDataII.db')
cursor = conn.cursor()
offset = page * page_size
selected_option1 = option_combo1.currentText()
selected_option2 = option_combo2.currentText()
query = f"SELECT * FROM T_LV2 WHERE Attribute3 LIKE '%{selected_option2}%' AND BatchId LIKE '%{selected_option1}%' LIMIT {page_size} OFFSET {offset};"
print(f"Executing query: {query}")
cursor.execute(query)
results = cursor.fetchall()
conn.close()
table.clearContents()
table.setRowCount (0)
if len(results) > 0:
for row_num, row_data in enumerate(results):
table.insertRow(row_num)
for col_num, data in enumerate(row_data):
item = QTableWidgetItem(str(data))
item.setTextAlignment(Qt.AlignCenter)
table.setItem(row_num, col_num, item)
# 计算并显示数量统计
total_count = len(results) / 28
count_label.setText(f"统计托数:{total_count}")
else:
QMessageBox.information(widget, "提示", "未查到相关数据。")
count_label.setText("统计托数:0")
except sqlite3.DatabaseError as e:
QMessageBox.information(widget, "提示", "查询为空或数据库错误。")
table.clearContents()
table.setRowCount(0)
count_label.setText("统计托数:0")
def next_page():
global current_page
current_page += 1
query_data(current_page)
def prev_page():
global current_page
if current_page > 0:
current_page -= 1
query_data(current_page)
app = QApplication([])
widget = QWidget()
widget.setWindowTitle("目的样查询工具")
widget.resize(600, 400)
layout = QVBoxLayout()
# 创建第一个下拉框(从 T_BatchInfo 表中 BatchId 获取数据)
try:
conn = sqlite3.connect('C:/Users/TPM/Desktop/VCMDataII.db')
cursor = conn.cursor()
cursor.execute("SELECT DISTINCT BatchId FROM T_BatchInfo")
batch_ids = [row[0] for row in cursor.fetchall()]
conn.close()
except sqlite3.DatabaseError as e:
batch_ids = []
option_label1 = QLabel("")
option_combo1 = QComboBox()
option_combo1.addItems(batch_ids)
option_hbox1 = QHBoxLayout()
option_hbox1.addWidget(option_label1)
option_hbox1.addWidget(option_combo1)
layout.addLayout(option_hbox1)
# 创建第二个下拉框(原目的样分类下拉框)
option_label2 = QLabel("选择目的样分类:")
option_combo2 = QComboBox()
option_combo2.addItems(['包', 'P ', '接', '开', '停'])
option_hbox2 = QHBoxLayout()
option_hbox2.addWidget(option_label2)
option_hbox2.addWidget(option_combo2)
layout.addLayout(option_hbox2)
# 创建查询按钮
query_button = QPushButton("查询")
query_button.clicked.connect(lambda: query_data(current_page))
layout.addWidget(query_button)
# 创建滚动区域
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
table_widget = QWidget()
table_layout = QVBoxLayout(table_widget)
table = QTableWidget()
table.setColumnCount(13)
columns = ('箱', '托', '单', '号', '码', '传', 日期', '期', '上传', '虚码', '参数一', '参数二', '目的样类别')
table.setHorizontalHeaderLabels(columns)
for col in range(13):
table.horizontalHeaderItem(col).setTextAlignment(Qt.AlignCenter)
table.setColumnWidth(col, 80)
table_layout.addWidget(table)
scroll_area.setWidget(table_widget)
layout.addWidget(scroll_area)
# 创建数量统计标签
count_label = QLabel("统计托数:0")
layout.addWidget(count_label)
# 创建下一页和上一页按钮
next_page_button = QPushButton("下一页")
next_page_button.clicked.connect(next_page)
prev_page_button = QPushButton("上一页")
prev_page_button.clicked.connect(prev_page)
page_hbox = QHBoxLayout()
page_hbox.addWidget(prev_page_button)
page_hbox.addWidget(next_page_button)
layout.addLayout(page_hbox)
widget.setLayout(layout)
widget.show()
app.exec_()
import time
|