The following code creates a ScrolledText box which the user can type into. They can also choose one of the buttons to enter a Spanish Language character.
from functools import partial
import tkinter as tk
from tkinter import scrolledtext
root = tk.Tk()
root.title("Test")
root.geometry('950x700')
root.configure(background='ivory3')
frame_button=tk.Frame(root, background='ivory3')
frame_button.grid(row=0,column=0, pady=2)
def insert_char(char):
"""When the user presses one of the spanish language character buttons
put the correct character in the textw box. """
try:
my_focus = str(root.focus_get())
if char == "á":
textw.insert("end","á")
elif char == "é":
textw.insert("end","é")
except Exception as ex:
txt = "ERROR: insert_char(): " + str(ex)
clear_txt()
textw.insert(tk.END, txt) # Write the error text onto the screen
return(-1)
# GUI Box that the user can either type or paste text to be read into.
textw = scrolledtext.ScrolledText(root,width=90,height=21)
textw.grid(row=1, column=0)
textw.config(background="light grey", foreground="black",
font="Times 14 bold", wrap='word', relief="sunken", bd=5)
textw.delete('1.0', tk.END) # Delete any old text on the screen
textw.update() # Clear the screen.
Button_AA=tk.Button(frame_button,width=4,bg="grey",fg="white",text="á",
font="Times 12 bold", relief=tk.RAISED, bd=10,
command=partial(insert_char, "á"))
Button_AE=tk.Button(frame_button,width=4,bg="grey",fg="white",text="é",
font="Times 12 bold", relief=tk.RAISED, bd=10,
command=partial(insert_char, "é"))
Button_AA.grid(row=0,column=0)
Button_AE.grid(row=0,column=1)
root.mainloop()
Say the user types in 'Hable con ella.' and they realize it should be 'Hablé con ella.' If they click right after the e and hit backspace I want them to be able to click on the é button to fix the word. The problem is that the é will be placed at the end of the sentence, giviing me 'Habl con ella.é'
How do I get my button to place the char where the curser is?
You are explicitly adding the text to the end. If you want to insert at the insertion cursor, use the index "insert" rather than "end".
textw.insert("insert","á")