這個問題在這里已經有了答案: 如何在按下按鈕時更改 Tkinter 標簽文本 2 個答案 昨天關門。
# 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)
以上是我正在創建的程式的一部分。我正處于它的最后階段,并且在嘗試將加密的單詞放到視窗上時遇到了麻煩。
在用戶在 cipherEntry 中輸入內容并單擊 cipherButton 后,我試圖讓 cipheredWord 成為 cipheredMessage 中的文本。輸入進入 cipheringProcess,它決定了它將通過什么加密程序。在多個函式名稱中(無論選擇什么密碼)_cipher()。
我找不到一種方法可以將 cipheredWord 從 binary_cipher() 獲取為 openCipher() 中的 cipheredMessage。
我曾嘗試使用:
- 使用 ["text"] = cipheredWord
- .config
- 。放
- 字串變數()
如果有人可以給我一種方法,我將不勝感激!提前感謝任何能夠幫助我的人
uj5u.com熱心網友回復:
# 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
uj5u.com熱心網友回復:
我認為您只需添加一個函式來更改用于將文本分配給標簽的變數。
def changelabel():
status="button clicked"
return status
cipheredMessage = Label(cipher, text=changelabel() , padx=5, pady=2).pack()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487542.html
下一篇:AWK變數和函式語法
