20241022095500

This commit is contained in:
zhaoyafan 2024-10-22 09:55:44 +08:00
parent c2427efb42
commit d54479f42a
2 changed files with 64 additions and 2 deletions

66
main.py
View File

@ -2,9 +2,11 @@ import os
import re
import io
import sys
import threading
import clr
import time
import json
import queue
import shutil
import sqlite3
import logging
@ -19,6 +21,20 @@ from PyQt5.QtCore import Qt, QCoreApplication, QTimer, QPropertyAnimation
from PyQt5.QtGui import QPixmap, QIntValidator
from threading import RLock
from flask import Flask, request
flask_app = Flask(__name__)
def _flask_thread():
try:
flask_app.run(host='0.0.0.0', port=7746)
except:
pass
flask_thread = threading.Thread(target=_flask_thread)
sys.path.append(os.path.dirname(__file__))
clr.AddReference('BarTender')
try:
@ -27,6 +43,17 @@ except Exception:
pass
def killThread(thread):
import ctypes
try:
if thread.is_alive():
return ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread.ident), ctypes.py_object(BaseException)) == 1
else:
return True
except BaseException:
pass
def cp(src: str, dst: str):
"""
Support file or directory.
@ -559,7 +586,7 @@ class MainWindow(QMainWindow):
def __init__(self, logger: Logger):
super().__init__()
self.app_name = '标签打印'
self.app_version = ('1.0.4', '20241019', 'zhaoyafan', 'zhaoyafan@foxmail.com', 'https://www.fanscloud.net/')
self.app_version = ('1.0.5', '20241022', 'zhaoyafan', 'zhaoyafan@foxmail.com', 'https://www.fanscloud.net/')
self.logger = logger
self.toast = ToastNotification()
self.setting = Setting(
@ -573,6 +600,10 @@ class MainWindow(QMainWindow):
self.last_opened_template = ['', '']
self.input_convert_letter = 0
self.input_scan_prohibited_enter = 0
self.task_queue = queue.Queue()
self.task_timer = QTimer(self)
self.task_timer.timeout.connect(self.task_execute)
self.task_timer.start(100)
self.setWindowTitle(self.app_name)
self.setGeometry(0, 0, 600, 400)
self.statusBar()
@ -707,8 +738,24 @@ class MainWindow(QMainWindow):
def closeEvent(self, event):
self.bt.quit()
killThread(flask_thread)
event.accept()
def task_push(self, func, args):
self.task_queue.put([func, args])
def task_execute(self):
while True:
if not self.task_queue.empty():
task = self.task_queue.get()
try:
task[0](*task[1])
except:
pass
self.task_queue.task_done()
else:
break
def load_setting(self):
# load blockRepeat
self.blockRepeatAction.setChecked(bool(self.setting['blockRepeat']))
@ -941,12 +988,14 @@ class MainWindow(QMainWindow):
def on_print_scan_enter(self):
self.input_scan_prohibited_enter or self.on_start_printing()
def on_start_printing(self):
def on_start_printing(self, content=None):
edit = self.edtSCN
if (self.input_convert_letter == 1) == 1:
text = edit.text().strip().upper()
else:
text = edit.text().strip()
if (not content) == 0:
text = str(content).strip()
edit.setText(text)
if (self.bt.bt_format is None) == 1:
self.toast.show_toast('未加载模板文件')
@ -1051,4 +1100,17 @@ if __name__ == '__main__':
sys.exit(1)
app = QApplication(sys.argv)
window_main = MainWindow(logger=Logger(name='main', fh=None, ch=LoggerConsHandler()))
@flask_app.route('/print', methods=['POST'])
def api_print():
content = request.json['data']
if (isinstance(content, str) is True) == 1:
threading.Thread(target=window_main.task_push, args=[window_main.on_start_printing, [content]]).start()
return 'Printing task sent'
else:
return 'Error'
flask_thread.start()
sys.exit(app.exec_())

Binary file not shown.