From d54479f42a60f10dc8932dcdd61d310d38a06111 Mon Sep 17 00:00:00 2001 From: zhaoyafan Date: Tue, 22 Oct 2024 09:55:44 +0800 Subject: [PATCH] 20241022095500 --- main.py | 66 +++++++++++++++++++++++++++++++++++++++++++++-- requirements.txt | Bin 276 -> 620 bytes 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 58a7bd5..340bc64 100644 --- a/main.py +++ b/main.py @@ -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_()) diff --git a/requirements.txt b/requirements.txt index af5698ff1aba915e5d324b8e365f8a795e212a3e..661663e4d1fecd509c585260786c3f55c49cfe0c 100644 GIT binary patch literal 620 zcmZva%}&EW41|3~;!#p1p($LrazH`?ao~v3(n1ADSqo@*}uHMRWkZCbc~wSsjxHlSWSBN_JZb)SaTOI;xkSu66c*79pB}I z=aZi$eucD0FPh5*E9%fx8XUL&PI=+y)5Ln8R$v-$tTe4Tj5w_;@EZN`^>0-TZw7P+ zG^@wb9?8goreFK1it4HPMZXRCo#UN7yXM|TGGfMGAu+Gr*Op4jbJRX}?Kk8Dx<)kI jbKg)a)sVx=Sr6W!wFBo!KQWSvSm!{Ofk;R>{a?NT+BsLW delta 7 OcmaFEGKFb_2qORsxB^T7