You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy中已定义ShowCurrentMeds类却仍报FactoryException: Unknown class <ShowCurrentMeds>错误求助

Kivy中已定义ShowCurrentMeds类却仍报FactoryException: Unknown class 错误求助

我已经把这个问题翻来覆去检查了好多遍,看其他人遇到类似问题都是因为KV文件和Python文件里没有同时定义对应的类,但我明明两边都加了ShowCurrentMeds类,却还是收到这个错误提示。

这个问题在我程序的好几个地方都出现了,最近的一次报错是:

raise FactoryException('Unknown class <%s>' % name) kivy.factory.FactoryException: Unknown class <ShowCurrentMeds>

下面是我这部分的KV代码:

<ShowCurrentMeds>:
    name: 'ShowCurrentMeds'
    RecycleDataViewBahavior:
    # scroll layout of buttons
    # shows med 

对应的Python代码:

class ShowCurrentMeds(RecycleDataViewBehavior):
    def currentmeds(self,meds):
        def edit(self, instance):
            EditMedPage.editmed_enter(temp[8])
            ScreenManager.current = 'EditMedPage'
        dates = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}

        meds = MedsPage.meds
        for med in range(len(meds[0])):
            temp = meds[0][med]
            currentmed = DropDown()
            # med name
            currentmed.add_widget(Label(text=temp[0]))
            if temp[1] is not None:
                # brand
                currentmed.add_widget(Label(text=temp[1]))
            if temp[2] is not None:
                # type of med
                currentmed.add_widget(Label(text=temp[2]))
            if temp[3] is not None:
                # explanation
                currentmed.add_widget(Label(text=temp[3]))
            # year
            if temp[4] is not None:
                # month
                if temp[5] is not None:
                    # date
                    if temp[6] is not None:
                        currentmed.add_widget(Label(text='Started '+temp[6]+'/'+temp[5]+'/'+temp[4]))
                    currentmed.add_widget(Label(text='Started '+dates.get(temp[5])+' '+temp[4]))
                currentmed.add_widget(Label(text= 'Started '+temp[4]))
            if temp[7] is not None:
                # end date
                currentmed.add_widget(Label(text='Ends '+temp[7]))

            thereisatime = False
            doselayout = GridLayout(cols=2)
            for time in range(len(meds[1])):
                temptime = meds[1][time]
                if temptime[0] == temp[0]:
                    thereisatime = True
                    doselayout.add_widget(Label(text=temptime[1]))
                    # time
                    doselayout.add_widget(Label(text=temptime[2]))
                    # dose
            if thereisatime:
                currentmed.add_widget(doselayout)
            
            editbutton = Button(text='Edit')
            editbutton.bind(on_press=edit)
            currentmed.add_widget(editbutton)

            medbutton = Button(text=temp[1])
            medbutton.bind(on_release=currentmed.open)

这个类是被下面这段代码调用的:

class MedsPage(Screen):
    def med_enter(self):
        meds = json.loads(requests.get('http://CyanUnicorn26.eu.pythonanywhere.com/meds/current/main/', json={'SessionID':app.sessionID}).text)
        ShowCurrentMeds.currentmeds(meds)

两边都有这个类了,我实在搞不懂为什么还会报错?提前谢谢大家帮忙,要是只是个低级错误我先道歉,我经常犯这种错,但现在四个不同的屏幕类都出现这个问题,感觉可能是哪里有共性问题?

完整的错误信息(隐私相关内容已修改):

File "c:\Users\...\SpoonieTracker code breaking.py", line 647, in <module>
 app.run()
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\app.py", line 955, in run
     self._run_prepare()
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\app.py", line 924, in _run_prepare
     self.load_kv(filename=self.kv_file)
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\app.py", line 697, in load_kv
     root = Builder.load_file(rfilename)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\lang\builder.py", line 310, in load_file
     return self.load_string(data, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\lang\builder.py", line 412, in load_string
     self._apply_rule(
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\lang\builder.py", line 665, in _apply_rule
     child.apply_class_lang_rules(
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\uix\widget.py", line 470, in apply_class_lang_rules   
     Builder.apply(
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\lang\builder.py", line 545, in apply
     self._apply_rule(
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\lang\builder.py", line 667, in _apply_rule
     self._apply_rule(
   File "C:\Users\Me\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\lang\builder.py", line 625, in _apply_rule
     cls = Factory_get(cname)
         ^^^^^^^^^^^^^^^^^^
   File "C:\Users\Ronnie\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\factory.py", line 147, in __getattr__
     raise FactoryException('Unknown class <%s>' % name)
 kivy.factory.FactoryException: Unknown class <ShowCurrentMeds>

备注:内容来源于stack exchange,提问作者ThatOneNerd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 11:19:30