[英]Why isn't this rectangle centered in Kivy?
I'm trying to draw a rectangle in the center of my widget: 我正在尝试在小部件的中心绘制一个矩形:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
class MyWidget(Widget):
def __init__(self):
super(MyWidget, self).__init__()
with self.canvas:
Rectangle(pos=(self.center_x, self.center_y)
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
This is what I'm getting: 这就是我得到的:
Doing the exact same thing using a .kv
file works: 使用
.kv
文件执行完全相同的操作是.kv
:
<MyWidget>:
canvas:
Rectangle:
pos: self.center_x, self.center_y
Please explain how to achieve what I'm trying to do, and why it does work using a .kv
file, as opposed to Python code. 请说明如何实现我要执行的操作,以及为什么使用
.kv
文件而不是Python代码可以正常工作。 Thank you 谢谢
If you add the Widget from a kv file the the widget will be automatically be attached to the App's root attribute and used as the base of the application widget tree. 如果从kv文件添加窗口小部件,则该窗口小部件将自动附加到应用程序的root属性,并用作应用程序窗口小部件树的基础。 So in your situation from the kv file the size of the widget is automatically bind to the application windows size and becomes root widget.
因此,根据您的情况,从kv文件中,窗口小部件的大小会自动绑定到应用程序窗口的大小,并成为根窗口小部件。 Taking that into consideration the self.center_x and self.center_y works.
考虑到这一点,self.center_x和self.center_y起作用。 You can read this at https://kivy.org/docs/guide/lang.html under the line `MyApp -> my.kv.
您可以在https://kivy.org/docs/guide/lang.html MyApp-> my.kv行下阅读此内容。
When not using a kv file this will not happen and the default size of the widget will be (100,100). 如果不使用kv文件,则不会发生这种情况,小部件的默认大小为(100,100)。 To properly place your rectangle use a layout so you can refer use the size_hint to properly place or re size any child widget.
要正确放置矩形,请使用布局,以便您可以参考使用size_hint正确放置或调整任何子窗口小部件的大小。 As stated in the documentation
如文档中所述
FloatLayout honors the pos_hint and the size_hint properties of its children.
FloatLayout继承其子级的pos_hint和size_hint属性。
https://kivy.org/docs/api-kivy.uix.floatlayout.html#kivy.uix.floatlayout.FloatLayout https://kivy.org/docs/api-kivy.uix.floatlayout.html#kivy.uix.floatlayout.FloatLayout
So create a Float layout in example, add a widget with pos_hint equals to center_x and center_y that will refer to the layout being a parent and then draw a rectangle to the widget. 因此,在示例中创建一个Float布局,添加一个pos_hint等于center_x和center_y的小部件,它将以该布局为父级,然后在该小部件上绘制一个矩形。
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Rectangle
from kivy.core.window import Window
class myLayout(FloatLayout):
def __init__(self):
super(myLayout, self).__init__()
self.size = Window.size
self.myWidget = Widget(size=(100,100))
self.add_widget(self.myWidget)
with self.myWidget.canvas:
Rectangle(pos=(self.center_x, self.center_y))
class MyApp(App):
def build(self):
return myLayout()
if __name__ == '__main__':
MyApp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.