You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter按钮绑定多参函数 单选切换加密数组实现加密

Tkinter 加密功能实现方案

不需要引入numpy等第三方库,用Python原生的字典结构就能实现加密数组的动态切换,具体实现步骤如下:

1. 预定义所有加密类型的映射配置

在代码全局位置用字典存储每一种加密类型对应的明文字符集、密文映射数组,单选框选中的值直接作为字典的key,就能直接取到对应配置,不需要做数组复制操作:

# 全局加密配置字典:key与单选框的value完全对应,value存储该加密方式的明文字典和密文映射表
CIPHER_CONFIG = {
    "Binary": {
        "plain_chars": list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789"),
        "cipher_chars": ["01000001","01000010","01000011", ..., "00111001"] # 按顺序补全对应二进制值即可
    },
    "Caesar": {
        "plain_chars": list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "),
        "cipher_chars": list("DEFGHIJKLMNOPQRSTUVWXYZABCdefghijklmnopqrstuvwxyzabc ") # 偏移3位凯撒密码示例
    },
    "Hexadecimal": {
        "plain_chars": list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789"),
        "cipher_chars": ["41","42","43", ..., "39"] # 按顺序补全对应十六进制值即可
    },
    "Atbash": {
        "plain_chars": list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
        "cipher_chars": list("ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba")
    },
    "Letter-to-Number": { # 注意修正你原代码里的拼写错误,原选项写的是Letter-to-Numbe缺了r
        "plain_chars": list("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
        "cipher_chars": [str(i) for i in range(1,27)]
    }
}

2. 改造ciphering函数,嵌入加密循环逻辑

你原代码已经能正确把输入文本、选中的加密类型传到ciphering函数里,直接在函数内取出对应配置,运行你写的while循环加密逻辑即可。注意修正原代码里的变量重名bug:原函数内把Toplevel窗口命名为ciphering,和函数名重名会导致变量覆盖报错。
修正后的完整ciphering函数:

def ciphering(input_text, selected_cipher):
    # 取出选中加密类型的配置,默认取Binary配置避免key不存在报错
    current_config = CIPHER_CONFIG.get(selected_cipher, CIPHER_CONFIG["Binary"])
    lexicon = current_config["plain_chars"]
    cipherArray = current_config["cipher_chars"]

    # 初始化加密循环需要的变量
    seperatedWord = list(input_text)
    counter = 0
    text_length = len(seperatedWord)
    lexiconPlacing = 0

    # 直接运行你写的while加密逻辑,增加越界判断避免死循环
    while counter < text_length:
        if lexiconPlacing >= len(lexicon):
            # 明文字符集里找不到的字符保留原值,直接跳过
            counter += 1
            lexiconPlacing = 0
            continue
        if seperatedWord[counter] == lexicon[lexiconPlacing]:
            seperatedWord[counter] = cipherArray[lexiconPlacing]
            counter += 1   
            lexiconPlacing = 0  
        lexiconPlacing += 1

    # 拼接最终加密结果
    encrypted_result = '-'.join(seperatedWord)

    # 弹出结果窗口
    result_win = Toplevel()
    result_win.title("decryptt - Encrypt Result")
    Label(result_win, text=f"Input content: {input_text}").pack(pady=5)
    Label(result_win, text=f"Cipher type: {selected_cipher}").pack(pady=5)
    Label(result_win, text=f"Encrypted result: {encrypted_result}").pack(pady=5)
    Button(result_win, text="Exit", padx=10, pady=5, command=result_win.destroy).pack(pady=10)

3. 需要注意的其他问题

  • 不要把控件初始化和pack布局写在同一行,比如cipherLabel = Label(...).pack()会导致cipherLabel变量值为None,后续需要操作控件属性时会报错,拆成两行写即可:
    cipherLabel = Label(cipher, text="cipher")
    cipherLabel.pack()
    
  • 原while循环没有做数组越界判断,用户输入明文字符集外的内容(比如特殊符号、中文)时会出现死循环或者下标报错,上面代码里已经加了对应的异常处理逻辑。
  • 单选框选项里的Letter-to-Numbe拼写错误要修正,保证和CIPHER_CONFIG里的key完全一致,否则会匹配不到配置。

内容的提问来源于stack exchange,提问作者HarrisonT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.31 21:24:26