Tkinter文本窗口小部件光标在特定行上
问题描述:
我试图使用Text
小部件实现命令控制台。在下面的代码中,当我点击“返回”键时,插入提示后光标移动到下一行。我无法在提示符下使光标位置。我试图捕获x,y坐标,但它也没有帮助。Tkinter文本窗口小部件光标在特定行上
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()
答
返回'break'
将阻止<Return>
在回调返回后正常处理。
请尝试下面的代码。
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'
谢谢,它的工作。 – sarbjit