Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing position of cursor in Entry widget

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()
like image 437
Deepworks Avatar asked Sep 17 '25 20:09

Deepworks


1 Answers

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
like image 63
fhdrsdg Avatar answered Sep 19 '25 11:09

fhdrsdg