0
# Imports the tkinter library.
from tkinter import *

# Pillow is a improved verison of PIL, stands for Python Image Library. Allows you to import image types like .jpg and .png
from PIL import ImageTk,Image

# Imports messagebox module from tkinter library.
from tkinter import messagebox

# Tk() helps to display the root window and manages all the other components of the tkinter application and assigns it to root.
root = Tk()

# Renames the title of the tkinter window
root.title('d3cryptt')

global lexicon
global bcipher
global ccipher
global hcipher
global acipher
global lcipher
global cipherChoice
global decipherChoice
global lexiconPlacing
global counter
global cipherType
global cipheredWord
global cipheredMessage

counter = 0
lexiconPlacing = 0


# This is the array that holds the number and letters for the BINARY cipher
lexicon = ["0","1","2","3","4"]...

bcipher = ["00000000","00000001","00000010","00000011","00000100","00000101"]

ccipher = ["0","1","2","3","4","5"]

hcipher = ["30","31","32","33","34","35"]

acipher = ["0","1","2","3","4","5"]

lcipher = ["_","_","_","_","_","_"]


# This is the area for all my 'def' functions to keep my program organised. 'def' is used to define functions.
def binary_cipher(world):
    lexiconPlacing = 0
    counter = 0
    seperatedWord = list(world)
    print(str(world) + " has been inputted")
    print(seperatedWord)
    length = len(seperatedWord)
    print("The inputted word has " + str(length) + " characters")

    while counter < length:
        if seperatedWord[counter] == lexicon[lexiconPlacing]:
            print("letter match: " + seperatedWord[counter])
            seperatedWord[counter] = hcipher[lexiconPlacing]
            counter = counter + 1
            lexiconPlacing = 0  

        lexiconPlacing = lexiconPlacing + 1

    print(seperatedWord)
    lexiconPlacing = 0
    counter = 0
    cipheredWord = '-'.join(seperatedWord)
    print(cipheredWord)

def openCipher():
    cipher = Toplevel()
    cipher.title("decryptt - CIPHER")
    cipherLabel = Label(cipher, text="cipher").pack()
    cipherEntry = Entry(cipher, width=20, borderwidth=5) #separating pack now allows you to use get() on this
    cipherEntry.pack()
    cipherChoices = [
        ("Binary","Binary"),
        ("Caesar","Caesar"),
        ("Hexadecimal","Hexadecimal"),
        ("Atbash","Atbash"),
        ("Letter-to-Number","Letter-to-Number")
    ]
    cipherType = StringVar()
    cipherType.set("Binary")


    for text, cipherChoice in cipherChoices:
        Radiobutton(cipher, text=text, variable=cipherType, value=cipherChoice).pack()

    cipherButton = Button(cipher, text="Cipher", padx=10, pady=5, command=lambda: cipheringProcess(cipherType.get(), cipherEntry.get())).pack() #lambda allows you to pass arguments to functions
    EnteredMessage = Label(cipher, text="Your ciphered input will appear below", padx=5, pady=2).pack()
    spacer3 = Label(cipher, text="◤━━━━━━━━━━━━◥", padx=1, pady=0.1).pack()
    cipheredMessage = Label(cipher, text=" ", padx=5, pady=2).pack()
    spacer3 = Label(cipher, text="◣━━━━━━━━━━━━◢", padx=1, pady=0.1).pack()
    quitButton = Button(cipher, text="Exit Cipher", padx=10, pady=5, command=cipher.destroy).pack()

def cipheringProcess(cipher_type, cipher_entry):
    method = cipher_type
    entry = cipher_entry
    if method == "Binary":
        cipheredWord = binary_cipher(entry)
    elif method == "Caesar":
        cipheredWord = caesar_cipher(entry)
    elif method == "Hexadecimal":
        cipheredWord = hexadecimal(entry)       
    elif method == "Atbash":
        cipheredWord = atbash(entry)
    elif method == "Letter-to-Number":
        cipheredWord = l2n(entry)

Above is part of the program I am creating. I am in the final stages of it and am having trouble trying to get the ciphered word onto the window.

I am trying to get cipheredWord to be the text in cipheredMessage after the user inputs something into cipherEntry and clicks cipherButton. The input goes into the cipheringProcess, which determines what ciphering process it will go through. in the multiple functions names (whatever cipher selected)_cipher().

I can't find a way that works to get cipheredWord from binary_cipher() to be cipheredMessage in openCipher().

I have tried to use:

  • using ["text"] = cipheredWord
  • .config
  • .set
  • StringVar()

If anyone can give me a way of doing it, I would really appreciate it! Thanks in advance to anyone that is able to help me

HarrisonT
  • 45
  • 9

2 Answers2

1

I think you just add a function to change the variable which is used to assign the text to the label.

