Home > OS >  How can I assign a function to a button in Tkinter with two different variables?
How can I assign a function to a button in Tkinter with two different variables?

Time:06-07

I know this may sound like a common question and something easy but I can't wrap my head around it.

I have pasted my code below. I will refer to this code as Program 1

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-Numbe")
    ]
    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: ciphering(cipherEntry.get(), cipherType.get())).pack() #lambda allows you to pass arguments to functions
    quitButton = Button(cipher, text="Exit Cipher", padx=10, pady=5, command=cipher.destroy).pack()

    

def openDecipher():
    decipher = Toplevel()
    decipher.title("decryptt - DECIPHER")
    decipherLabel = Label(decipher, text="decipher").pack()
    decipherEntry = Entry(decipher, width=20, borderwidth=5) #separating pack now allows you to use get() on this
    decipherEntry.pack()
    decipherButton = Button(decipher, text="Decipher", padx=10, pady=5, command=lambda:deciphering(decipherEntry.get())).pack() #lambda allows you to pass arguments to functions
    quitButton = Button(decipher, text="Exit Decipher", padx=10, pady=5, command=decipher.destroy).pack()

    
# This is the function that is suppose to split the input from cipherEntry into individual characters in an array.
def ciphering(input,choice):
    ciphering = Toplevel() #needed to add new label to
    cipherLabeling = Label(ciphering, text = "You have inputted '"   input   "'").pack() 
    seperatedWord = list(input)
    cipherLabeling = Label(ciphering, text = seperatedWord[:]).pack()
    seperatedWordLength = len(seperatedWord)
    cipherLabeling = Label(ciphering, text = seperatedWordLength).pack()
    selection = Label(ciphering, text = choice).pack()
    quitButton = Button(ciphering, text="Exit", padx=10, pady=5, command=ciphering.destroy).pack()

and this code as Program 2

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

    # else:
    #   print("letter don't match")

    lexiconPlacing = lexiconPlacing   1

# else:
#   print("no")

print(seperatedWord)
lexiconPlacing = 0
counter = 0

print('-'.join(seperatedWord))

Image of the window where you can enter the word or phrase you want to cipher and choice from the cipher types listed

What I need help with is how do I make it so that the while loop in Program 2 is accessed when you click the "cipher button in the image above. I have it so that takes the input in the textbox and the selected cipher type over to the other window, but I don't know how to use make it so that they are inputted into the function and how to actually make the function work in a Tkinter window. I am finding it especially difficult to make it so that the array being used in Program 2 (cipherType, which is what ever cipher is selected, so the array used would be binary if they selected binary using the radio buttons. I don't know how to change it to the specific array using their selected radio button).

I have tried making it so that whatever option they choice, it copies the array associated with it to a general array, which is cipherType. But that doesn't work because I have read that you can't really copy an array without using numpy or something like that.

Basically, what I need help with is how do I make it so that I can use an input from a radio button that changes the array inside of a function that is being called, which ciphers the whatever the user has inputted. Which then opens another window showing the result.

Image of me trying to explain the ciphering process and what I need help with

In advance thank you to anyone that helps or gives me any tips :)

CodePudding user response:

I cannot reproduce your code as it is incomplete. But I think that one window is enough to encrypt one word. For decryption, you can simply add another entry field for the encrypted word. All the encryption/decryption magic will take place behind the scenes, in functions. I will try to describe the logic of the program for encryption.

Here in program development you have to go "from top to bottom".

  1. You need to define all five ransomware functions:
def binary_cipher(world):
    print(f'binary {world}')
    return encrypted_word


def caesar_cipher(world):
    print(f'caesar {world}')
    return encrypted_word
# .....

Where arrays are just python lists.

  1. Now we need to select the desired function depending on the selected radiobutton.
def choice_encryption(cipher_type, cipher_entry):
    method = cipher_type.get()
    entr = cipher_entry.get()
    if method == 'binary':
        enc_word = binary_cipher(entr)
    elif method == 'caesar':
        enc_word = caesar_cipher(entr)
# .......
  1. Output the encrypted word in the entry field, after the previously entered word.

  2. With the "Cipher" button you call the choice_encryption function with two StringVar variables.

    command=lambda: choice_encryption(cipherType, cipherEntry)

  • Related