kivy 错误 - kivy.factory.FactoryException: Unknown class <WindowManager>

Posted

技术标签:

【中文标题】kivy 错误 - kivy.factory.FactoryException: Unknown class <WindowManager>【英文标题】:kivy error - kivy.factory.FactoryException: Unknown class <WindowManager> 【发布时间】:2021-10-03 20:22:21 【问题描述】:

这是我的 main.py 代码-

import pyaudio
import speech_recognition as sr
import pyttsx3
from playsound import playsound
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
from kivy.graphics import Color
from kivy.uix.image import Image
from kivy.lang import Builder
kv = Builder.load_file('audibuddy.kv')

class MainWindow(Screen,FloatLayout):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.add_widget(Touch())
class speechtotext(Screen):
    pass
class texttospeech(Screen):
    pass
class WindowManager(ScreenManager):
    pass

class Touch(Widget):            
    def on_touch_down(self,touch):
        print("Mouse down",touch)
    def on_touch_move(self,touch):
        print("Mouse move",touch)
    def on_touch_up(self,touch):
        print("Mouse up",touch)

        

class Audibuddy(App):
    
    def build(self):
        return kv
if __name__  ==  "__main__":
    Audibuddy().run()

这是我的 .kv 文件-

WindowManager:
    MainWindow:
    speechtotext:
    texttospeech:
<MainWindow>:
    GridLayout:
        Button:
            font_size: 40
            color: 1,1,1,1
    
    

    
    
    canvas:
        Color:
            rgba:0.16862745098,0.16862745098,0.16862745098,1
        Rectangle:
            pos:self.pos
            size :self.size
    BoxLayout:
        pos_hint:'top':1.456
        size_hint:2,0.57
            
        canvas:
            Color:
                rgba:0.09803921568,0.13725490196,0.17647058823,1
            Rectangle:
                pos:self.pos
                size:self.size
                    
           
    BoxLayout:
        pos_hint:'left':1324,'top':1.44
        Image:
            source:'image-removebg-preview(6).png'
            allow_strech:True
            keep_ratio:False
                 
    Button:
        id:settings_btn
        background_normal:"image-removebg-preview(10).png"
        background_down:"image-dark.png"
        allow_strech:True
        keep_ratio:False
        pos_hint:"top":1,"right":0.99
        size_hint: 0.09,0.09
                                            
    Label:
        text:"AudiBuddy"
        pos_hint:"left":1, "top":1
        font_size:40
    Button:
        id:speechtotext_btn
        text:"Speech to Text"
        pos_hint:"x":0.15,"top":0.7
        size_hint: 0.7,0.1
        background_colour:0.3,0.2,0,5,1
    Button:
        id:texttospeech_btn
        text:"Text to Speech"
        pos_hint:"x":0.15,"top":0.55
        size_hint: 0.7,0.1
    Button:
        id:exitapp_btn
        text:"Exit Application"
        pos_hint:"x":0.15,"top":0.4
        size_hint: 0.7,0.1
<speechtotext>:
<texttospeech>:

它给了我这个错误:

File "C:\Users\admin_temp\Documents\GitHub\audibuddy\main.py", line 16, in <module>
     kv = Builder.load_file('audibuddy.kv')
   File "D:\Program Files\anaconda\lib\site-packages\kivy\lang\builder.py", line 306, in load_file
     return self.load_string(data, **kwargs)
   File "D:\Program Files\anaconda\lib\site-packages\kivy\lang\builder.py", line 404, in load_string
     widget = Factory.get(parser.root.name)(__no_builder=True)
   File "D:\Program Files\anaconda\lib\site-packages\kivy\factory.py", line 146, in __getattr__
     raise FactoryException('Unknown class <%s>' % name)
 kivy.factory.FactoryException: Unknown class <WindowManager>

我无法弄清楚我的代码有什么问题。我是 kivy 的新手,这是我的第一个应用程序。我正在尝试将我之前设计的主窗口 ui 放在 MainWindow 类中。 另外,谁能快点告诉我答案,因为我明天必须提交项目。

编辑:我稍微编辑了代码,现在看起来像这样 main.py

import pyaudio
import speech_recognition as sr
import pyttsx3
from playsound import playsound
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
from kivy.graphics import Color
from kivy.uix.image import Image
from kivy.lang import Builder
kv = Builder.load_file('audibuddy.kv')

