123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import sys
- import shutil
- from PyQt5.QtGui import QIcon
- from cryptography.fernet import Fernet
- from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog, QVBoxLayout, QLabel, QMessageBox
- class FileDialogExample(QWidget):
- def __init__(self):
- super().__init__()
- # 设置窗口
- self.setWindowTitle("许可更新管理器")
- self.setGeometry(100, 100, 300, 150)
- # 设置窗口图标
- self.setWindowIcon(QIcon("icon.png")) # 使用你自己的图标文件路径
- self.resize(300, 120)
- # 创建布局
- layout = QVBoxLayout()
- # 创建标签
- self.label = QLabel("请选择一个许可文件", self)
- self.label.setWordWrap(True)
- layout.addWidget(self.label)
- # 创建选择文件按钮
- self.select_file_button = QPushButton("选择文件", self)
- self.select_file_button.setFixedSize(60, 25)
- self.select_file_button.clicked.connect(self.select_file)
- layout.addWidget(self.select_file_button)
- # 创建确定按钮
- self.ok_button = QPushButton("确定", self)
- self.ok_button.setFixedSize(60, 25)
- self.ok_button.clicked.connect(self.ok_button_clicked)
- layout.addWidget(self.ok_button)
- # 设置窗口布局
- self.setLayout(layout)
- def copy_file(self, src_path, dst_path):
- shutil.copy(src_path, dst_path)
- def select_file(self):
- # 打开文件对话框选择文件
- options = QFileDialog.Options()
- file, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "Text Files (*.txt);;All Files (*)",
- options=options)
- if file:
- print(file)
- try:
- self.selected_file = file # 保存选择的文件路径
- licensefile = file
- # 打开文件并读取内容
- text = []
- with open(licensefile, 'r') as file:
- for line in file:
- text.append(line.replace('\n', ' '))
- # 打印文件内容
- print(text)
- # 尝试执行的代码
- fernet = Fernet(text[0].encode("utf-8"))
- decrypted_message = fernet.decrypt(text[1].encode("utf-8"))
- license = decrypted_message.decode('utf-8')
- licenseDict = self.str_to_dict(license)
- host = licenseDict["host"]
- date = licenseDict["license"]
- # QMessageBox.information(self, "提示信息", f"许可文件解析成功,授权机器名:{host},授权到期时间:{date}")
- self.label.setStyleSheet("color: green;") # 这里 #999999 是一种浅灰色
- self.label.setText("许可文件解析成功,授权机器名:{},授权到期时间:{}。".format(host, date))
- except Exception as e:
- del self.selected_file # 保存选择的文件路径
- # 处理异常的代码
- # QMessageBox.critical(None, 'Error', "许可文件解析失败,请检查!")
- self.label.setStyleSheet("color: red;") # 这里 #999999 是一种浅灰色
- self.label.setText("许可文件解析失败,请检查!")
- def ok_button_clicked(self):
- # 执行业务逻辑
- if hasattr(self, 'selected_file'):
- licensefile = "license.txt"
- self.copy_file(self.selected_file, licensefile)
- # 打开文件并读取内容
- # text = []
- # with open(licensefile, 'r') as file:
- # for line in file:
- # text.append(line.replace('\n', ' '))
- # # 打印文件内容
- # print(text)
- # # 尝试执行的代码
- # fernet = Fernet(text[0].encode("utf-8"))
- # decrypted_message = fernet.decrypt(text[1].encode("utf-8"))
- # license = decrypted_message.decode('utf-8')
- # licenseDict = self.str_to_dict(license)
- # host = licenseDict["host"]
- # date = licenseDict["license"]
- # QMessageBox.information(self, "提示信息", f"许可文件更新成功,授权机器名:{host},授权到期时间:{date}")
- QMessageBox.information(self, "提示信息", f"许可文件更新成功")
- # # 假设这里是业务逻辑:读取文件内容
- # try:
- # with open(self.selected_file, 'r') as f:
- # content = f.read()
- # print(f"文件内容: {content}")
- # except Exception as e:
- # print(f"读取文件失败: {e}")
- else:
- QMessageBox.information(self, "提示信息", "请选择许可文件")
- def str_to_dict(self, string):
- # 去除首尾的花括号
- string = string.strip("{}")
- # 分割每个键值对
- pairs = string.split(", ")
- # 创建空字典
- result = {}
- # 遍历键值对并添加到字典中
- for pair in pairs:
- key, value = pair.split(": ")
- # 去除键和值两边的引号
- key = key.strip("'")
- value = value.strip("'")
- # 添加到字典中
- result[key] = value
- return result
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- window = FileDialogExample()
- window.show()
- sys.exit(app.exec_())
|