# -*- coding: utf-8 -*- ''' @Project :Scan @File :ScanInput.py @Author :Leslie @Date :2023/5/6 9:03 ''' import time from PyQt5.QtCore import QCoreApplication from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QFileDialog, QMessageBox import Config from Config import Configuration from Dialog.Dialogadd import Dialogadd from ui.Scan import Ui_Form softwareIconPath = r'./pic/scan.png' folderName = 'material' # softwareIconPath = r'./pic/scan.png' from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * # class WorkThread(QThread): # sinOut = pyqtSignal(str) # def run(self): # while True: # time.sleep(5) # self.sinOut.emit("1111run") class ScanInput(QWidget): def __init__(self, versionCode='sss',productDate="aaaa",directory="C:/Users/lili/Desktop/material",parent=None, closeToDlg=None): super(ScanInput, self).__init__(parent) self.ui = Ui_Form() self.ui.setupUi(self) self.setWindowTitle("物料扫码") self.setWindowIcon(QIcon(softwareIconPath)) self.directory = directory self.ui.versionCode.setText(versionCode) self.ui.Deliverydate.setText(productDate) self.QComboBoxnit() # self.ui.save.clicked.connect(self.actSaveTriggered) self.ui.open.clicked.connect(self.actOpenTriggered) self.ui.materialType.currentTextChanged.connect(self.materialTypeChanged) QMessageBox.information(self, '提示', '请在英文输入法状态下进行扫码操作') # 创建保存.txt的文件夹 # self.mkdirFile() # 线程传参 self.autoSave = AutoSaveThread(self,directory) self.autoSave.start() # 下拉框属性 def getParamDict(self): paramDict = Config.Configuration().getConfiguration() return paramDict # 下拉框初始化 def QComboBoxnit(self): paramDict = self.getParamDict() for item in paramDict: item = str(item) self.ui.materialType.addItem(item) # 打开文件 def actOpenTriggered(self): curPath = QCoreApplication.applicationDirPath() QMessageBox.information(self, '提示', '请选择以版本代码+发料日期为名的.txt文件') # 调用打开文件对话框打开一个文件 QFileName = QFileDialog.getOpenFileName(self, "选取文件", curPath, "文本文件(*.txt)") if QFileName[0]: fileName = QFileName[0].split("/")[-1].split(".")[0].split("_") if len(fileName)==2: versionCode = fileName[0] deliveryDate = fileName[1] self.ui.Deliverydate.setText(deliveryDate) self.ui.versionCode.setText(versionCode) f = open(QFileName[0], 'r',encoding='utf-8') with f: data = f.read() self.ui.plainTextEdit.setPlainText(data) else: QMessageBox.critical(self,'错误','请选择以版本代码+发料日期为名的.txt文件') # 文件另存为 def actSaveTriggered(self): desktopPath = Configuration().get_desktop() versionCode = self.ui.versionCode.text() Deliverydate = self.ui.Deliverydate.text() filenameStr = versionCode + "_" + Deliverydate + ".txt" filePathName = desktopPath + '\\' + folderName + '\\' + filenameStr aFileName = QFileDialog.getSaveFileName(self, "另存为", filePathName, "文本文档(*.txt)") text = self.ui.plainTextEdit.toPlainText() if str(aFileName[0]) == "": QMessageBox.warning(self, "提示", "没有保存数据,请重新保存。") saveFlag = 0 return aFileName, saveFlag else: saveFlag = 1 try: with open(aFileName[0], 'w') as f: f.write(text) except Exception as e: QMessageBox.information(self, "提示", str(e), ) QMessageBox.information(self, "提示", "数据文件保存成功!", QMessageBox.Ok) return aFileName, saveFlag # 物料类别选择 def materialTypeChanged(self, materialType): if not materialType == '无': str = "= = = = = = = = = = = = " + materialType + " = = = = = = = = = = = = =" self.ui.plainTextEdit.insertPlainText(str +"\n") self.ui.plainTextEdit.insertPlainText("\n") # 创建保存文件的文件夹 # def mkdirFile(self): # # desktopPath = Configuration().get_desktop() # # folderNamePath = desktopPath + '\\' + folderName # # if not os.path.exists(folderNamePath): # os.mkdir(folderNamePath) # else: # return True # 添加物料类别的界面 目前没用 def openDialog(self): dialog = Dialogadd(self) # 连接【子窗口内置消息和主窗口的槽函数】 # dialog.datetime.dateChanged.connect(self.slot_inner) # 连接【子窗口自定义消息和主窗口槽函数】 dialog.dialogSignel.connect(self.slot_emit) dialog.show() def closeThread(self): self.autoSave.stop() def closeEvent(self, event): # # desktopPath = Configuration().get_desktop() # versionCode = self.ui.versionCode.text() deliveryDate = self.ui.Deliverydate.text() filenameStr = versionCode + "_" + deliveryDate + ".txt" filePath = self.directory filePathName = filePath + '/' + filenameStr result = QMessageBox.question(self, "注意:", "将文件保存到"+filePathName, QMessageBox.Ok) if result == QMessageBox.Ok: txt = self.ui.plainTextEdit.toPlainText() file = open(filePathName, 'w',encoding="utf-8") file.write(txt) file.close() #关闭线程 self.autoSave.terminate() # 结束此进程 self.autoSave.wait() # 等待结束完成 if self.autoSave.isFinished(): # 如果当前线程已经完成工作,则删除 del self.autoSave event.accept() else: event.ignore() # QMessageBox.information(self, "消息", "谢谢!") # 自动保存 线程任务 class AutoSaveThread(QThread): # 初始化传参 def __init__(self, inputDiog,directory,parent=None): super(AutoSaveThread, self).__init__(parent) self.inputDiog = inputDiog self.versionCode = inputDiog.ui.versionCode.text() self.deliveryDate = inputDiog.ui.Deliverydate.text() self.directory = directory def run(self): self.loop_monitor() def time_printer(self): # 定时 if self.inputDiog.ui.plainTextEdit.toPlainText(): # desktopPath = Configuration().get_desktop() versionCode = self.versionCode deliveryDate = self.deliveryDate filePath = self.directory filenameStr = versionCode+"_"+deliveryDate + ".txt" # filePathName = filePath + '\\' + folderName + '\\' + filenameStr filePathName = filePath + '/' + filenameStr txt = self.inputDiog.ui.plainTextEdit.toPlainText() file = open(filePathName, 'w',encoding="utf-8") file.write(txt) file.close() self.loop_monitor() else: self.loop_monitor() def loop_monitor(self): time.sleep(60*5) self.time_printer() if __name__ == '__main__': import sys app = QApplication(sys.argv) ui = ScanInput() ui.show() sys.exit(app.exec_())