Search code examples
pythonpython-3.xkivy

Kivy ModalView border


I want to add a border-radius to a ModalView in Kivy, but I don't know how to do this without a Image.

Here is my code:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.modalview import ModalView

class MyApp(App):
    def build(self):
        p = ModalView()
        p.add_widget(Label(text="Hello!"))
        p.size_hint = (.2, .2)
        p.background_color = (0, 1, 1, 1)
        #add a border radius here
        p.open()
        return
        
MyApp().run()

If someone knows how, I would be very grateful.


Solution

  • You can define your own custom ModalView class as:

    class MyModalView(ModalView):
        pass
    

    Then define how the new class looks using Builder:

            Builder.load_string('''
    <-MyModalView>:
        canvas.before:
            Color:
                rgba: root.background_color
            RoundedRectangle:
                pos: self.pos
                size: self.size
                radius: [20,]
            ''')
    

    Starting the kv rule with a - indicates that the normal kv rule for displaying a ModalView should not be inherited by the new class.

    The just use MyModalView rather than ModalView in your code.