Codes
import tkinter as tk
from tkinter import messagebox
class Kaisa():
def __init__(self): # 构造函数
self.window = tk.Toplevel() #Toplevel 子窗口
self.input = tk.StringVar()
self.output = tk.StringVar()
self.key = tk.StringVar()
self.dzb = "" # 对照表
def set_window(self):
self.window.title("凯撒密码")
self.window.geometry('400x250')
tk.Label(self.window, text="请输入明文/密文:").place(x=50, y=40)
tk.Label(self.window, text="请输入位移数:").place(x=50, y=80)
tk.Label(self.window, text="输出结果:").place(x=50, y=160)
tk.Label(self.window, textvariable=self.output,bg='red').place(x=50, y=200)
tk.Entry(self.window, textvariable=self.input).place(x=150, y=40)
tk.Entry(self.window, textvariable=self.key,width=2).place(x=150, y=80)
self.key.set(3)
tk.Button(self.window, bg='lightgreen', activebackground='yellow',
text='加密', font=20, command=self.encode).place(x=80, y=120)
tk.Button(self.window, bg='lightgreen', activebackground='yellow',
text='解密', font=20, command=self.decode).place(x=170, y=120)
tk.Button(self.window, bg='yellow', activebackground='green',
text='确定', font=20, command=self.getdzb).place(x=200, y=80)
self.window.mainloop()
def getdzb(self):
k = int(self.key.get())
#print(k)
if(k < 0 or k > 25):
messagebox.showwarning(title='警告', message='密钥需要在0-25之间')
return
self.dzb = ""
for i in range(0, 26):
if i+k < 26:
self.dzb += chr(i+k+97)
else:
self.dzb += chr(i+k+97-26)
print(self.dzb)
def encode(self):
input = self.input.get() # 局部变量
input = input.lower()
if not(input.isalpha()): # 只由字母组成
messagebox.showwarning(title='警告', message='你输入的明文不正确')
return
res = ""
for ch in input:
res += self.dzb[int(ord(ch))-97] # 数值-97
self.output.set(res)
def decode(self):
input = self.input.get()
input = input.lower()
if not(input.isalpha()):
messagebox.showwarning(title='警告', message='你输入的密文不正确')
return
res = ""
for ch in input:
res += chr((self.dzb.find(ch))+97)
self.output.set(res)
class Content():
def __init__(self):
self.window = tk.Tk() #Tk()顶级窗口+初始化
def set_window(self):
self.window.title("古典加密系列")
self.window.geometry('600x400')
tk.Button(self.window,text="凯撒密码",font=40,command=self.ks).place(x=140,y=100)
#tk.Button(self.window,text="单码密码",font=40, command=self.dm).place(x=250,y=100)
#tk.Button(self.window,text="乘法逆元对",font=40,command=self.Cf).place(x=360,y=100)
#tk.Button(self.window,text="Hill加密",font=40,command=self.hill).place(x=140,y=210)
#tk.Button(self.window,text="同余代换",font=40,command=self.Ty).place(x=250,y=210)
#tk.Button(self.window,text="判断素数",font=40,command=self.sushu).place(x=360,y=210)
self.window.mainloop()
def ks(self):
x = Kaisa()
x.set_window()
if __name__ == '__main__':
content = Content()
content.set_window()