Python Forum

Full Version: kivy: how to create a widget that represents a circled letter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
[kivy]
how to create a widget that represents a circled letter?
I want to be able to move this widget to my root widget as I please. However, I can't associate the circle with the letter. The application I'm working on is a brick breaker structured like the pong in the tutorial.
Just to show some code, I attach a non functional one:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse
from kivy.uix.label import Label
from kivy.metrics import dp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty

Builder.load_string('''

<MyCircleWithLetter>:
    letter: "A"
    canvas:
        Color:
            rgba: 1, 1, 0, 1
        Ellipse:
            pos: self.center_x - dp(100), self.center_y - dp(100)
            size: dp(200), dp(200)
    Label:
        text: root.letter
        font_size: dp(100)
        pos: self.center_x - dp(50), self.center_y - dp(50)

<Widgetroot>:
    position: position
    MyCircleWithLetter:
        id: position
        pos: root.pos
        #self.center_x: Window.width//2
        #self.center_y: Window.height//2
''')

class MyCircleWithLetter(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass
    
class WidgetRoot(Widget):
    position: ObjectProperty()
    def __init__(self, **kwargs):
        self.pos = (Window.width, Window.height)
        super().__init__(**kwargs)
        
        
class MyPaintApp(App):
    def build(self):
        fl = WidgetRoot()
            
        return fl
if __name__ == "__main__":
    MyPaintApp().run()
Merci
solved: Wink
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse
from kivy.uix.label import Label
from kivy.metrics import dp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty

Builder.load_string('''

<MyCircleWithLetter>:
    letter: "A"
    
    canvas:
        Color:
            rgba: 1, 1, 0, 1
        Ellipse:
            pos: self.center_x - dp(100), self.center_y - dp(100)
            size: dp(200), dp(200)
    Label:
        text: root.letter
        font_size: dp(100)
        pos: root.center_x - dp(50), root.center_y - dp(50)

<Widgetroot>:
    position: position
    MyCircleWithLetter:
        id: position
        pos: root.pos
       
''')

class MyCircleWithLetter(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass
    
class WidgetRoot(Widget):
    position: ObjectProperty()
    def __init__(self, **kwargs):
        self.pos = (Window.width//2, Window.height//2)
        super().__init__(**kwargs)
        
        
class MyPaintApp(App):
    def build(self):
        fl = WidgetRoot()
            
        return fl
if __name__ == "__main__":
    MyPaintApp().run()