python - 如何在 Python 中从突出显示中计算字符串
问题描述
我希望有一种方法可以通过鼠标突出显示来计算字符串。例如,如果用户突出显示句子中的一个词,它将计算其中有多少个字符串并向用户显示。tkinter 有可能吗?
预期输出:它将显示我突出显示的单词中有多少个字符串
解决方案
对的,这是可能的。用于text.selection_get()
获取文本并将其传递给len
.
示例代码:
from tkinter import *
def print_count(event):
if text.tag_ranges('sel'):
print(len(text.selection_get()))
print(len(text.selection_get().split())) # gives count of the number of words highlighted
#print(text.selection_get())
root = Tk()
text = Text(root)
text.bind('<<Selection>>', print_count)
text.pack()
root.mainloop()
推荐阅读
- java - initialContext 内容在哪里定义?
- api - 如何使用 Argo 服务器 API
- react-native - 在模态中反应导航不需要的底部边框半径
- java - 将给定数组中的所有零移动到末尾,并将每个非零元素替换为最接近的较大值(如果有)
- python - 使用 if 和 Python 打印 XML 文件结果
- ssl - CURL 显示“未发送所需的 SSL 证书”前夕
- kubernetes - 让 prometheus/grafana 和 k3s 一起工作
- android - androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
- rasa - 在 RASA 中定义意图和实体
- javascript - 如何在 react-native 中呈现我的下拉菜单?