#: import Factory kivy.factory.Factory
<Second>:
FloatLayout:
Button:
text:"Open MDCard"
size_hint: .3, .07
pos_hint: {"x":.07, "y":0.25}
on_release: Factory.MyMDCard().open()
<MyMDCard@MDCard>
orientation: "vertical"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'Blue.png'
FloatLayout:
Button:
id: 'change'On Jul 10, 2021, at 10:04 AM, mehdi alizade <aliza...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAFSqpV%3DSziXsNP5pqo9JO6601o1BaumPhwEQeneodpHn1BZqog%40mail.gmail.com.
Here is an example:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.card import MDCard
kv = """
<BlankMDCard>:
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "280dp", "180dp"
pos_hint: {"center_x": .5, "center_y": .5}
MDLabel:
text: "The Title Goes Here"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "1dp"
MDLabel:
text: "The Body"
MDBoxLayout:
orientation: 'vertical'
MDLabel:
text: 'Add an MDCard Widget'
halign: 'center'
size_hint_y: None
height: 24
FloatLayout:
id: card
MDRaisedButton:
text: 'Add MDCard'
on_release: app.add_card()
pos_hint: {'center_x': 0.5}
"""
class BlankMDCard(MDCard):
pass
class AddCardApp(MDApp):
def build(self):
return Builder.load_string(kv)
def add_card(self):
if not self.root.ids.card.children:
self.root.ids.card.add_widget(BlankMDCard()) # add the card to the enclosing layout
AddCardApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/327E4273-6C91-46D1-BC24-68895F0C462A%40cox.net.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/60e9e7c3.1c69fb81.d6f7.be1aSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
I changed the code as follows.
#: import Factory kivy.factory.Factory
<Second>:
FloatLayout:
Button:
text:"Open MDCard"
size_hint: .3, .07
pos_hint: {"x":.07, "y":0.25}
on_release: root.add_widget(Factory.MyMDCard())
<MyMDCard@MDCard>
orientation: "vertical"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'Blue.png'
FloatLayout:
Button:
text:"Close MDCard"
on_release: ???
Now I put a button in the widget that I want to close by clicking on it. Can you help?
The problem you may have with this code is that a new MyMDCard object is created everytime the button is pressed. I suspect you do not want to create a new MDCard if the Card has already been created.
I have extended the previous example to add a remove button:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.card import MDCard
kv =
"""
<BlankMDCard>:
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "280dp", "180dp"
pos_hint: {"center_x": .5, "center_y": .5}
MDLabel:
text: "The Title Goes Here"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "1dp"
MDLabel:
text: "The Body"
MDBoxLayout:
orientation: 'vertical'
spacing: 2
MDLabel:
text: 'Add an MDCard Widget'
halign: 'center'
size_hint_y: None
height: 24
FloatLayout:
id: card
MDRaisedButton:
text: 'Add MDCard'
on_release: app.add_card()
pos_hint: {'center_x': 0.5}
MDRaisedButton:
text: 'Remove MDCard'
on_release: app.remove_card()
pos_hint: {'center_x': 0.5}
"""
class BlankMDCard(MDCard):
pass
class AddCardApp
(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.card = None # used to hold the card instance
def build(self):
return Builder.load_string(kv)
def add_card(self):
if not self.card:
self.card = BlankMDCard()
self.root.ids.card.add_widget(self.card) # add the card to the enclosing layout
def remove_card(self):
if self.card:
self.root.ids.card.remove_widget(self.card)
self.card = None
AddCardApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAFSqpVnWPPT7c11YjWvTQ3k3nYz5rntTn7Pd1L_LeBHTPs98GA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/60eb1cb0.1c69fb81.afd5d.9be4SMTPIN_ADDED_MISSING%40gmr-mx.google.com.