pyinstaller加密工具

import base64
import tkinter as tk
from tkinter import scrolledtext, messagebox


class EncryptionApp:

    def __init__(self, root: tk.Tk):
        self.root = root
        self.root.title("加密系统V1.0")
        self.root.geometry("800x700")
        self.root.resizable(False, False)  # 不允许调整大小

        # 加密方式,默认选择1
        self.selected_method = tk.IntVar(value=1)

        self.setup_ui()

    def setup_ui(self):
        """设置界面"""
        # 创建标题标签
        title_label = tk.Label(
            self.root,
            text="加密系统V1.0版本",
            font=("微软雅黑", 16, "bold"),
            fg="#2E86C1"
        )
        title_label.pack(pady=10)

        # 创建加密项按钮
        self.create_method_button()

        # 输入输出区域
        self.setup_io_area()
        
        # 操作按钮
        self.setup_action_buttons()

        # 底部信息
        self.setup_bottom_info()

    def create_method_button(self):
        """创建加密项单选按钮"""

        # 加密方式选择按钮框架
        encrypt_frame = tk.LabelFrame(
            self.root,
            font=("微软雅黑", 11),
            padx=20,
            pady=15
        )
        encrypt_frame.pack(pady=10)

        self.btn_enc1 = tk.Radiobutton(
            encrypt_frame,  # 1. 父容器:放在哪个框架里
            text="Base64加密",  # 2. 显示文本
            variable=self.selected_method,  # 3. 绑定变量(最重要!)
            value=1,  # 4. 这个按钮的值
            command=self.on_method_selected,  # 5. 点击时执行的函数
            indicatoron=0,
            width=15,  # 6. 宽度(字符数)
            height=2,  # 7. 高度(行数)
            font=("微软雅黑", 10),  # 8. 字体
            bg="#E8F4FD",  # 9. 背景色
            activebackground="#D6EAF8",  # 10. 鼠标悬停时的背景色
            relief=tk.RAISED  # 11. 边框样式(凸起效果)
        )
        self.btn_enc1.grid(row=0, column=0, padx=15, pady=5)
        # self.btn_enc1.pack(side=tk.LEFT, padx=5)

        self.btn_enc2 = tk.Radiobutton(
            encrypt_frame,
            text="移位加密",
            variable=self.selected_method,
            value=2,
            command=self.on_method_selected,
            indicatoron=0,
            width=15,
            height=2,
            font=("微软雅黑", 10),
            bg="#E8F4FD",  # 未选中时的颜色
            activebackground="#D6EAF8",
            relief=tk.RAISED
        )
        self.btn_enc2.grid(row=0, column=1, padx=15, pady=5)

        self.btn_enc3 = tk.Radiobutton(
            encrypt_frame,
            text="翻转加密",
            variable=self.selected_method,
            value=3,
            command=self.on_method_selected,
            indicatoron=0,
            width=15,
            height=2,
            font=("微软雅黑", 10),
            bg="#E8F4FD",  # 未选中时的颜色
            activebackground="#D6EAF8",
            relief=tk.RAISED
        )
        self.btn_enc3.grid(row=0, column=2, padx=15, pady=5)

    def on_method_selected(self):
        value = self.selected_method.get()
        dict_enc = {1: "Base64加密", 2: "移位加密", 3: "翻转加密"}
        print(f"机密方式为:{dict_enc.get(value)}")
        self.status_bar.config(text=f"就绪 (当前选择: {dict_enc.get(value)})")

    def setup_io_area(self):
        """输入输出区域"""
        io_frame = tk.Frame(self.root)
        io_frame.pack(
            fill=tk.BOTH,  # 填充方向:水平和垂直都填充
            expand=True,  # 扩展以占用额外空间
            padx=30,  # 左右边距30像素
            pady=20  # 上下边距20像素
        )

        # 配置网格权重
        io_frame.grid_columnconfigure(1, weight=1)  # 列1占1份
        io_frame.grid_rowconfigure(0, weight=1)
        io_frame.grid_rowconfigure(1, weight=1)

        input_label = tk.Label(
            io_frame,
            text="原文输入:",
            font=("微软雅黑", 11, "bold"),
            fg="#2C3E50"
        )
        input_label.grid(
            row=0,  # 第0行
            column=0,  # 第0列
            padx=10,  # 左右边距10像素
            pady=10,  # 上下边距10像素
            sticky="ne"  # 对齐方式:北-东(右上角对齐)
        )

        # 输入文本框
        self.text_area = scrolledtext.ScrolledText(
            io_frame,
            width=50,  # 宽度:50个字符
            height=8,  # 高度:8行文本
            font=("Consolas", 10),  # 字体:Consolas(等宽字体,适合代码)
            wrap=tk.WORD,  # 换行方式:按单词换行
            relief=tk.GROOVE,  # 边框样式:凹槽效果
            bd=2,  # 边框宽度:2像素
            bg="#F7F9F9"  # 背景色:浅灰色
        )
        self.text_area.grid(
            row=0,  # 第0行
            column=1,  # 第1列(与标签同行)
            padx=10,  # 左右边距
            pady=10,  # 上下边距
            sticky="nsew"  # 重要!填充整个单元格
        )
        self.text_area.focus()  # 设置焦点:启动时光标就在输入框

        # ========== 输出区域 ==========

        output_label = tk.Label(
            io_frame,
            text="密文输入:",
            font=("微软雅黑", 11, "bold"),
            fg="#2C3E50"
        )
        output_label.grid(
            row=1,
            column=0,
            padx=10,
            pady=10,
            sticky="ne"
        )

        # 输出文本框(只读)
        self.text_area2 = scrolledtext.ScrolledText(
            io_frame,
            width=50,
            height=8,
            font=("Consolas", 10),
            wrap=tk.WORD,
            relief=tk.GROOVE,
            bg="#EAEDED",
            state=tk.DISABLED
        )
        self.text_area2.grid(
            row=1,
            column=1,
            padx=10,
            pady=10,
            sticky="nsew"
        )

    def setup_action_buttons(self):
        """设置操作按钮"""
        button_frame = tk.Frame(self.root)
        button_frame.pack(pady=15)

        # 加密按钮
        encrypt_button = tk.Button(
            button_frame,
            text="执行加密",
            command=self.encrypt_text,
            width=15,
            height=2,
            bg="#8E44AD",
            fg="white",
            font=("微软雅黑", 11)
        )
        encrypt_button.grid(
            row=0,
            column=0,
            padx=10
        )

        # 清空按钮
        clear_button = tk.Button(
            button_frame,
            text="清空内容",
            command=self.clear_fields,
            width=15,
            height=2,
            bg="#8E44AD",
            fg="white",
            font=("微软雅黑", 11)
        )
        clear_button.grid(
            row=0,
            column=1,
            padx=10
        )

        # 复制按钮
        copy_button = tk.Button(
            button_frame,
            text="复制按钮",
            command=self.copy_to_clipboard,
            width=15,
            height=2,
            bg="#8E44AD",
            fg="white",
            font=("微软雅黑", 11)
        )
        copy_button.grid(
            row=0,
            column=2,
            padx=10
        )

        self.root.bind('Return', lambda e: self.encrypt_text())

    def encrypt_text(self):
        input_text = self.text_area.get("1.0", tk.END).strip()
        if not input_text:
            messagebox.showwarning("警告", "请输入要加密的内容!")

        value = self.selected_method.get()
        print(value, input_text)

        if value == 1:
            encrypted = self.encrypt_method1(input_text)
        elif value == 2:
            encrypted = self.encrypt_method2(input_text)
        elif value == 3:
            encrypted = self.encrypt_method3(input_text)
        else:
            encrypted = "未知加密方式"

        # 临时启用文本框
        self.text_area2.config(state=tk.NORMAL)
        # 清空原有内容
        # "1.0":第1行第0个字符(文本起始位置)
        # tk.END:文本结束位置(特殊常量)
        # delete("1.0", tk.END):删除从开始到结束的所有内容
        self.text_area2.delete("1.0", tk.END)
        self.text_area2.insert("1.0", encrypted)
        self.text_area2.config(state=tk.DISABLED)

        # 更新状态栏
        method_names = {1: "Base64加密", 2: "移位加密", 3: "倒序加密"}
        self.status_bar.config(text=f"加密完成: {method_names[value]}")

    def copy_to_clipboard(self):
        """复制加密结果到剪贴板"""
        # 获取输出框内容
        encrypted_text = self.text_area2.get("1.0", tk.END).strip()
        # 检查是否为空
        if not encrypted_text:
            messagebox.showwarning("提示", "没有可复制的内容")
        else:
            # 清除旧内容
            self.root.clipboard_clear()
            # 写入新内容
            self.root.clipboard_append(encrypted_text)
            # 提示
            messagebox.showinfo("成功", "已复制到剪贴板!")
            # 更改状态
            self.status_bar.config(text="已复制到剪贴板")

    def clear_fields(self):
        self.text_area2.config(state=tk.NORMAL)  # 解锁输出框
        self.text_area2.delete("1.0", tk.END)  # 清空输出框
        self.text_area2.config(state=tk.DISABLED)  # 重新锁定输出框
        self.text_area.focus()  # 焦点给输入框
        self.status_bar.config(text="已清空")  # 更新状态栏

    def setup_bottom_info(self):
        # 底部信息标签
        info_label = tk.Label(
            self.root,
            text="提示:输入文本后选择加密方式,按 Enter 键或点击'执行加密'",
            font=("微软雅黑", 9),
            fg="#7F8C8D")
        info_label.pack(side=tk.BOTTOM, pady=10)

        # 状态栏
        self.status_bar = tk.Label(
            self.root,
            text="就绪 (当前选择: Base64加密)",
            bd=1,
            relief=tk.SUNKEN,
            anchor=tk.W,
            font=("微软雅黑", 9))
        self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)

    def encrypt_method1(self, text):
        """Base64 加密"""
        try:
            encrypted = base64.b64encode(text.encode()).decode()
            return f"[Base64加密]\n{encrypted}"
        except Exception as e:
            return f"[Base64加密失败]\n错误: {str(e)}"

    def encrypt_method2(self, text):
        """移位加密"""
        encrypted = ""
        for char in text:
            encrypted += chr(ord(char) + 1)
        return f"[移位加密]\n{encrypted}"

    def encrypt_method3(self, text):
        """倒序加密"""
        encrypted = text[::-1]
        return f"[倒序加密]\n{encrypted}"


if __name__ == '__main__':
    root = tk.Tk()
    app = EncryptionApp(root)
    root.mainloop()
# 生成exe,方式1
pyinstaller --onefile --windowed --name "GUI应用" .\new_class_gui.py

# 生成exe,方式2,带图标
pyinstaller --onefile --windowed --icon=assets/icons/myicon.ico app.py

 

posted @ 2026-01-23 11:07  蜗牛·哥  阅读(18)  评论(0)    收藏  举报