如何计算字母数并在 tkinter 文本小部件中排文本? [英] How to count the number of letters and row a text in tkinter text widget?

查看:28
本文介绍了如何计算字母数并在 tkinter 文本小部件中排文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算字母的数量并在 tkinter 文本小部件中排列文本.这是我的示例代码:

I want to count the number of letters and row a text in tkinter text widget. This is my sample code:

from tkinter import *

def letter_refresh():
    pass   

def row_refresh():
    pass

app=Tk()

label=Label(text='writing')
label.grid(row=1, column=0)

text = Text( )
text.insert(1.0,"abcd\nefgh\n\nijkl\nmnop")
text.grid(row=1,column=1,padx=5, pady=20)


letter=Button(text='letter_count', command = letter_refresh)
letter.grid(row=2, column=0,padx=1, pady=1)

letter_display=Entry()
letter_display.grid(row=2, column=1,  padx=5, pady=5,sticky=W)



row=Button(text='row_count', command = row_refresh)
row.grid(row=3, column=0,padx=1, pady=1)

row_display=Entry()
row_display.grid(row=3, column=1,  padx=5, pady=5,sticky=W)

app.mainloop()

我想letter_count结果是16,如果letter_count botton push.

I want to letter_count result is 16, if letter_count botton push.

我想row_count结果是4,如果row_count botton push.

I want to row_count result is 4, if row_count botton push.

推荐答案

条目的文本只不过是一个字符串.

An entry's text is nothing more than a string.

要计算可以在 \n 上拆分的行数,并检查返回列表的 len.

To count the rows you can split on \n and check the len of the returned list.

要计算字母,只需检查字符串的 len.您可能希望从该值中减去 '\n' 的数量(len(string) - string.count('\n')).

To count the letters simply check the len of the string. You will probably want to subtract the number of '\n' from that value (len(string) - string.count('\n')).

这篇关于如何计算字母数并在 tkinter 文本小部件中排文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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