class Touch(Widget):            
    def on_touch_down(self,touch):
        print("Mouse down",touch)
    def on_touch_move(self,touch):
        print("Mouse move",touch)
    def on_touch_up(self,touch):
        print("Mouse up",touch)
class MainWindow(Screen):
    pass
class speechtotext(Screen):
    pass
class texttospeech(Screen):
    pass
class WindowManager(ScreenManager):
    pass



class Audibuddy(App):
    
    def build(self):
        return kv
if __name__  ==  "__main__":
    Audibuddy().run()

和 .kv 文件 -

WindowManager:
    MainWindow:
    speechtotext:
    texttospeech:
<MainWindow>:
    Button:
            font_size: 40
            color: 1,1,1,1
    
    

    
    
    canvas:
        Color:
            rgba:0.16862745098,0.16862745098,0.16862745098,1
        Rectangle:
            pos:self.pos
            size :self.size
    BoxLayout:
        pos_hint:'top':1.456
        size_hint:2,0.57
            
        canvas:
            Color:
                rgba:0.09803921568,0.13725490196,0.17647058823,1
            Rectangle:
                pos:self.pos
                size:self.size
                    
           
    BoxLayout:
        Label:
            text:"audibuddy"
            pos_hint"top":1,"left":1
        
                 
    Button:
        id:settings_btn
        background_normal:"image-removebg-preview(10).png"
        background_down:"image-dark.png"
        allow_strech:True
        keep_ratio:False
        pos_hint:"top":1,"right":0.99
        size_hint: 0.09,0.09
                                            
    Label:
        text:"AudiBuddy"
        pos_hint:"left":1, "top":1
        size_hint:10,10
        font_size:40
    Button:
        id:speechtotext_btn
        text:"Speech to Text"
        pos_hint:"x":0.15,"top":0.7
        size_hint: 0.7,0.1
        background_colour:0.3,0.2,0,5,1
    Button:
        id:texttospeech_btn
        text:"Text to Speech"
        pos_hint:"x":0.15,"top":0.55
        size_hint: 0.7,0.1
    Button:
        id:exitapp_btn
        text:"Exit Application"
        pos_hint:"x":0.15,"top":0.4
        size_hint: 0.7,0.1
<speechtotext>:
<texttospeech>:

但现在我得到一个不同的错误 -

File "C:\Users\admin_temp\Documents\GitHub\audibuddy\main.py", line 16, in <module>
     kv = Builder.load_file('audibuddy.kv')
   File "D:\Program Files\anaconda\lib\site-packages\kivy\lang\builder.py", line 306, in load_file
     return self.load_string(data, **kwargs)
   File "D:\Program Files\anaconda\lib\site-packages\kivy\lang\builder.py", line 373, in load_string
     parser = Parser(content=string, filename=fn)
   File "D:\Program Files\anaconda\lib\site-packages\kivy\lang\parser.py", line 402, in __init__
     self.parse(content)
   File "D:\Program Files\anaconda\lib\site-packages\kivy\lang\parser.py", line 511, in parse
     objects, remaining_lines = self.parse_level(0, lines)
   File "D:\Program Files\anaconda\lib\site-packages\kivy\lang\parser.py", line 614, in parse_level
     _objects, _lines = self.parse_level(
   File "D:\Program Files\anaconda\lib\site-packages\kivy\lang\parser.py", line 674, in parse_level
     if current_property[:3] == 'on_':
 TypeError: 'NoneType' object is not subscriptable

【问题讨论】:

【参考方案1】:

这可能是因为您使用的是 anaconda 如果您尝试使用其他替代方法

【讨论】:

您好,感谢您的回复,但我已经解决了。我刚刚将 .kv 文件从 audibuddy.kv 重命名为 main.kv 并以某种方式修复了它。【参考方案2】:

朋友不要这样做

    class Audibuddy(App):

        def build(self):
            return kv

你需要做的就是从那里删除构建功能

    class Audibuddy(App):

        pass 

如果这不起作用,那么只需创建一个与您的项目同名的 kv 文件并做同样的事情,我希望这会有所帮助,并且对于糟糕的解释非常抱歉

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于kivy 错误 - kivy.factory.FactoryException: Unknown class <WindowManager>的主要内容,如果未能解决你的问题,请参考以下文章

Kivy:[错误] [AudioGstplayer] 找不到资源

类型错误:没有名为“kivy._clock”的模块

Kivy - 错误无法获得权限?

Kivy'对象没有属性'错误

kivy 错误 - kivy.factory.FactoryException: Unknown class <WindowManager>

kivy & plyer 通知错误