DTambah
DTambah

Reputation: 69

How do I convert user input text to lowercase in tkinter?

This is a continuation of a translator app I'm building. I want that whatever text the user keys into the entry field it should be converted to lowercase, so that it would match the keys in my python dictionary. Please, how do I do it? Thanks. Below is the code:

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()

Upvotes: 0

Views: 993

Answers (2)

norie
norie

Reputation: 9857

Chain lower() to ent.get().

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)

Upvotes: 0

TheLizzard
TheLizzard

Reputation: 7680

Python's built-in string class supports the .lower() method. It loops over each character in the string and converts it into lower case unless it is a number/special character. So in your case, you need to do entered = entered.lower() just after you set the variable (with the user input).

Upvotes: 2

Related Questions