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)

Inkblot
  • 708
  • 2
  • 8
  • 19
  • Please cut this down to a [mcve]. – Teepeemm Oct 29 '15 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? – Inkblot Oct 29 '15 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. – Bryan Oakley Oct 29 '15 at 20:59
  • My mistake, I was trying to shorten it to paste it here and didn't check over for logical mistakes. – Inkblot Oct 29 '15 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. – Teepeemm Oct 29 '15 at 21:01
  • I shortened it in Python and ran it before pasting it here to try and show you what I meant by it wasn't working throughout the code but I will remove whatever is working fine. – Inkblot Oct 29 '15 at 21:03
  • Why do you think global is not working? What specific symptom are you seeing that you don't expect? It seems to be working for me. If I enter a new code, it saves it to `encryption_code`. – Bryan Oakley Oct 29 '15 at 21:07
  • I honestly don't have a clue why it isn't working because if you change the code to QWERTY etc. and then type ABC in the `entry` it still gives LFW and not QWE. – Inkblot Oct 29 '15 at 21:08
  • you're using `enc` to do the encryption, but you never change that variable when the user enters a new code, so the encryption always uses the original encryption code. – Bryan Oakley Oct 29 '15 at 21:13
  • So I'd put `enc` in the if statement and then declare it global wherever I want to use it? – Inkblot Oct 29 '15 at 21:18

1 Answers1

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))
        ...
Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685