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()
以上是我在 Tkinter 中制作的加密應用程式的部分代碼。取出了不太重要的部分。
基本上,在 OpenCipher() 函式中創建的是一個名為 cipherEntry 的輸入框。然后是具有不同名稱的不同密碼的單選按鈕,并且每個單選按鈕的值和變數對于該單選按鈕彼此相同。然后是另一個按鈕,它可以使用 cipherEntry 并使用 ciphering() 函式將其帶到另一個視窗。
我需要知道的是,如何使用他們按下以進入該視窗的相同按鈕( cipherButton )將他們選擇的任何單選按鈕的值和/或變數獲取到該視窗。因為我想然后使用他們的選擇和輸入來了解他們希望將輸入更改為什么密碼型別。我已經對它的功能進行了排序。
我嘗試過使用 cipherType、cipherChoice、cipherChoices,但不知道如何將它們都放在那里。使用上面的當前代碼。它就像沒有第二個命令一樣作業。它完全無視我輸入的任何選擇,并且“選擇”標簽小部件不顯示他們的選擇。我還將每個變數都設為全域變數,看看是否有任何作用,但沒有運氣。
我真的很感激任何幫助:)
uj5u.com熱心網友回復:
首先,代碼應該給出一個錯誤,因為def ciphering(entry,choice)需要同時傳遞兩個位置引數。即使在修復它之后,它也應該給出另一個錯誤,因為cipherChoice它是一個字串(來自元組串列)并且沒有get屬性。
這里要重點關注的是:
command=lambda: [ciphering(cipherEntry.get()), ciphering(cipherChoice.get())]
當您說類似lambda: [func1(arg1),func1(arg2)]您設定為執行該功能時,一個接一個地func1再次執行func1(所以兩次)。您想要的是僅使用沒有任何串列的普通函式將多個引數傳遞給同一個函式,例如: lambda
command=lambda: ciphering(cipherEntry.get(), cipherType.get())
另請注意我如何更改cipherChoice.get()為cipherType.get(),這是因為它cipherChoice是一個字串并且也沒有get屬性,但是單選按鈕的值應該僅從關聯的 tkinter 變數(StringVar)中獲取。所以你必須使用cipherType.get()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/486915.html
