Search code examples
pythonpython-3.xkivykivymd

How can i add white border after background color in Kivy via Pure Python?


Below code works and i can clearly see the blue Color(0, 0.525, 0.953, 1) background of takim_isimleri_box BoxLayout Base code:

from kivy.graphics import Color, RoundedRectangle, Line
from kivy.uix.boxlayout import BoxLayout

takim_isimleri_box = BoxLayout()
with takim_isimleri_box.canvas.before:
    Color(0, 0.525, 0.953, 1)  # Background color 
    takim_isimleri_box.rect = RoundedRectangle(size=takim_isimleri_box.size, pos=takim_isimleri_box.pos, radius=[10, 10, 10, 10])

def update_rect2(instance, value):
    instance.rect.size = instance.size
    instance.rect.pos = instance.pos
takim_isimleri_box.bind(size=update_rect2, pos=update_rect2)  

I want to add a white white border after background color in Kivy via Pure Python? Howe can i do this?

What i tried, i asked it to Chatgpt it recommended me below code but did not worked. ChatgPt:

from kivy.graphics import Color, RoundedRectangle, Line
from kivy.uix.boxlayout import BoxLayout

takim_isimleri_box = BoxLayout()

with takim_isimleri_box.canvas.before:
    Color(0, 0.525, 0.953, 1)  # Background color 
    takim_isimleri_box.rect = RoundedRectangle(size=takim_isimleri_box.size, pos=takim_isimleri_box.pos, radius=[10, 10, 10, 10])

with takim_isimleri_box.canvas.after:
    Color(1, 1, 1, 1)  # White color for the border
    Line(rectangle=(takim_isimleri_box.x, takim_isimleri_box.y, takim_isimleri_box.width, takim_isimleri_box.height), width=1.5)

def update_rect2(instance, value):
    instance.rect.size = instance.size
    instance.rect.pos = instance.pos

takim_isimleri_box.bind(size=update_rect2, pos=update_rect2)

Above code did not work. There is no white border. I only see blue background without white border.

Thanks very much


Solution

  • Is this what you are looking for?

    import kivy
    from kivy.graphics import Color, Line, RoundedRectangle
    from kivy.app import App
    from kivy.uix.button import Button
    from kivy.uix.boxlayout import BoxLayout
    
    
    class MyApp(App):
    
        def build(self):
            box = BoxLayout()
            with box.canvas:
                Color(0, 0, 1)
                RoundedRectangle(size=box.size, pos=box.pos)
                Color(1, 1, 1)
                Line(rectangle=[box.x, box.y, box.width, box.height], width=10)
            return box
    
    
    if __name__ == '__main__':
        MyApp().run()