How to open MDCard when I press a button in kivy(python)?

1,047 views
Skip to first unread message

mehdi alizade

unread,
Jul 10, 2021, 1:04:24 PM7/10/21
to kivy-...@googlegroups.com
Hi, I have a button on a screen, I want to open MDCard when I press this button. I wrote this code the same as Popup but the MDCard object has no attribute 'open'.

#: 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'

Elliot Garbus

unread,
Jul 10, 2021, 1:46:07 PM7/10/21
to kivy-...@googlegroups.com
Instance the widget, then use the add_widget method to add the newly created widget to the layout. 

Sent from my iPhone

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.

Elliot Garbus

unread,
Jul 10, 2021, 2:32:39 PM7/10/21
to kivy-...@googlegroups.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()

mehdi alizade

unread,
Jul 10, 2021, 4:25:39 PM7/10/21
to kivy-...@googlegroups.com
It was a useful example. thank you

mehdi alizade

unread,
Jul 11, 2021, 5:11:06 AM7/11/21
to kivy-...@googlegroups.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?

Elliot Garbus

unread,
Jul 11, 2021, 12:30:47 PM7/11/21
to kivy-...@googlegroups.com

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()

mehdi alizade

unread,
Jul 11, 2021, 1:36:24 PM7/11/21
to kivy-...@googlegroups.com
This is exactly what I want. Thank you so much.

Reply all
Reply to author
Forward
0 new messages