Centering an object in Kivy

后端 未结 1 1958
醉酒成梦
醉酒成梦 2021-02-20 00:44

I am trying to center a circle inside a layout. I\'m currently doing some padding calculations, but I\'m also looking for a better way. I imagine one of the predefined layouts

相关标签:
1条回答
  • 2021-02-20 01:35

    Snippet using FloatLayout, size_hint and pos_hint:

    from kivy.app import App
    from kivy.lang import Builder
    
    kv = '''
    FloatLayout:
        Widget:
            size: min(root.size), min(root.size)
            size_hint: None, None
            pos_hint: {'center_x': .5, 'center_y': .5}
            canvas:
                Color:
                    rgb: 1, 0, 0
                Ellipse:    
                    size: self.size
                    pos: self.pos
    '''
    Builder.load_string(kv)
    
    class MyApp(App):
        def build(self):
            return Builder.load_string(kv)
    
    MyApp().run()
    

    Flag of Japan:

    from kivy.app import App
    from kivy.lang import Builder
    
    kv = '''
    FloatLayout:
        canvas:
            Color:
                rgb: 1, 1, 1
            Rectangle:    
                size: self.size
                pos: self.pos   
        Widget:
            size: min(root.size)/2, min(root.size)/2
            size_hint: None, None
            pos_hint: {'center_x': .5, 'center_y': .5}
            canvas:
                Color:
                    rgb: 1, 0, 0
                Ellipse:    
                    size: self.size
                    pos: self.pos
    '''
    Builder.load_string(kv)
    
    class MyApp(App):
        def build(self):
            return Builder.load_string(kv)
    
    MyApp().run()
    
    0 讨论(0)
提交回复
热议问题