[英]Basic Kivy Q, Positioning Canvas Child {kivy language}
Good Evening All 大家晚上好
Just wondering if someone can share the info, started playing around with kivy, all I am trying to so is have a basic canvas widget and position a rectangle on the top of the screen, using co-ord 0,0 draws it as the bottom. 我只是想知道是否有人可以共享信息,开始使用kivy,所以我要做的就是拥有一个基本的画布小部件,并在屏幕顶部放置一个矩形,使用坐标0,0将其绘制为底部。
it also raised the question that I can set near the top by using, say 0, 400, but how to you make it on the top all the time and resolution independent. 它还提出了一个问题,我可以通过使用0、400来将其设置在顶部附近,但是如何始终将其设置在顶部并与分辨率无关。 I am trying to make a small app as part of learning it and re-enforcing what I've learnt in python so far.
我正在尝试制作一个小型应用程序,作为学习它的一部分,并重新加强到目前为止在python中学到的东西。
Thanks for any insight 感谢您的见解
canvas:
Rectangle:
pos: self.pos
size: self.width , self.height / 10
Label:
font_size: 25
top: root.top
text:"Score"
Label:
font_size: 25
top: root.top
text:"4000 points"
In kivy canvas point (0, 0) is actually bottom left one. 在基维画布中,点(0,0)实际上是左下角的一个。 You can calculate top position yourself easily:
您可以自己轻松计算最高排名:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.properties import ListProperty
kv_string = '''
<MyWidget>:
r_size: [root.size[0]/2, root.size[1]/2]
canvas:
Color:
rgb: 0.1, 0.6, 0.3
Rectangle:
size: root.r_size
pos: 0, root.size[1]-root.r_size[1]
'''
Builder.load_string(kv_string)
class MyWidget(Widget):
r_size = ListProperty([0, 0])
class TestApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
TestApp().run()
You can also use FloatLayout
, set resolution independent subwidget sizes and positions using pos_hint
and size_hint
attributes, then draw something within borders of each such widget: 您还可以使用
FloatLayout
,使用pos_hint
和size_hint
属性设置分辨率无关的子小部件的大小和位置,然后在每个此类小部件的边框内绘制一些东西:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
kv_string = '''
<MyWidget>:
Widget:
pos_hint: {'center_y': 0.5, 'center_x': 0.5}
size_hint: 0.2, 0.2
canvas:
Color:
rgb: 0.1, 0.6, 0.3
Rectangle:
size: self.size
pos: self.pos
Widget:
pos_hint: {'center_y': 0.5, 'center_x': 0.2}
size_hint: 0.2, 0.2
canvas:
Color:
rgb: 0.1, 0.6, 0.3
Rectangle:
size: self.size
pos: self.pos
Widget:
pos_hint: {'center_y': 0.5, 'center_x': 0.8}
size_hint: 0.2, 0.2
canvas:
Color:
rgb: 0.1, 0.6, 0.3
Rectangle:
size: self.size
pos: self.pos
Widget:
pos_hint: {'center_y': 0.2, 'center_x': 0.5}
size_hint: 0.2, 0.2
canvas:
Color:
rgb: 0.1, 0.6, 0.3
Rectangle:
size: self.size
pos: self.pos
Widget:
pos_hint: {'center_y': 0.8, 'center_x': 0.5}
size_hint: 0.2, 0.2
canvas:
Color:
rgb: 0.1, 0.6, 0.3
Rectangle:
size: self.size
pos: self.pos
'''
Builder.load_string(kv_string)
class MyWidget(FloatLayout):
pass
class TestApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
TestApp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.