【发布时间】:2021-07-16 23:07:49
【问题描述】:
有什么方法可以在 tkinter 中检测 Entry 小部件中的选择,就像我们在 Text 小部件 txt.get(SEL_FIRST, SEL_LAST) 和 txt.tag_ranges("sel") 中得到的一样
【问题讨论】:
有什么方法可以在 tkinter 中检测 Entry 小部件中的选择,就像我们在 Text 小部件 txt.get(SEL_FIRST, SEL_LAST) 和 txt.tag_ranges("sel") 中得到的一样
【问题讨论】:
如果您只想检查选择是否存在,您可以使用Entry.select_present() 方法,如果选择存在则返回True。
如果您想获得选择,可以使用Entry.selection_get()
小例子:
from tkinter import *
def selection_present():
print(entry.select_present())
if entry.select_present():
print(entry.selection_get())
root = Tk()
entry = Entry(root)
entry.pack()
Button(root, text="Check selection", command=selection_present).pack()
root.mainloop()
【讨论】: