'How I can Inherit the RoundRectangleElevationBehavior with MDCard in .py file
Actually I am working on a Project with kivy gui and I am Stuck at one point in it. Actually i want to create a MDCard with the elevation of 15 within the python file (without the use of .kv file or kv string ) So when I am using the elevaton property inside the MDCard widget in the Python file it show the error such as:
If you see this error, this means that either youre using
CommonElevationBehavior directly or your 'shader' dont have a_draw_shadowinstruction, 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')
so I want the solution that How i can inherit the MDCard with RoundRectangleElevationBehavior in python file (not in .kv file or kv string) so i will able to use the MDCard with elevation property with no error.
The Whole Source Code is here:
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()
so if you have any solution of it so please let me know also. it will be very helpful for me. Thank You!!
Solution 1:[1]
First create a custom card which extends MDCard and 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
Solution 2:[2]
From the 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.
So, try changing:
class FirstWin(Screen,RoundedRectangularElevationBehavior):
to:
class FirstWin(RoundedRectangularElevationBehavior, Screen):
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | darkmatter08 |
| Solution 2 | John Anderson |
