0

我正在尝试编写一个简单的体重指数计算器。用户使用滑块选择高度,使用加号和减号按钮选择重量。当我尝试添加 MDCard 时程序崩溃。我正在使用 Pycharm 2021.1.1(社区版)、Python 3.8.10。我安装了以下解释器:Kivy、ivymd、glew、gstreamer、sdl2、pypiwin32、pygments、docutils。我怎么解决这个问题?即使我使用 Card 而不是 MDCard,也会发生崩溃。

Traceback (most recent call last):
   File "C:/Users/utente/PycharmProjects/pythonProject/main.py", line 26, in <module>
     MainApp().run()
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\app.py", line 949, in run
     self._run_prepare()
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\app.py", line 918, in _run_prepare
     self.load_kv(filename=self.kv_file)
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\app.py", line 691, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\lang\builder.py", line 306, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\lang\builder.py", line 408, in load_string
     self._apply_rule(
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\lang\builder.py", line 661, in _apply_rule
     child.apply_class_lang_rules(
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\uix\widget.py", line 463, in apply_class_lang_rules
     Builder.apply(
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\lang\builder.py", line 541, in apply
     self._apply_rule(
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\lang\builder.py", line 663, in _apply_rule
     self._apply_rule(
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\lang\builder.py", line 621, in _apply_rule
     cls = Factory_get(cname)
   File "C:\Users\utente\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\factory.py", line 146, in __getattr__
     raise FactoryException('Unknown class <%s>' % name)
 kivy.factory.FactoryException: Unknown class <MDCard>

Process finished with exit code 1

主文件

from kivymd.app import MDApp
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivy.properties import NumericProperty,StringProperty,ObjectProperty


class HomeScreen(Screen):
    weight = NumericProperty(40)

    def getvalue(self):
        sliderheightvalue = self.ids.height_value
        print(sliderheightvalue.value)

    def increase(self):
        self.weight = self.weight +1

    def decrease(self):
        self.weight = self.weight -1

class MainApp(MDApp):
    def __init__(self):
        Window.size = (400, 600)
        super().__init__()


MainApp().run()

主文件

ScreenManager:
    HomeScreen:
        id:home
        name:'home'

<HomeScreen>:
    canvas.before:
        Color:
            rgb:(252/255,186/255,3/255)
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        Label:
            pos_hint:{'top':.90,'left':1}
            size_hint: 1,.1
            font_size:30
            text:'Temperature'
            color: 1,1,1,1,

        Label:
            text: 'height'
            color: 1,0,0,1,
            font_size:25
            size_hint: 1,.1
            pos_hint: {'top': .74}

        Label:
            text: 'weight'
            color: 1, 0, 0, 1,
            font_size: 25
            size_hint: 1, .1
            pos_hint: {'top': .44}

        Slider:
            id: height_value
            min:00
            max:100
            step:0.5
            value:50
            size_hint: 1, .1
            pos_hint: {'top': .54}
            on_touch_up:
                root.getvalue()


        Label:
            text: str(height_value.value)
            color: 1, 0, 0, 1,
            font_size: 25
            size_hint: 1, .1
            pos_hint: {'top': .63}

        MDCard:
            size:"280dp","79dp"
            color: 1, 0, 0, 1,
            size_hint: none, none
            pos_hint: {'center_x': .5,'center_y': .5}
            MDIconButton:
                icon:'plus'
                user_font_size:"64sp"
                on_release:
                    root.increase()
            Label:
                text:str(root.weight) + 'kg'
                color:0,0,0,1
            MDIconButton:
                icon:'minus'
                user_font_size:"64sp"
                on_release:
                    root.decrease()
4

0 回答 0