python语言描写维吉尼亚演示程序的可视化界面设计代码
时间: 2023-08-13 16:06:26 浏览: 87
以下是一个基于Python tkinter的维吉尼亚演示程序的可视化界面设计代码示例:
```python
import tkinter as tk
from tkinter import ttk
class VigenereGui:
def __init__(self, master):
self.master = master
master.title("Vigenere Cipher")
self.plain_text_label = ttk.Label(master, text="Plain Text")
self.plain_text_label.grid(row=0, column=0, padx=5, pady=5)
self.plain_text_entry = ttk.Entry(master, width=50)
self.plain_text_entry.grid(row=0, column=1, padx=5, pady=5)
self.key_label = ttk.Label(master, text="Key")
self.key_label.grid(row=1, column=0, padx=5, pady=5)
self.key_entry = ttk.Entry(master, width=50)
self.key_entry.grid(row=1, column=1, padx=5, pady=5)
self.cipher_text_label = ttk.Label(master, text="Cipher Text")
self.cipher_text_label.grid(row=2, column=0, padx=5, pady=5)
self.cipher_text_entry = ttk.Entry(master, width=50)
self.cipher_text_entry.grid(row=2, column=1, padx=5, pady=5)
self.encrypt_button = ttk.Button(master, text="Encrypt", command=self.encrypt)
self.encrypt_button.grid(row=3, column=0, padx=5, pady=5)
self.decrypt_button = ttk.Button(master, text="Decrypt", command=self.decrypt)
self.decrypt_button.grid(row=3, column=1, padx=5, pady=5)
def encrypt(self):
plain_text = self.plain_text_entry.get()
key = self.key_entry.get()
# Perform Vigenere encryption
cipher_text = ""
for i in range(len(plain_text)):
cipher_text += chr((ord(plain_text[i]) + ord(key[i % len(key)])) % 26 + ord('A'))
self.cipher_text_entry.delete(0, tk.END)
self.cipher_text_entry.insert(0, cipher_text)
def decrypt(self):
cipher_text = self.cipher_text_entry.get()
key = self.key_entry.get()
# Perform Vigenere decryption
plain_text = ""
for i in range(len(cipher_text)):
plain_text += chr((ord(cipher_text[i]) - ord(key[i % len(key)])) % 26 + ord('A'))
self.plain_text_entry.delete(0, tk.END)
self.plain_text_entry.insert(0, plain_text)
root = tk.Tk()
vigenere_gui = VigenereGui(root)
root.mainloop()
```
该代码使用tkinter库创建了一个具有三个文本框和两个按钮的窗口。用户可以在第一个文本框中输入明文,在第二个文本框中输入密钥,并通过单击“Encrypt”按钮进行加密。加密后的密文将显示在第三个文本框中。同样,用户也可以在第三个文本框中输入密文,并通过单击“Decrypt”按钮进行解密。解密后的明文将显示在第一个文本框中。
阅读全文