我如何在 .py 文件中使用 MDCard 继承 RoundRectangleElevationBehavior

How I can Inherit the RoundRectangleElevationBehavior with MDCard in .py file

实际上,我正在使用 kivy gui 进行一个项目,但我曾一度陷入其中。 实际上我想在 python 文件中创建一个高度为 15 的 MDCard(不使用 .kv 文件或 kv 字符串)所以当我在 MDCard 小部件中使用 elevaton 属性 时Python 文件显示如下错误:

If you see this error, this means that either youre using CommonElevationBehavior directly or your 'shader' dont have a _draw_shadow instruction, remember to overwrite this functionto draw over the image context. Тhe figure you would like. Or your class MDCard is not inherited from any of the classes ('CommonElevationBehavior', 'RectangularElevationBehavior', 'CircularElevationBehavior', 'RoundedRectangularElevationBehavior', 'ObservableShadow', 'FakeRectangularElevationBehavior', 'FakeCircularElevationBehavior')

所以我想要 How i can inherit the MDCard with RoundRectangleElevationBehavior in python file (not in .kv file or kv string) 的解决方案,这样我就可以毫无错误地使用提升 属性 的 MDCard。

完整的源代码在这里:

from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.behaviors import RoundedRectangularElevationBehavior
from kivy.uix.screenmanager import ScreenManager,Screen

class FirstWin(Screen,RoundedRectangularElevationBehavior):
    def __init__(self,**kwargs):
        super(FirstWin,self).__init__(**kwargs)
        mycard=MDCard(
            elevation=15,
            size_hint =(0.4,0.7),
            pos_hint={'center_x':0.5,'center_y':0.5}

        )
        self.add_widget(mycard)


class SecondWin(Screen):
    pass
class MymdCard(MDApp):
    def build(self):
        sm = ScreenManager()
        self.theme_cls.theme_style = "Dark"

        sm.add_widget(FirstWin(name='welcomeScreen'))
        sm.add_widget(SecondWin(name='functionScreen'))
        return sm




if __name__ == '__main__':
    MymdCard().run()

因此,如果您有任何解决方案,请也告诉我。这对我很有帮助。 谢谢!!

来自behaviors documentation

The behavior class must always be before the widget class. If you don’t specify the inheritance in this order, the behavior will not work because the behavior methods are overwritten by the class method listed first.

所以,尝试更改:

class FirstWin(Screen,RoundedRectangularElevationBehavior):

至:

class FirstWin(RoundedRectangularElevationBehavior, Screen):

首先创建一个扩展 MDCard 和 RoundedRectangularElevationBehaviour 的自定义卡片

from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.behaviors import RoundedRectangularElevationBehavior
from kivy.uix.screenmanager import ScreenManager,Screen

class FirstWin(Screen,RoundedRectangularElevationBehavior):
    def __init__(self,**kwargs):
        super(FirstWin,self).__init__(**kwargs)
        mycard=MyCustomCard(
            elevation=15,
            size_hint =(0.4,0.7),
            pos_hint={'center_x':0.5,'center_y':0.5}

        )
        self.add_widget(mycard)
class MyCustomCard(RoundedRectangularElevationBehavior, MDCard):
    pass