简体   繁体   中英

How to change cursor position in Python tkinter Entry widget at every inserted and deleted values

I have one Entry field and popup Keypad window.Whenever user clicked on entry field it accept input through Keypad. One of the problem which I faced it whenever I am reading current cursor position while entering user input it always showing cursor position at 'zero' and also user is not seeing any cursor in entry field while entering input through Keypad.

Code:

from tkinter import *
from tkinter import simpledialog

class Gui(Toplevel):
    def __init__(self, parent,*args):
        Toplevel.__init__(self, parent)
        self.title('User input window')
        self.geometry('450x350')
        self.EntryVar=StringVar()
        self.label = Label(self, text='User Input:', width=15, background='white', justify=CENTER,
                                  font='-weight bold')
        self.entry = Entry(self, width=15, background='white', textvariable=self.EntryVar,
                                  font='-weight bold')
        self.label.grid(row=2,padx=10, pady=5, column=0, sticky='E')
        self.entry.grid(row=2,padx=10, pady=5, column=1, sticky='E')
        self.entry.bind('<FocusIn>', self.keypad_popup)

    def keypad_popup(self,event):
        new = numPad(self, event.widget)

class numPad(simpledialog.Dialog):
    def __init__(self, parent, *args):
        self.focus = args[0]
        self.parent = parent
        self.top = Toplevel(parent)
        self.top.title('Keypad')
        self.top.grab_set()
        self.createWidgets()

    def createWidgets(self):
        btn_list = ['6', '1', '2', '3', '0','C', 'D']
        r = 1
        c = 0
        n = 0
        btn = []
        for label in btn_list:
            cmd = lambda x=label: self.click(x)
            button = Button(self.top, text=label, width=10, height=5, command=cmd)
            btn.append(button)
            btn[-1].grid(row=r, column=c)
            n += 1
            c += 1
            if c == 3:
               c = 0
               r += 1

    def click(self, label):
        if label == 'D' and self.focus == self.parent.entry:
            currentText = self.parent.EntryVar.get()
            self.parent.EntryVar.set(currentText[:-1])

        elif label == 'C':
            self.top.destroy()
            self.top.master.focus()

        elif self.focus == self.parent.entry:
            currentText = self.parent.entry.get()
            self.parent.EntryVar.set(currentText + label)
            print('Cursor position:', self.parent.entry.index(INSERT))


if __name__ == '__main__':
    root = Tk()
    root.title('root window')
    root.geometry("150x150")
    app = Gui(root)
    root.mainloop()

Setting the value via the associated variable doesn't change the cursor position, unless the previous position was beyond the end of the new value. Since the cursor position starts at 0 and you don't change it, it's always going to be zero.

If you want to make sure the insertion cursor is at the end after you change the value, you can use the icursor method.

Example:

self.parent.entry.icursor("end")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM