冲浪的口香糖 发表于 2025-8-26 16:12:25

用python做了一个摩斯密码计算器

Python tkitner做的摩斯密码计算器,可以生成密码和解密密码
文件:

FishC 发表于 2025-8-26 16:13:33

这是一个使用Python tkinter制作的摩斯密码计算器的基本示例代码:


import tkinter as tk
from tkinter import messagebox

# 摩斯密码字典
MORSE_CODE_DICT = {
    'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
    'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
    'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
    'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
    'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
    '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
    '9': '----.', '0': '-----', ', ': '--..--', '.': '.-.-.-', '?': '..--..',
    '/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-'
}

# 反转字典用于解码
REVERSE_DICT = {v: k for k, v in MORSE_CODE_DICT.items()}

def encrypt():
    text = input_text.get("1.0", "end-1c").upper()
    morse_code = ""
    for char in text:
      if char == " ":
            morse_code += "/ "
      elif char in MORSE_CODE_DICT:
            morse_code += MORSE_CODE_DICT + " "
      else:
            morse_code += char + " "
    output_text.delete("1.0", "end")
    output_text.insert("1.0", morse_code)

def decrypt():
    code = input_text.get("1.0", "end-1c")
    text = ""
    for word in code.split(" / "):
      for char in word.split():
            if char in REVERSE_DICT:
                text += REVERSE_DICT
            else:
                text += char
      text += " "
    output_text.delete("1.0", "end")
    output_text.insert("1.0", text.strip())

# 创建主窗口
root = tk.Tk()
root.title("摩斯密码计算器")
root.geometry("500x400")

# 创建输入框和标签
tk.Label(root, text="输入文本或摩斯密码:").pack(pady=5)
input_text = tk.Text(root, height=5, width=50)
input_text.pack(pady=5)

# 创建按钮框架
button_frame = tk.Frame(root)
button_frame.pack(pady=5)

encrypt_button = tk.Button(button_frame, text="加密", command=encrypt)
encrypt_button.pack(side="left", padx=5)

decrypt_button = tk.Button(button_frame, text="解密", command=decrypt)
decrypt_button.pack(side="left", padx=5)

# 创建输出框和标签
tk.Label(root, text="结果:").pack(pady=5)
output_text = tk.Text(root, height=5, width=50)
output_text.pack(pady=5)

# 运行主循环
root.mainloop()


这个程序具有以下功能:

主要功能:
o 将英文文本加密为摩斯密码
o 将摩斯密码解密为英文文本
o 使用 tkinter 创建图形界面

使用方法:
1. 在上方文本框中输入要加密的英文或要解密的摩斯密码
2. 点击 加密 按钮生成摩斯密码
3. 点击 解密 按钮将摩斯密码转换回英文

注意事项:
o 摩斯密码中的单词之间用 / 分隔
o 字母之间用空格分隔
o 程序会自动处理大小写问题

你可以直接复制这段代码到Python文件中运行,需要确保已安装Python和tkinter库(通常Python标准安装中已包含)。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

某一个“天” 发表于 2025-8-26 18:16:44

{:10_249:}
页: [1]
查看完整版本: 用python做了一个摩斯密码计算器