Python 如何在tkinter中将用户输入的文本转换为小写?

Python 如何在tkinter中将用户输入的文本转换为小写?,python,tkinter,Python,Tkinter,这是我正在构建的翻译应用程序的延续。我希望将用户键输入到输入字段中的任何文本都应转换为小写,以便与python字典中的键匹配。拜托,我该怎么做?谢谢代码如下: from tkinter import * import tkinter. messagebox root=Tk() root.geometry('250x250') root.title("Meta' Translator") root.configure(background="#35424a"

这是我正在构建的翻译应用程序的延续。我希望将用户键输入到输入字段中的任何文本都应转换为小写,以便与python字典中的键匹配。拜托,我该怎么做?谢谢代码如下:

from tkinter import *
import tkinter. messagebox
root=Tk()
root.geometry('250x250')
root.title("Meta' Translator")
root.configure(background="#35424a")

#Entry widget object
textin = StringVar()

#press ENTER key to activate translate button
def returnPressed(event):
  clk()

def clk():
    entered = ent.get()
    output.delete(0.0,END)
    try:
        textin = exlist[entered]
    except:
        textin = 'Word not found'
    output.insert(0.0,textin)

#heading
lab0=Label(root,text='Translate English Words to Meta\'',bg="#35424a",fg="silver",font=('none 11 
bold'))
lab0.place(x=0,y=2)

#Entry field
ent=Entry(root,width=15,font=('Times 18'),textvar=textin,bg='white')
ent.place(x=30,y=30)

#focus on entry widget
ent.focus()

#Search button
but=Button(root,padx=1,pady=1,text='Translate',command=clk,bg='powder blue',font=('none 18 
bold'))
but.place(x=60,y=90)

#press ENTER key to activate Translate button
root.bind('<Return>', returnPressed)

#output field
output=Text(root,width=15,height=1,font=('Times 18'),fg="black")
output.place(x=30,y=170)

#prevent sizing of window
root.resizable(False,False) 

#Dictionary
exlist={
    "hat":"ɨ̀də̀m", 
    "hoe":"əsɔ́",
    "honey":"jú",
    "chest":"ɨgɔ̂",
    "eye":"ɨghə́",
    "ear":"ǝ̀tǒŋ",
    }

root.mainloop()
从tkinter导入*
输入tkinter。对话框
root=Tk()
根几何体('250x250')
根标题(“元”译者)
root.configure(后台=“#35424a”)
#条目小部件对象
textin=StringVar()
#按回车键激活翻译按钮
def RETURN按下(事件):
clk()
def clk():
entered=ent.get()
输出.删除(0.0,结束)
尝试:
textin=exlist[输入]
除:
textin='找不到单词'
输出.插入(0.0,文本输入)
#标题
lab0=Label(root,text='Translate English Words to Meta\'',bg=“35424a”,fg=“silver”,font=('none 11
粗体('))
lab0.位置(x=0,y=2)
#输入字段
ent=Entry(根,宽度=15,字体=('Times 18'),textvar=textin,bg='white')
耳鼻喉部位置(x=30,y=30)
#关注入口小部件
ent.focus()
#搜索按钮
但是=按钮(root,padx=1,pady=1,text='Translate',command=clk,bg='powder blue',font=('none'18
粗体('))
但是.地点(x=60,y=90)
#按回车键激活翻译按钮
root.bind(“”,按返回键)
#输出字段
输出=文本(根,宽=15,高=1,字体=('Times 18'),fg=“黑色”)
输出位置(x=30,y=170)
#防止调整窗口大小
根目录。可调整大小(False,False)
#字典
exlist={
“帽子”:“d̀d̀m”,
“锄头”:“锄头”,
“亲爱的”:“jú”,
“胸部”:“ɨgɔ770;”,
“眼睛”:“ɨghə́”,
“耳朵”:“耳朵到耳朵”,
}
root.mainloop()

Python的内置字符串类支持
.lower()
方法。它循环字符串中的每个字符,并将其转换为小写,除非它是数字/特殊字符。因此,在您的情况下,您需要在设置变量(使用用户输入)之后执行
entered=entered.lower()

Python的内置字符串类支持
.lower()
方法。它循环字符串中的每个字符,并将其转换为小写,除非它是数字/特殊字符。因此,在您的情况下,您需要在设置变量(使用用户输入)之后执行
entered=entered.lower()

将lower()链到ent.get()

将lower()链接到ent.get()


.lower()
.lower()
完美。成功了。谢谢,太好了。成功了。谢谢
def clk():
    entered = ent.get().lower()
    output.delete(0.0,END)
    try:
        textin = exlist[entered]
    except:
        textin = 'Word not found'
    output.insert(0.0,textin)