简体   繁体   中英

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()

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

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