虽然使用“自定义”小部件,但是在pos_hint参数中仅评估“ x”和“ y”参数。但是,如果我使用center_x,center_y或top之类的其他键,则不会评估键值。在下面给出的示例程序中,如果我将pos_hint用作center_x和center_y,则该行在布局的中间未对齐。 但是,如果我使用x和y,则参数对齐有效。
from kivy.app import App
from kivy.graphics import Line
from kivy.uix.scatter import Scatter
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.scatterlayout import ScatterLayout
class MyPaintWidget(Scatter):
def __init__(self, **kwargs) :
super(MyPaintWidget, self).__init__(**kwargs)
def create_figure(self, **kwargs):
self.canvas.add(Line( points=[0, 10, 30, 10]))
return self
class MyPaintApp(App):
def build(self):
parent = RelativeLayout()
#self.painter = MyPaintWidget(pos_hint={'center_x': 0.5, 'center_y':0.5}) Not working with center_x and center_y alignment
self.painter = MyPaintWidget(pos_hint={'x': 0.5, 'y':0.5}) #working with x and y parameter
parent.add_widget(self.painter.create_figure())
return parent
if __name__ == '__main__':
MyPaintApp().run()
即使尝试使用注释部分中提到的示例KV文件
from kivy.app import App
from kivy.graphics import Line
from kivy.uix.scatter import Scatter
from kivy.uix.relativelayout import RelativeLayout
from kivy.lang import Builder
KV = '''
<LineRectangle>:
canvas:
Color:
rgba: .1, .1, 1, .9
Line:
width: 2.
rectangle: (self.x, self.y, self.width, self.height)
Label:
center: root.center
text: 'Rectangle'
'''
class LineRectangle(Scatter):
pass
class MyPaintApp(App):
def build(self):
Builder.load_string(KV)
root = RelativeLayout()
#root.add_widget(LineRectangle(pos_hint={'center_x':0.5, 'center_y':0.5}, size_hint=(0.2, 0.2)))
root.add_widget(LineRectangle(pos_hint={'center_x':0.5, 'center_y':0.5}, size_hint=(None, None)))
return root
if __name__ == '__main__':
MyPaintApp().run()
答案 0 :(得分:1)
pos_hint属性允许您以百分比(类似于size_hint)的形式设置小部件在其父布局内的位置。