I want to be able to change the user input of an apostrophe (')
to this backquote symbol (`)
because I'm using the user input to ask a MySQL database for the information the user asks for, and MySQL strings either start or end with apostrophe. This means that if a user types one in and python tries to do anything with it, there ends up being an error so I just want to be able to change the (')
to a (`)
when the user types it into the entry box and to show it in the entry box but I am having trouble with this. I don't want to change anything to the MySQL database so I just want python to fix this issue itself.
from tkinter import * # allows for the use of tkinter GUI
win = Tk() # creates the tkinter window
win.title("test") # sets the name of the window
win.geometry("300x200") # sets the size of the window
win.resizable(False, False) # prevents the window from being resized
def changeto(e):
nsearchentry.insert(-1,'hi')
print(nsearchentry)
nsearchentry = Entry(win, width=20, font=('Arial', 12)) # sets up the nsearch entry box and its parameters
nsearchentry.pack() # moves the nsearchentry box to its location
nsearchentry.bind("'", changeto)
win.mainloop()
If you want to convert inside (User won't know) then at last (Just before you want to use that data) use .replace()
.
Example code is :
data=nsearchentry.get()
data=data.replace("'","`")
#Here you want to use that data
Or if you want it in real-time then try this :
def ch():
x=nsearchentry.get()
nsearchentry.delete(0,END)
# We have to delete to make sure same text won't overwrite again
nsearchentry.insert(0,x.replace("'","`"))
nsearchentry.bind("'", lambda e: win.after(1,ch))
# 1 Millisecond delay just to make sure that ' is written in Entry
Trick Idea :
def ch():
x=nsearchentry.get()
nsearchentry.delete(0,END)
nsearchentry.insert(0,x[0:-1]+"`")
# Remove ' and add `
nsearchentry.bind("'", lambda e: win.after(1,ch))