0

Below is a code snippet which gives the user 4 options. To either: encrypt a message, decrypt a message, change the encryption_code or to show the encryption_code.

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()

My problem is that declaring encryption_code global is not working throughout my code. The functions where it doesn't work is def display_encrypt and def display decrypt (the encryption_code stays as LFWOAYUISVKMNXPBDCRJTQEGHZ even if the user has changed it) whereas in other functions, it works perfectly.

For example, you change the encryption code to QWERTYUIOPASDFGHJKLZXCVBNM and then go to encrypt and type ABC. It should be encrypted to QWE but instead is encrypted to LFW(the original encryption code)

10
  • Please cut this down to a minimal reproducible example. Commented Oct 29, 2015 at 20:54
  • But to show you how it works I need all of it or should I just show where I'm getting the error? Commented Oct 29, 2015 at 20:55
  • You have some of the same bugs in here that you had in your previous question. Your first if statement in code_change is wrong. The expression will always be true. Commented Oct 29, 2015 at 20:59
  • My mistake, I was trying to shorten it to paste it here and didn't check over for logical mistakes. Commented Oct 29, 2015 at 21:01
  • The minimal code that gives the error. Hard code the one option out of the four you give. Don't ask for user input. Only import what you need to get the error. Get it down to a MCVE, and then paste that. Don't paste your code and then try to shorten it here. Commented Oct 29, 2015 at 21:01

1 Answer 1

1

There are at least two bugs in your code, perhaps more.

First, just like in your previous question, your first if statement in code_change is incorrect. There are at least two answers that address this. I suggest you go back and re-read the answer you accepted here: https://stackoverflow.com/a/33411642/7432.

The second issue is that you're not using encrypton_code to do the encryption, you are using enc (and similarly, dec for description). You never change these variables when the user enters another encryption string. Because you never change them, every string you encrypt uses the original encryption string.

The solution is to update enc and dec in code_change:

def code_change():
    global enc, dec
    ...
    if len(encryptget) == 26:
        ...
        enc = dict(zip(letters,encryption_code))
        dec = dict(zip(encryption_code, letters))
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.