@Goyo已經回答了你的問題.我想分享我的貢獻,因為我沒有興趣選擇Entry小部件文本的文本而不用它做任何其他事情.是以,我将為您提供一個髒的MCVE,以顯示您将如何使用所選文本:a)您要删除它還是b)您将複制它.
對于a),以下功能将完成工作:
def select_text_or_select_and_copy_text(e):
e.widget.select_range(0,'end')
它将在您将函數名稱描述的相應事件綁定到條目小部件的條件下工作:
entry.bind('',select_text_or_select_and_copy_text)
entry.bind('',select_text_or_select_and_copy_text)
對于b),您可以使用此功能:
def delete_text(e):
e.widget.delete('0','end')
并将Delete事件綁定到條目小部件:
entry.bind('',delete_text)
我在Ubuntu上嘗試過這個MCVE,它可以工作:
import tkinter as tk
import tkinter.ttk as ttk
def select_text_or_select_and_copy_text(e):
e.widget.select_range(0,'end')
def delete_text(e):
e.widget.delete('0','end')
root = tk.Tk()
entry = ttk.Entry(root)
entry.pack()
entry.bind('',select_text_or_select_and_copy_text)
entry.bind('',delete_text)
root.mainloop()