2013-08-07 96 views
0

我試圖使用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() 

回答

1

返回'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' 
+0

謝謝,它的工作。 – sarbjit

相關問題