python - 如何在 tkinter 中将用户输入文本转换为小写?
问题描述
这是我正在构建的翻译应用程序的延续。我希望用户在输入字段中键入的任何文本都应转换为小写,以便与我的 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()
解决方案
Python 的内置字符串类支持该.lower()
方法。它遍历字符串中的每个字符并将其转换为小写,除非它是数字/特殊字符。因此,在您的情况下,您需要entered = entered.lower()
在设置变量(使用用户输入)之后执行此操作。
推荐阅读
- linux - Bash 脚本从脚本输出到文件失败,但在 bash 中有效
- perl - 加入数组元素,然后从结果中删除空格
- android - 从图库中选择图像并上传到我的计算机/服务器上的数据存储
- dart - Flutter Firestore 服务器端时间戳
- postgresql - 无法绑定 IPv4 套接字:权限被拒绝
- javascript - 弹出模式仅适用于 angularjs 中的第一项
- wordpress - Wordpress:如果用户角色 = CSS-Class THEN 显示:无
- c++14 - 使用模板参数值定义的参数数量调用函数
- xml - 导入 XML 以访问 vba 在文件中添加结束标记
- java - 从 JAVA 邮件发送到 IMAP 邮箱时,附件文件名得到更改