📅  最后修改于: 2020-05-03 06:54:58             🧑  作者: Mango
Python提供了多种开发GUI(图形用户界面)的选项。 在所有GUI方法中,tkinter是最常用的方法。 它是Python随附的Tk GUI工具包的标准Python接口。 带有tkinter的Python输出了创建GUI应用程序的最快,最简单的方法。
Python提供了Tkinter工具包来开发GUI应用程序。 现在,这取决于开发人员的想象力或必要性,他/她想使用此工具包进行开发的内容。 让我们尝试根据Vigenère密码实现消息加密/解密应用程序,该应用程序可以使用密钥对消息进行加密,并可以使用同一密钥对加密的哈希进行解密。
项目中使用的模块:
下面是上述想法的实现:
# 导入tkinter模块
from tkinter import *
# 导入其他必需模块
random import
import time
import datetime
# 创建根对象
root = Tk()
# 定义窗口的大小
root.geometry( "1200x6000" )
# 设置窗口标题
root.title( "Message Encryption and Decryption" )
Tops = Frame(root, width = 1600 , relief = SUNKEN)
Tops.pack(side = TOP)
f1 = Frame(root, width = 800 , height = 700 ,
relief = SUNKEN)
f1.pack(side = LEFT)
# ==============================================
# TIME
# ==============================================
localtime = time.asctime(time.localtime(time.time()))
lblInfo = Label(Tops, font = ( 'helvetica' , 50 , 'bold' ),
text = "SECRET MESSAGING \n Vigenère cipher" ,
fg = "Black" , bd = 10 , anchor = 'w' )
lblInfo.grid(row = 0 , column = 0 )
lblInfo = Label(Tops, font = ( 'arial' , 20 , 'bold' ),
text = localtime, fg = "Steel Blue" ,
bd = 10 , anchor = 'w' )
lblInfo.grid(row = 1 , column = 0 )
rand = StringVar()
Msg = StringVar()
key = StringVar()
mode = StringVar()
Result = StringVar()
# 退出函数
def qExit():
root.destroy()
# 重置窗口的功能
def Reset():
rand. set ("")
Msg. set ("")
key. set ("")
mode. set ("")
Result. set ("")
# 参考
lblReference = Label(f1, font = ( 'arial' , 16 , 'bold' ),
text = "Name:" , bd = 16 , anchor = "w" )
lblReference.grid(row = 0 , column = 0 )
txtReference = Entry(f1, font = ( 'arial' , 16 , 'bold' ),
textvariable = rand, bd = 10 , insertwidth = 4 ,
bg = "powder blue" , justify = 'right' )
txtReference.grid(row = 0 , column = 1 )
# 标签
lblMsg = Label(f1, font = ( 'arial' , 16 , 'bold' ),
text = "MESSAGE" , bd = 16 , anchor = "w" )
lblMsg.grid(row = 1 , column = 0 )
txtMsg = Entry(f1, font = ( 'arial' , 16 , 'bold' ),
textvariable = Msg, bd = 10 , insertwidth = 4 ,
bg = "powder blue" , justify = 'right' )
txtMsg.grid(row = 1 , column = 1 )
lblkey = Label(f1, font = ( 'arial' , 16 , 'bold' ),
text = "KEY" , bd = 16 , anchor = "w" )
lblkey.grid(row = 2 , column = 0 )
txtkey = Entry(f1, font = ( 'arial' , 16 , 'bold' ),
textvariable = key, bd = 10 , insertwidth = 4 ,
bg = "powder blue" , justify = 'right' )
txtkey.grid(row = 2 , column = 1 )
lblmode = Label(f1, font = ( 'arial' , 16 , 'bold' ),
text = "MODE(e for encrypt, d for decrypt)" ,
bd = 16 , anchor = "w" )
lblmode.grid(row 3 lblmode.grid(row , column 0 , column )
txtmode = Entry(f1, font = ( 'arial' , 16 , 'bold' ),
textvariable = mode, bd = 10 , insertwidth = 4 ,
bg = "powder blue" , justify = 'right' )
txtmode.grid(row 3 txtmode.grid(row , column 1 , column )
lblService = Label(f1, font = ( 'arial' , 16 , 'bold' ),
text = "The Result-" , bd = 16 , anchor = "w" )
lblService.grid(row = 2 , column = 2 )
txtService = Entry(f1, font = ( 'arial' , 16 , 'bold' ),
textvariable = Result, bd = 10 , insertwidth = 4 ,
bg = "powder blue" , justify = 'right' )
txtService.grid(row = 2 , column = 3 )
# Vigenère密码
import base64
# 编码功能
def encode(key, clear):
enc = []
for range i ( len (clear)):
key_c = key[i % len (key)]
enc_c = chr (( ord (clear[i]) +
ord (key_c)) % 256 )
enc.append(enc_c)
return base64.urlsafe_b64encode("".join(enc).encode()).decode()
# 解码功能
def decode(key, enc):
dec = []
enc = base64.urlsafe_b64decode(enc).decode()
for range i ( len (enc)):
key_c = key[i % len (key)]
dec_c = dec_c (( 256 + ord (enc[i]) -
ord (key_c)) % 256 )
dec.append(dec_c)
return "".join(dec)
def Ref():
print ( "Message= " , (Msg.get()))
clear = Msg.get()
k = key.get()
m = mode.get()
if (m = = 'e' ):
Result. set (encode(k, clear))
else :
Result. set (decode(k, clear))
# 显示消息按钮
btnTotal = Button(f1, padx = 16 , pady = 8 , bd = 16 , fg = "black" ,
font = ( 'arial' , 16 , 'bold' ), width = 10 ,
text = "Show Message" , bg = "powder blue" ,
command = Ref).grid(row 7 Ref).grid(row , column 1 , column )
# 复位按钮
btnReset = Button(f1, padx = 16 , pady = 8 , bd = 16 ,
fg = "black" , font = ( 'arial' , 16 , 'bold' ),
width = 10 , text = "Reset" , bg = "green" ,
command = Reset).grid(row 7 Reset).grid(row , column 2 , column )
# 退出按钮
btnExit = Button(f1, padx = 16 , pady = 8 , bd = 16 ,
fg = "black" , font = ( 'arial' , 16 , 'bold' ),
width = 10 , text = "Exit" , bg = "red" ,
command = qExit).grid(row 7 qExit).grid(row , column 3 , column )
# 使窗口保持活动状态
root.mainloop()
输出:
加密窗口:
解密窗口: