I am trying to shift the cursor of an Entry widget to the left, upon pressing the button. With what I've tried I can get the cursor's current position, but I can't move it.
I  searched online but I didn't find anything useful. How can I move the cursor position in an Entry widget?
#! /usr/bin/python
from Tkinter import *
class App(Frame):
    def __init__(self,parent):
        Frame.__init__(self,parent)
        self.parent=parent
        self.button = Button(self.parent, text="Shift_cursor_left", fg="red")
        self.button.pack(side=LEFT)
        self.entry_label=Entry(self.parent,width=10,bd="1",bg="cyan",font=("Helvetica",15),text="python",justify=RIGHT)
        self.entry_label.pack()
        self.entry_label.focus()
        self.entry_label.insert(0,"Python")
        self.button["command"]=self.shift_cursor()
    def shift_cursor(self):
        position = self.entry_label.index(INSERT)
        print position
        # self.entry_label.mark_set(INSERT,'1.2')
root=Tk()
app=App(root)
root.mainloop()
mark_set is a method for a Text widget, for an Entry widget use the icursor method:
self.entry_label.icursor(0)
Also, a Button's command should be a function reference, not a function call, so change
self.button["command"]=self.shift_cursor()
to
self.button["command"]=self.shift_cursor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With