tkinter - Tkinter 小部件文本:计算行数
问题描述
在这个简短的程序中,我想知道需要显示多少行消息。问题是,它没有回答我这里需要 3 行,而是给了我 1。为什么?我尝试了有关该主题的不同解决方案,但没有人在工作,每个人都有一些问题(这里没有计算行数)。
我在 Windows 10 上使用 Python 3.9.2
谢谢您的帮助 !!!
from tkinter import *
root = Tk()
text = Text(root, width = 12, height = 5, wrap = WORD)
text.insert(END, 'This is an example text.')
text.pack()
print(int(text.index('end-1c').split('.')[0]))
root.mainloop()
解决方案
改编自这里:
from tkinter import *
root = Tk()
text = Text(root, width = 12, height = 5, wrap = WORD)
text.insert(END, 'This is an example text.')
text.pack()
root.update()
# Note `text.count("0.0", "end", "displaylines")` returns a tuple like this: `(3, )`
result = text.count("0.0", "end", "displaylines")[0]
print(result)
root.mainloop()
推荐阅读
- javascript - CodeMirror 文本区域中的 onresize 事件
- mysql - 如何修复 dBeaver 的身份验证插件错误
- angular - 从内部文件导入成员会抛出“Module not found: Error: Can't resolve”
- html - jQuery - 将点击的 HTML 元素的属性放入图像的 src
- python - ImportError: DLL load failed while importing _pywrap_tensorflow_internal: 动态链接库 (DLL) 初始化例程失败
- java - 正则表达式替换整个单词,而不是字符
- javascript - 获取条纹支付意图方法错误
- c# - 用于匹配来自多层嵌套集合的对象的 Linq 谓词
- javascript - 需要将 Windows 身份验证添加到基本 HTML/JavaScript 站点
- java - 为什么 Stream.Builder 同时具有 add 和 accept 方法?