I'm working on a project and I'm designing the setting screen. I want to use it so many time so I made a class with variable such as content_cls My Question Is How To Set This Variable With Classes That Are Made In Kivy File
main.py
from kivymd.app import MDApp
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.card import MDCard
from kivy.proeprties import ObjectProperty , StringProperty
from kivy.lang import Builder
Builder.load_file("mainfiles\\kivymd2.kv")
class SettingCard(MDCard):
right_text = StringProeprty("")
content_cls = ObjectProperty()
class WidgetsManager(MDScreenManager):
pass
class MainApp(MDApp):
def build(self):
return WidgetsManager()
def on_start(self):
SettingCard.add_widget(Setting_card.content_cls)
if __name__ == "__main__":
MainApp().run()
kivymd2.kv
<Nav_drawer_header@MDLabel>:
size_hint : None , None
adaptive_size : True
font_style : "H5"
theme_text_color : "Primary"
font_name : "mainfiles\\B-NAZANIN.ttf"
<Setting_card>:
_no_ripple_behavior : True
size_hint : None , None
adaptive_height : True
radius : dp(20) , dp(20) , dp(20) , dp(20)
md_bg_color : "lightgrey"
width : self.parent.width
MDLabel:
bold : True
font_size : 35
text : app.farsi_font(root.right_text)
size_hint : None, None
adaptive_size : True
pos_hint : {"center_y" : .5 , "right" : .9}
font_name : "mainfiles\\B-NAZANIN.ttf"
<WidgetManager>:
MDScreen:
name : "setting screen"
FitImage:
size_hint : 1 , .9
source : "images\\download (8).jpg"
pos_hint : {"top" : .9}
MDScrollView:
do_scroll_x : False
do_scroll_y : True
id : setting_scroll
width : root.width
height : root.height/10*9
MDGridLayout:
size_hint : None , None
adaptive_height : True
width : self.parent.width
cols : 1
Setting_card:
id : fast_setting
right_text : "شروع سریع"
content_cls : Nav_drawer_header
I Try to have a clean code and because of that I made that class And I Except to call classes in kivy file and put it in content_*cls but when I did this it said that Nav_*drawer_header is not defined
Pay Attention That I made an example for Nav_drawer_header and I won't never use it as content_cls
You can use the kivy.factory
for this. At the top of your kv
file, add the line:
#:import Factory kivy.factory.Factory
Then, change the content_cls
attribute in your kv
file to:
content_cls : Factory.get('Nav_drawer_header')
This sets the content_cls
attribute to the Nav_drawer_header
class object.
You can then create an instance of Nav_drawer_header
by using setting_card.content_cls()
, where setting_card
is that instance of Setting_card
. Note that you cannot use Setting_card.content_cls()
because Setting_card
is not an instance.
So your on_start()
method can be:
def on_start(self):
setting_card = self.root.ids.fast_setting
setting_card.add_widget(setting_card.content_cls())
There are other errors in your code. Check your spelling of Property
.