Home > Blockchain >  How do I make a button that allows me to send two variables into the same function in Tkinter?
How do I make a button that allows me to send two variables into the same function in Tkinter?

Time:06-05

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","bcipher"),
        ("Caesar","ccipher"),
        ("Hexadecimal","hcipher"),
        ("Atbash","acipher"),
        ("Letter-to-Number","lcipher")
    ]
    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()), ciphering(cipherChoice.get())]).pack() #lambda allows you to pass arguments to functions
    quitButton = Button(cipher, text="Exit Cipher", padx=10, pady=5, command=cipher.destroy).pack()


    
# This is the function that is suppose to split the input from cipherEntry into individual characters in an array.
def ciphering(entry,choice):
    ciphering = Toplevel() #needed to add new label to
    cipherLabeling = Label(ciphering, text = "You have inputted "   entry).pack() #couldn’t add a list to string like that, nor use get() on a list, changed to just use the string
    seperatedWord = list(entry)
    cipherLabeling = Label(ciphering, text = seperatedWord[2]).pack()
    seperatedWordLength = len(seperatedWord)
    cipherLabeling = Label(ciphering, text = seperatedWordLength).pack()
    selection = Label(ciphering, text = choice).pack()

Above is part of the code I have for my ciphering app I am making in Tkinter. Took out the less important parts.

Basically, what is being created in OpenCipher() functions is an entry box that is named cipherEntry. Then there are radio buttons with different names of different ciphers and the value and variable of each radio button is the same as each other for that radio button. Then there is another button that takes whatever cipherEntry is and brings it to another window using the ciphering() function.

What I need to know is how do I also get whatever the value and/or variable of whatever radio button they have selected to that window using the same button they pressed to get to that window ( cipherButton ). Because I want to then use their selection and input to know what cipher type they want their input to be changed to. I already have the function for it sorted.

I have tried using cipherType, cipherChoice, cipherChoices but have no idea how to get them both in there. With the current code above. It works as if there was no second command. It totally disregards whatever selection I put in and the 'selection' label widget doesn't display their choice. I have also made each variable a global to see if that did anything but no luck.

I would really appreciate any assistance :)

CodePudding user response:

First of all, the code should give an error because def ciphering(entry,choice) expects two positional arguments to be passed at the same time. Even after fixing that, it should give another error because cipherChoice is a string(from the list of tuples) and does not have a get attribute.

The thing to focus on here is:

command=lambda: [ciphering(cipherEntry.get()), ciphering(cipherChoice.get())]

When you say something like lambda: [func1(arg1),func1(arg2)] you are set to executing the function func1 and again func1 one after the other(so twice). What you want is to pass multiple arguments to the same function just using a normal lambda without any list, like:

command=lambda: ciphering(cipherEntry.get(), cipherType.get())

Also notice how I changed cipherChoice.get() to cipherType.get(), it is because cipherChoice is a string and also does not have a get attribute, but the value of the radiobutton should be acquired from the associated tkinter variable(StringVar) only. So you have to use cipherType.get()

  • Related