ScanInput.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # coding:utf8
  2. '''
  3. @Project :Scan
  4. @File :ScanInput.py
  5. @Author :Leslie
  6. @Date :2023/5/6 9:03
  7. '''
  8. import datetime
  9. import os
  10. from PyQt5.QtCore import *
  11. import time
  12. from PyQt5.QtCore import QCoreApplication, QThread, pyqtSignal, QDate
  13. from PyQt5.QtGui import QIcon
  14. from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QPlainTextEdit, QMessageBox
  15. import CRC8
  16. from ScanUtils import Config
  17. from ScanUtils.Config import Configuration
  18. from ScanUtils.Dialogadd import Dialogadd
  19. from ScanUtils.ui.Scan import Ui_Form
  20. from VersionSet import VersionSet
  21. softwareIconPath = r'./pic/scan.png'
  22. folderName = 'material'
  23. # softwareIconPath = r'./pic/scan.png'
  24. from PyQt5.QtCore import *
  25. from PyQt5.QtGui import *
  26. from PyQt5.QtWidgets import *
  27. # class WorkThread(QThread):
  28. # sinOut = pyqtSignal(str)
  29. # def run(self):
  30. # while True:
  31. # time.sleep(5)
  32. # self.sinOut.emit("1111run")
  33. class ScanInput(QWidget):
  34. def __init__(self, versionCode,productDate, parent=None, closeToDlg=None):
  35. super(ScanInput, self).__init__(parent)
  36. self.ui = Ui_Form()
  37. self.ui.setupUi(self)
  38. self.setWindowTitle("物料扫码")
  39. self.setWindowIcon(QIcon(softwareIconPath))
  40. self.ui.versionCode.setText(versionCode)
  41. self.ui.Deliverydate.setText(productDate)
  42. self.QComboBoxnit()
  43. self.ui.save.clicked.connect(self.actSaveTriggered)
  44. self.ui.open.clicked.connect(self.actOpenTriggered)
  45. self.ui.materialType.currentTextChanged.connect(self.materialTypeChanged)
  46. # 创建保存.txt的文件夹
  47. self.mkdirFile()
  48. # 线程传参
  49. self.autoSave = AutoSaveThread(self)
  50. self.autoSave.start()
  51. # 下拉框属性
  52. def getParamDict(self):
  53. paramDict = Config.Configuration().getConfiguration()
  54. return paramDict
  55. # 下拉框初始化
  56. def QComboBoxnit(self):
  57. paramDict = self.getParamDict()
  58. for item in paramDict:
  59. self.ui.materialType.addItem(item)
  60. # 打开文件
  61. def actOpenTriggered(self):
  62. curPath = QCoreApplication.applicationDirPath()
  63. QMessageBox.information(self, '提示', '请选择以版本代码+发料日期为名的.txt文件')
  64. # 调用打开文件对话框打开一个文件
  65. QFileName = QFileDialog.getOpenFileName(self, "选取文件", curPath,
  66. "文本文件(*.txt)")
  67. if QFileName[0]:
  68. fileName = QFileName[0].split("/")[-1].split(".")[0].split("_")
  69. versionCode = fileName[0]
  70. deliveryDate = fileName[1]
  71. self.ui.Deliverydate.setText(deliveryDate)
  72. self.ui.versionCode.setText(versionCode)
  73. f = open(QFileName[0], 'r')
  74. with f:
  75. data = f.read()
  76. self.ui.plainTextEdit.setPlainText(data)
  77. # 文件保存
  78. def actSaveTriggered(self):
  79. desktopPath = Configuration().get_desktop()
  80. # print(VersionSet.getCurrentParam())
  81. versionCode = self.ui.versionCode.text()
  82. Deliverydate = self.ui.Deliverydate.text()
  83. filenameStr = versionCode + "_" + Deliverydate + ".txt"
  84. filePathName = desktopPath + '\\' + folderName + '\\' + filenameStr
  85. aFileName = QFileDialog.getSaveFileName(self, "另存为", filePathName, "文本文档(*.txt)")
  86. text = self.ui.plainTextEdit.toPlainText()
  87. if str(aFileName[0]) == "":
  88. QMessageBox.warning(self, "提示", "没有保存数据,请重新保存。")
  89. saveFlag = 0
  90. return aFileName, saveFlag
  91. else:
  92. saveFlag = 1
  93. try:
  94. with open(aFileName[0], 'w') as f:
  95. f.write(text)
  96. except Exception as e:
  97. QMessageBox.information(self, "提示", str(e), )
  98. QMessageBox.information(self, "提示", "数据文件保存成功!", QMessageBox.Ok)
  99. return aFileName, saveFlag
  100. # 物料类别选择
  101. def materialTypeChanged(self, materialType):
  102. print(materialType)
  103. if not materialType == '无':
  104. str = "= = = = = = = = = = = = " + materialType + " = = = = = = = = = = = = ="
  105. self.ui.plainTextEdit.insertPlainText(str + "\n")
  106. self.ui.plainTextEdit.insertPlainText("\n")
  107. # 创建保存文件的文件夹
  108. def mkdirFile(self):
  109. desktopPath = Configuration().get_desktop()
  110. folderNamePath = desktopPath + '\\' + folderName
  111. if not os.path.exists(folderNamePath):
  112. os.mkdir(folderNamePath)
  113. else:
  114. return True
  115. # 添加物料类别的界面 目前没用
  116. def openDialog(self):
  117. dialog = Dialogadd(self)
  118. # 连接【子窗口内置消息和主窗口的槽函数】
  119. # dialog.datetime.dateChanged.connect(self.slot_inner)
  120. # 连接【子窗口自定义消息和主窗口槽函数】
  121. dialog.dialogSignel.connect(self.slot_emit)
  122. dialog.show()
  123. # 自动保存 线程任务
  124. class AutoSaveThread(QThread):
  125. # 初始化传参
  126. def __init__(self, inputDiog,parent=None):
  127. super(AutoSaveThread, self).__init__(parent)
  128. self.inputDiog = inputDiog
  129. self.versionCode = inputDiog.ui.versionCode.text()
  130. self.deliveryDate = inputDiog.ui.Deliverydate.text()
  131. def run(self):
  132. self.loop_monitor()
  133. def time_printer(self):
  134. # 定时
  135. if self.inputDiog.ui.plainTextEdit.toPlainText():
  136. desktopPath = Configuration().get_desktop()
  137. versionCode = self.versionCode
  138. deliveryDate = self.Deliverydate
  139. filenameStr = versionCode+"_"+deliveryDate + ".txt"
  140. filePathName = desktopPath + '\\' + folderName + '\\' + filenameStr
  141. txt = self.inputDiog.ui.plainTextEdit.toPlainText()
  142. file = open(filePathName, 'w')
  143. file.write(txt)
  144. file.close()
  145. self.loop_monitor()
  146. else:
  147. self.loop_monitor()
  148. def loop_monitor(self):
  149. time.sleep(20)
  150. self.time_printer()
  151. if __name__ == '__main__':
  152. import sys
  153. app = QApplication(sys.argv)
  154. ui = ScanInput()
  155. ui.show()
  156. sys.exit(app.exec_())