def changelabel():
   status="button clicked"
   return status
cipheredMessage = Label(cipher, text=changelabel() , padx=5, pady=2).pack()
codester_09
  • 5,622
  • 2
  • 5
  • 27
Mohsin
  • 7
  • 3
1
# Imports the tkinter library.
from tkinter import *

# Pillow is a improved verison of PIL, stands for Python Image Library. Allows you to import image types like .jpg and .png
from PIL import ImageTk,Image

# Imports messagebox module from tkinter library.
from tkinter import messagebox

# Tk() helps to display the root window and manages all the other components of the tkinter application and assigns it to root.
root = Tk()

# Renames the title of the tkinter window
root.title('d3cryptt')

global lexicon
global bcipher
global ccipher
global hcipher
global acipher
global lcipher
global cipherChoice
global decipherChoice
global lexiconPlacing
global counter
global cipherType
global cipheredWord
global cipheredMessage

counter = 0
lexiconPlacing = 0


# This is the array that holds the number and letters for the BINARY cipher
lexicon = ["0","1","2","3","4"]

bcipher = ["00000000","00000001","00000010","00000011","00000100","00000101"]

ccipher = ["0","1","2","3","4","5"]

hcipher = ["30","31","32","33","34","35"]

acipher = ["0","1","2","3","4","5"]

lcipher = ["_","_","_","_","_","_"]


# This is the area for all my 'def' functions to keep my program organised. 'def' is used to define functions.
def binary_cipher(world):
    lexiconPlacing = 0
    counter = 0
    seperatedWord = list(world)
    print(str(world) + " has been inputted")
    print(seperatedWord)
    length = len(seperatedWord)
    print("The inputted word has " + str(length) + " characters")

    while counter < length and lexiconPlacing < len(lexicon): # HANNA: edit 1, adding limit to lexiconPlacing
        if seperatedWord[counter] == lexicon[lexiconPlacing]:
            print("letter match: " + seperatedWord[counter])
            seperatedWord[counter] = hcipher[lexiconPlacing]
            counter = counter + 1
            lexiconPlacing = 0  

        lexiconPlacing = lexiconPlacing + 1

    print(seperatedWord)
    lexiconPlacing = 0
    counter = 0
    cipheredWord = '-'.join(seperatedWord)
    print(cipheredWord)
    
    return cipheredWord # HANNA: edit 2

def openCipher():
    cipher = Toplevel()
    cipher.title("decryptt - CIPHER")
    cipherLabel = Label(cipher, text="cipher").pack()
    cipherEntry = Entry(cipher, width=20, borderwidth=5) #separating pack now allows you to use get() on this
    cipherEntry.pack()
    cipherChoices = [
        ("Binary","Binary"),
        ("Caesar","Caesar"),
        ("Hexadecimal","Hexadecimal"),
        ("Atbash","Atbash"),
        ("Letter-to-Number","Letter-to-Number")
    ]
    cipherType = StringVar()
    cipherType.set("Binary")


    for text, cipherChoice in cipherChoices:
        Radiobutton(cipher, text=text, variable=cipherType, value=cipherChoice).pack()

    cipherButton = Button(cipher, text="Cipher", padx=10, pady=5, command=lambda: cipheringProcess(cipherType.get(), cipherEntry.get())).pack() #lambda allows you to pass arguments to functions

    EnteredMessage = Label(cipher, text="Your ciphered input will appear below", padx=5, pady=2).pack()

    spacer3 = Label(cipher, text="◤━━━━━━━━━━━━◥", padx=1, pady=0.1).pack()

    global cipheredMessage # HANNA: edit 3
    cipheredMessage = Label(cipher, text=" ", padx=5, pady=2)# HANNA: edit 3
    cipheredMessage.pack()# HANNA: edit 3

    spacer4 = Label(cipher, text="◣━━━━━━━━━━━━◢", padx=1, pady=0.1).pack()

    quitButton = Button(cipher, text="Exit Cipher", padx=10, pady=5, command=cipher.destroy).pack()

def cipheringProcess(cipher_type, cipher_entry):
    method = cipher_type
    entry = cipher_entry
    if method == "Binary":
        cipheredWord = binary_cipher(entry)
    elif method == "Caesar":
        cipheredWord = caesar_cipher(entry)
    elif method == "Hexadecimal":
        cipheredWord = hexadecimal(entry)       
    elif method == "Atbash":
        cipheredWord = atbash(entry)
    elif method == "Letter-to-Number":
        cipheredWord = l2n(entry)

    global cipheredMessage # HANNA: edit 4
    cipheredMessage.config(text = cipheredWord) # HANNA: edit 4

openCipher() # HANNA: edit 5

Hanna
  • 1,071
  • 1
  • 2
  • 14