【发布时间】:2011-05-22 21:41:36
【问题描述】:
在this page describing the Tkinter text widget,据说 '选择是一个名为SEL(或“sel”)的特殊标签,对应于当前选择。您可以使用常量 SEL_FIRST 和 SEL_LAST 来引用选择。如果没有选择,Tkinter 会引发 TclError 异常。'
我的问题:有没有更有效的方法来判断 Text 小部件中是否有选择,除了愚弄异常,如下面的代码?
seltext = None
try:
seltext = txt.get(SEL_FIRST, SEL_LAST)
except TclError:
pass
if seltext:
# do something with text
【问题讨论】:
标签: python text tkinter selection