Python tkinter,label没有属性'count'

问题描述 投票:0回答:2

我有一条错误消息说:

sentences = text.count(".") + text.count("!") + text.count("?")
AttributeError: 'Label' object has no attribute 'count'

我可以使用什么代替.count来计算物品?

python-3.x tkinter
2个回答
1
投票

直接访问Label对象将无法获取它的文本。你需要做text["text"].counttext.cget("text").count。要么从标签中提取实际文本。 Tkinter对象可以被视为字典,其中查找键是它们的attributescget代表“配置获取”,允许您更安全地检索属性。


1
投票

这是因为对象text没有实现count()函数。

假设您所指的text对象是tkinter Label,您需要像这样调用count()

text["text"].count(string)

所以你发布的行看起来像这样:

sentences = text["text"].count(".") + text["text"].count("!") + text["text"].count("?")

此外,我建议您将变量名称text更改为更具描述性的其他名称,以避免将来出现混淆。

© www.soinside.com 2019 - 2024. All rights reserved.