我有一条错误消息说:
sentences = text.count(".") + text.count("!") + text.count("?")
AttributeError: 'Label' object has no attribute 'count'
我可以使用什么代替.count
来计算物品?
直接访问Label
对象将无法获取它的文本。你需要做text["text"].count
或text.cget("text").count
。要么从标签中提取实际文本。 Tkinter对象可以被视为字典,其中查找键是它们的attributes。 cget
代表“配置获取”,允许您更安全地检索属性。
这是因为对象text
没有实现count()
函数。
假设您所指的text
对象是tkinter Label
,您需要像这样调用count()
:
text["text"].count(string)
所以你发布的行看起来像这样:
sentences = text["text"].count(".") + text["text"].count("!") + text["text"].count("?")
此外,我建议您将变量名称text
更改为更具描述性的其他名称,以避免将来出现混淆。