<i id='1JPD0'><tr id='1JPD0'><dt id='1JPD0'><q id='1JPD0'><span id='1JPD0'><b id='1JPD0'><form id='1JPD0'><ins id='1JPD0'></ins><ul id='1JPD0'></ul><sub id='1JPD0'></sub></form><legend id='1JPD0'></legend><bdo id='1JPD0'><pre id='1JPD0'><center id='1JPD0'></center></pre></bdo></b><th id='1JPD0'></th></span></q></dt></tr></i><div id='1JPD0'><tfoot id='1JPD0'></tfoot><dl id='1JPD0'><fieldset id='1JPD0'></fieldset></dl></div>

  1. <legend id='1JPD0'><style id='1JPD0'><dir id='1JPD0'><q id='1JPD0'></q></dir></style></legend>

    <small id='1JPD0'></small><noframes id='1JPD0'>

      <tfoot id='1JPD0'></tfoot>

      • <bdo id='1JPD0'></bdo><ul id='1JPD0'></ul>

      在 Kivy 中将对象居中

      Centering an object in Kivy(在 Kivy 中将对象居中)
      <i id='qNFhX'><tr id='qNFhX'><dt id='qNFhX'><q id='qNFhX'><span id='qNFhX'><b id='qNFhX'><form id='qNFhX'><ins id='qNFhX'></ins><ul id='qNFhX'></ul><sub id='qNFhX'></sub></form><legend id='qNFhX'></legend><bdo id='qNFhX'><pre id='qNFhX'><center id='qNFhX'></center></pre></bdo></b><th id='qNFhX'></th></span></q></dt></tr></i><div id='qNFhX'><tfoot id='qNFhX'></tfoot><dl id='qNFhX'><fieldset id='qNFhX'></fieldset></dl></div>

            <tbody id='qNFhX'></tbody>

              <tfoot id='qNFhX'></tfoot>

            1. <small id='qNFhX'></small><noframes id='qNFhX'>

                <bdo id='qNFhX'></bdo><ul id='qNFhX'></ul>
              • <legend id='qNFhX'><style id='qNFhX'><dir id='qNFhX'><q id='qNFhX'></q></dir></style></legend>
                本文介绍了在 Kivy 中将对象居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试在布局内居中放置一个圆圈.我目前正在做一些填充计算,但我也在寻找更好的方法.我想其中一个预定义的布局可能是更好的选择.这是我的代码正在生成的内容...

                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 may be a better choice. Here's what my code is producing...

                对于方形布局:

                对于宽布局:

                这是正确的行为,很好,但有更好的方法吗?例如,我可以想象这对于非圆形形状会变得混乱.

                This is the right behavior, which is great, but is there a better way? I can imagine this getting messy with non-circle shapes, for example.

                这是我的代码:

                #!/usr/bin/kivy
                import kivy
                kivy.require('1.7.2')
                
                from random import random
                from kivy.app import App
                from kivy.uix.widget import Widget
                from kivy.uix.gridlayout import GridLayout
                from kivy.uix.anchorlayout import AnchorLayout
                from kivy.uix.relativelayout import RelativeLayout
                from kivy.graphics import Color, Ellipse, Rectangle
                
                class MinimalApp(App):
                    title = 'My App'
                    def build(self):
                        root = RootLayout()
                        return(root)
                
                class RootLayout(AnchorLayout):
                    pass
                
                class Circley(RelativeLayout):
                    pass
                
                if __name__ == '__main__':
                    MinimalApp().run()
                

                还有 KV:

                #:kivy 1.7.2
                #:import kivy kivy
                
                <RootLayout>:
                    anchor_x: 'center'                              # I think this /is/ centered
                    anchor_y: 'center' 
                    canvas.before:
                        Color:
                            rgba: 0.4, 0.4, 0.4, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    Circley:
                        anchor_x: 'center'                          # this is /not/ centered.
                        anchor_y: 'center' 
                        canvas.before:
                            Color:
                                rgba: 0.94, 0.94, 0.94, 1
                            Ellipse:
                                size: min(self.size), min(self.size)
                                pos: 0.5*self.size[0] - 0.5*min(self.size), 0.5*self.size[1] - 0.5*min(self.size)
                                Label:
                                    text: unicode(self.size)    # this is /not/ appearing
                                    color: 1,0,0,1
                

                推荐答案

                Snippet using FloatLayout, size_hint and pos_hint:

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

                日本国旗:

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

                这篇关于在 Kivy 中将对象居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                Kivy - Bind Label Text To Variable (Python Only)(Kivy - 将标签文本绑定到变量(仅限 Python))

                    <i id='OP5vm'><tr id='OP5vm'><dt id='OP5vm'><q id='OP5vm'><span id='OP5vm'><b id='OP5vm'><form id='OP5vm'><ins id='OP5vm'></ins><ul id='OP5vm'></ul><sub id='OP5vm'></sub></form><legend id='OP5vm'></legend><bdo id='OP5vm'><pre id='OP5vm'><center id='OP5vm'></center></pre></bdo></b><th id='OP5vm'></th></span></q></dt></tr></i><div id='OP5vm'><tfoot id='OP5vm'></tfoot><dl id='OP5vm'><fieldset id='OP5vm'></fieldset></dl></div>
                    <legend id='OP5vm'><style id='OP5vm'><dir id='OP5vm'><q id='OP5vm'></q></dir></style></legend>
                    <tfoot id='OP5vm'></tfoot>

                          <bdo id='OP5vm'></bdo><ul id='OP5vm'></ul>

                            <tbody id='OP5vm'></tbody>

                          <small id='OP5vm'></small><noframes id='OP5vm'>