Tkinter 词频图 matplotlib 凯撒密码 Python
作业:
import tkinter as tk from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure import re from operator import itemgetter #Zhu013 window = tk.Tk() window.title('Caesar') window.geometry('600x600') #画布 canvas1 = tk.Canvas(window,height = 190,width = 500,bg = 'white') image_file1 = tk.PhotoImage(file ="gif格式本地图片地址") image= canvas1.create_image(0,0, anchor = 'nw' ,image= image_file1) canvas1.pack(side = 'top') canvas2 = tk.Canvas(window,height = 190,width = 500,bg = 'white') image_file2 = tk.PhotoImage(file ="gif格式本地图片地址") image= canvas2.create_image(0,0, anchor = 'nw' ,image= image_file2) canvas2.pack(side = 'bottom') #Label tk.Label(window,text = '字符串:').place(x=70,y=210) tk.Label(window,text = '加密密钥:').place(x=70,y=240) tk.Label(window,text = '统计破解密钥:').place(x=250,y=280) e=tk.StringVar() e.set('please input string here') e = tk.Entry(window,textvariable = e,show = None,width = 50) e.place(x=160,y=210) k = tk.IntVar() k = tk.Entry(window,textvariable = k,show = None,width = 5) k.place(x=160,y=240) def encode(): string='' content = e.get() content = content.upper() key = int(k.get()) for i in content: if(ord(i) == 32): ascii = 32 else: ascii = ((ord(i)+key-65)%26)+65 string = string+chr(ascii) t.insert('insert',string) """ ACEGIKMOQSUWY 65 67 69 71 73 75 77 79 81 83 85 87 89 """ def decode(): #tmp = 0 Oldcontent = e.get() Oldcontent = Oldcontent.upper() #lenth = len(content) #print(lenth) x = ["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"] word_count={'A':0,'B':0,'C':0,'D':0,'E':0,'F':0,'G':0,'H':0,'I':0,'J':0,'K':0,'L':0,'M':0,'N':0,'O':0,'P':0,'Q':0,'R':0,'S':0,'T':0,'U':0,'V':0,'W':0,'X':0,'Y':0,'Z':0} word_count1={'A':0,'B':0,'C':0,'D':0,'E':0,'F':0,'G':0,'H':0,'I':0,'J':0,'K':0,'L':0,'M':0,'N':0,'O':0,'P':0,'Q':0,'R':0,'S':0,'T':0,'U':0,'V':0,'W':0,'X':0,'Y':0,'Z':0} #处理标点 content = re.compile('[^A-Z^a-z^0-9^ ]').sub('',Oldcontent) content = "".join(content.split()) lenth = len(content) #统计画表 for word in content: if word in word_count: word_count[word] += 1 else: word_count[word] = 1 y = [word_count['A']/lenth, word_count['B']/lenth, word_count['C']/lenth, word_count['D']/lenth, word_count['E']/lenth, word_count['F']/lenth, word_count['G']/lenth, word_count['H']/lenth, word_count['I']/lenth, word_count['J']/lenth, word_count['K']/lenth, word_count['L']/lenth, word_count['M']/lenth, word_count['N']/lenth, word_count['O']/lenth, word_count['P']/lenth, word_count['Q']/lenth, word_count['R']/lenth, word_count['S']/lenth, word_count['T']/lenth, word_count['U']/lenth, word_count['V']/lenth, word_count['W']/lenth, word_count['X']/lenth, word_count['Y']/lenth, word_count['Z']/lenth] drawBar1(x,y) #统计破解 #可调整猜测数 value = order_dict(word_count,1) #print(value) key1 = 0 for i in range(0,1): string1 = '' #value = max(word_count,key = word_count.get) if(ord('E')>ord(value[i])): key1 = ord('E')-ord(value[i]) else: key1 = ord(value[i])-ord('E') for i in content: if(ord(i) == 32): ascii = 32 else: ascii = ((ord(i)-key1-65)%26)+65 string1 = string1 + chr(ascii) guess.insert('end',string1+'\n') #统计破解的字符串画表 for word in string1: if word in word_count1: word_count1[word] += 1 else: word_count1[word] = 1 y = [word_count1['A']/lenth, word_count1['B']/lenth, word_count1['C']/lenth, word_count1['D']/lenth, word_count1['E']/lenth, word_count1['F']/lenth, word_count1['G']/lenth, word_count1['H']/lenth, word_count1['I']/lenth, word_count1['J']/lenth, word_count1['K']/lenth, word_count1['L']/lenth, word_count1['M']/lenth, word_count1['N']/lenth, word_count1['O']/lenth, word_count1['P']/lenth, word_count1['Q']/lenth, word_count1['R']/lenth, word_count1['S']/lenth, word_count1['T']/lenth, word_count1['U']/lenth, word_count1['V']/lenth, word_count1['W']/lenth, word_count1['X']/lenth, word_count1['Y']/lenth, word_count1['Z']/lenth] drawBar2(x,y) #暴力破解 for key in range(0,26): string = '第'+str(key)+'暴力破解:' #print(key) for i in content: if(ord(i) == 32): ascii = 32 else: ascii = ((ord(i)-key-65)%26)+65 string = string + chr(ascii) t.insert('end',string) #print(word_count) def drawBar1(x,y): #matplotlib f =Figure(figsize=(5,2), dpi=100) f.clf() a = f.add_subplot(111) #x = [0, 1, 2, 3, 4, 5] #y = [222, 42, 455, 664, 454, 334] #绘制图形 a.bar(x,y,0.4,color='g') a.plot() #a.plot(x,y,0.4,color = 'g') canvas = FigureCanvasTkAgg(f,master = window) canvas.draw() canvas.get_tk_widget().place(x=50,y=0) def drawBar2(x,y): #matplotlib f =Figure(figsize=(5,2), dpi=100) f.clf() a = f.add_subplot(111) #绘制图形 a.bar(x,y,0.4,color='r') a.plot() #a.plot(x,y,0.4,color = 'g') canvas = FigureCanvasTkAgg(f,master = window) canvas.draw() canvas.get_tk_widget().place(x=50,y=400) #字典排序取值 def order_dict(dicts, n): print(n) result = [] result1 = [] p = sorted([(k, v) for k, v in dicts.items()], reverse=True) s = set() for i in p: s.add(i[1]) for i in sorted(s, reverse=True)[:n]: for j in p: if j[1] == i: result.append(j) for r in result: result1.append(r[0]) return result1 #按钮 tk.Button(window,text='Encode',width = 5,height = 2,command= encode).place(x=120,y=270) tk.Button(window,text='Decode',width = 5,height = 2,command= decode).place(x=180,y=270) #b1.pack() #b2.pack() #猜测结果 guess=tk.Text(window,height = 3,width = 25) guess.place(x=330,y=280) #结果Label t=tk.Text(window,height = 3,width = 75) t.place(x=50,y=340) window.mainloop()
来自非洲的欧提大人

浙公网安备 33010602011771号