我想制作它,以便它从用户那里获取输入并进行更改,但仅更改最后一个字母
我有很多次要更正此错误,但是由于某些原因它不起作用
from tkinter import *
import random
window = Tk()
window.title("Enigma Ui")
lbl = Label(window, text='''Welcome
''',font=("Comic Sans", 16))
lbl.grid(column=0, row=0)
window.geometry('350x200')
def clicked():
res = "" + txt.get()
keyword5 = ["a"]
if any(keyword in res for keyword in keyword5):
lbl.configure(text= "h")
keyword6 = ["b"]
if any(keyword in res for keyword in keyword6):
lbl.configure(text= "j")
btn = Button(window, text="Encrypt", bg="light blue", command = clicked)
btn.grid(column=20, row=30)
txt =Entry(window,width=10)
txt.grid(column=14,row=30)
window.mainloop()
我希望它接受用户输入并更改所有字母,而不仅仅是一个
答案 0 :(得分:1)
问题出在您单击的函数中,当您调用lbl.configure()时,您将始终只返回单个字母h或j。
以下是可能的其他点击功能:
def clicked():
res = "" + txt.get()
# define a dictionary to match keywords to their encrypted letter
keywords = {'a': 'h',
'b': 'j'}
new_label_value = res
# use the string replace function to encrypt matching letters in a loop
for keyword, encrypted in keywords.items():
new_label_value = new_label_value.replace(keyword, encrypted)
lbl.configure(text=new_label_value)
这将覆盖循环中的关键字字母并返回新字符串。