简体   繁体   中英

Tkinter text widget cursor on particular line

I am trying to implement command console using Text widget. In the below code, when I hit "return" key, cursor moves to next line after prompt is inserted. I am not able to make cursor position at the prompt. I tried capturing the x,y co-ordinates but it is also not helping.

from Tkinter import *

def getCommand(*args):
    global text
    x_pos = text.xview()[0]
    y_pos = text.yview()[0]
    text.insert(END, "\n")
    text.insert(END, "command>")

root = Tk()
text = Text(root)
text.pack()
text.insert(END,"command>")
text.focus()
text.bind("<Return>",getCommand)

root.mainloop()

Return 'break' will prevent <Return> processed normally after the callback return.

Try following code.

def getCommand(*args):
    global text
    x_pos = text.xview()[0]
    y_pos = text.yview()[0]
    command = text.get('insert linestart', 'insert').replace('command>', '', 1)
    print command
    text.insert(END, "\n")
    text.insert(END, "command>")
    return 'break'

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