TK 文本小部件索引没有正确计算行数? [英] TK text widget index not properly counting lines?

查看:23
本文介绍了TK 文本小部件索引没有正确计算行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了下面的代码并使用 index 方法来尝试获取文本框中包含的文本行数,但我一直返回 1 行而不是文本框中存在的 2 行.

I have tried the code below and used the index method to try to get the number of lines of text contained within the text box but i keep getting 1 line returned instead of the 2 that are present in my text box.

from Tkinter import *

master = Tk()

text = Text(master,width=40,height=50,wrap='word')
text.insert('1.0', 'Here is my text to insert, next i want to test the word wrap')
text.tag_add('font', '1.0', 'end')
text.tag_configure('font', font='arial 12', relief='raised')
text.pack()
print int(text.index('end-1c').split('.')[0])

mainloop()

推荐答案

它只计算物理行,不计算已换行的行.你只插入了一行,所以它只计算一行.换行对行数没有影响.

It only counts physical lines, not lines that have wrapped. You've only inserted one line, so it's only counting one line. Wrapping has no effect on the number of lines.

这篇关于TK 文本小部件索引没有正确计算行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