下面是一個給出用戶4選項的代碼片段。要麼:加密消息,解密消息,更改encryption_code
或顯示encryption_code
。聲明一個變量global不能在整個代碼片段中工作
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encryption_code = 'LFWOAYUISVKMNXPBDCRJTQEGHZ'
letters += letters.lower()
encryption_code += encryption_code.lower()
enc = dict(zip(letters,encryption_code))
dec = dict(zip(encryption_code, letters))
window = tkinter.Tk()
style = ttk.Style(window)
style.configure("BW.TLabel")
encrypt_frame = tkinter.Frame(window)
encrypted_frame = tkinter.Frame(window)
change_frame = tkinter.Frame(window)
changed_frame = tkinter.Frame(window)
entry = tkinter.Entry(encrypt_frame)
encrypt_entry = tkinter.Entry(change_frame)
encryptget = encrypt_entry.get()
def code_change():
global changed_frame
global encrypt_entry
global encryption_code
global encryptget
if len(encryptget) == 26:
encryption_code = encryptget
encryption_code += encryption_code.lower()
changed_label.configure(background=window.cget('bg'))
changed_label.config(text="You have successfully changed the encryption code!")
change_header.config(text="Your code is: " + str(encryptget.upper()))
changed_frame.pack_forget()
changed_label.pack()
changed_frame.pack()
def encrypt():
global encrypt_frame
entry.focus_set()
entry.pack()
encrypt_confirm.pack()
back_button.pack()
encrypt_frame.pack()
first_frame.pack_forget()
def display_encrypt():
global encryption_code
if len(entry.get()) > 0:
display_enc = "".join([enc.get(ch, ch) for ch in entry.get()])
entry.delete(0, tkinter.END)
new_message.config(background=window.cget('bg'))
new_message.config(text=str(display_enc))
new_message.pack()
encrypted_frame.pack()
def back():
new_message.pack_forget()
entry.delete(0, tkinter.END)
first_frame.pack()
encrypt_frame.pack_forget()
encrypted_frame.pack_forget()
change_frame.pack_forget()
changed_frame.pack_forget()
def change_code():
global change_frame
encrypt_entry.focus_set()
encrypt_entry.pack()
change_confirm.pack()
back_button4.pack()
change_frame.pack()
first_frame.pack_forget()
first_frame = tkinter.Frame(window)
encrypt_button = ttk.Button(first_frame, text="Encrypt", width=20, command=encrypt)
change_code = ttk.Button(first_frame, text="Change code", width=20, command=change_code)
encrypt_button.pack()
change_code.pack()
first_frame.pack()
back_button = ttk.Button(encrypt_frame, text="Back", width=20, command=back)
back_button4 = ttk.Button(change_frame, text="Back", width=20, command=back)
encrypt_confirm = ttk.Button(encrypt_frame, text="Confirm", width=20, command=display_encrypt)
new_message = tkinter.Label(encrypted_frame, text="", font=('Helvetica', 10))
change_confirm = ttk.Button(change_frame, text="Confirm", width=20, command=code_change)
window.mainloop()
我的問題是聲明encryption_code
全局不起作用在我的代碼。它不起作用的功能是def display_encrypt
和def display decrypt
(即使用戶更改了它,encryption_code
仍保持爲LFWOAYUISVKMNXPBDCRJTQEGHZ),而在其他功能中,它可以很好地工作。
例如,您將加密代碼更改爲QWERTYUIOPASDFGHJKLZXCVBNM,然後進行加密並鍵入ABC。它應該被加密到QWE,而是被加密到LFW(原始加密代碼)
請把這個下降到[MCVE。 – Teepeemm
但是爲了告訴你它是如何工作的,我需要所有這些,或者我應該只顯示出現錯誤的位置? – Inkblot
你在這裏有一些和你之前的問題一樣的錯誤。 'code_change'中的第一個'if'語句是錯誤的。表達將永遠是真實的。 –