【发布时间】:2022-08-22 23:13:03
【问题描述】:
我一直在使用 Codemy Youtube 教程使用 iMac 学习 kivy,我目前正在实施拼写检查应用程序。我已经研究了教程中的代码,并且编写了项目所需的所有内容,但是当我运行它并输入要检查的单词并按下按钮时,我收到一条错误消息,指出 \'TypeError: \'NoneType \' 不可调用。当我将鼠标悬停在第 7 行时,从它与导入的 Spelling 相关的外观看,它显然是 NoneType,我不知道为什么,因为它是指定 \ 时菜单中的第一个选项'从 kivy.core.spelling 导入拼写\'。任何建议将不胜感激。这是 py 和 kv 文件。
拼写检查器.py
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.core.spelling import Spelling
Builder.load_file(\'spell_checker.kv\')
class SpellCheckerLayout(Widget):
def press(self):
#Create instance of spelling
s = Spelling()
#Select the language
s.select_language(\'en_US\')
#See language options
print(s.list_languages())
#Grab word from text box
word = self.ids.word_input.text
option = s.suggest(word)
#Update our label
self.ids.word_label.text = f\'{option}\'
class BackgroundApp(App):
def build(self):
#Window.clearcolor = (1,1,1,1)
return SpellCheckerLayout()
if __name__ == \'__main__\':
BackgroundApp().run()
拼写检查器.kv
<SpellCheckerLayout>
BoxLayout:
orientation: \"vertical\"
size: root.width, root.height
Label:
id: word_label
text_size: self.size
halign: \"center\"
valign: \"middle\"
text: \"Enter A Word\"
font_size: 32
TextInput:
id: word_input
multiline: False
size_hint: (1, .5)
Button:
size_hint: (1, .5)
font_size: 32
text: \"Submit\"
on_press: root.press()
-
查看日志文件以查找是否有任何拼写提供程序。
-
您需要模块“python-enchant”。确保已安装它。我测试了你的代码,没有问题,所以一定是这样。
-
那是问题谢谢