Kivy:Can没有把'BorderImage'放在指定位置
Kivy:Can't put 'BorderImage' at the specified position
(1) 通过使用以下 kv 文件版本能够将 BorderImage
小部件放置在指定位置。 .
<Screen>:
ProgressBar:
max: 100
pos_hint: {'top':0.86, 'x':0.01}
size_hint_x: 0.49
size_hint_y: 0.1
canvas:
BorderImage:
border: (10, 10, 10, 10)
pos: self.x, self.center_y
size: self.width, 8
source: '0.png'
(2) 但是,下面的 纯 Python 应该实现与 (1) 相同功能的代码没有无法正常工作。BorderImage
小部件位于屏幕的底部。
pos_hint={'top':0.86,'x':0.01}
不起作用。
我认为如何指定 pos=(bar.x, bar.center_y)
不好,因为 bar.center_y
值与 (1).
的代码不同
class BarWidget(FloatLayout):
def __init__(self, **kwargs):
super(BarWidget, self).__init__(**kwargs)
self.build()
def build(self):
bar = ProgressBar(pos_hint={'top':0.86,'x':0.01}, max=100, size_hint_x=0.49, size_hint_y=0.1)
with bar.canvas:
BorderImage(border=(10, 10, 10, 10), pos=(bar.x, bar.center_y), size=(self.width/2, 8), source='0.png')
self.add_widget(bar)
我该如何修改bar.center_y
?
(1):screen shot
(2):screen shot
在 kv 语言中,当您设置像 size: self.size
这样的属性时,只要 'self' 小部件改变形状的大小,该属性就会自动更新。当事情在 screen/layout 中加载时,它们从时髦的位置和大小开始,然后移动到正确的位置。如果您在 kv 中工作,由于更改 sizes/positions 后事情会自动更新,因此它会按您预期的那样工作。
在 python 中,您必须显式绑定一些函数来更新 canvas 如果小部件的大小或位置从中继承其大小和位置发生变化。您可以使用 bind
函数(在 most/all kivy 小部件中可用)来做到这一点。使用 bind
,你可以说 bind(<attribute>=<function>)
,这意味着只要小部件的 <attribute>
,也就是大小或位置发生变化,它就会调用 <function>
我没有使用您的代码对此进行准确测试,因为并未发布所有代码,但这就是我为我的项目所做的。让我知道它是如何工作的。如果它不起作用,请将您的答案编辑为我可以 copy/paste 使用的代码片段,我会更新我的答案。
class BarWidget(FloatLayout):
def __init__(self, **kwargs):
super(BarWidget, self).__init__(**kwargs)
self.build()
def build(self):
# Make it so you can reference the bar and border image later
self.bar = ProgressBar(pos_hint={'top':0.86,'x':0.01}, max=100, size_hint_x=0.49, size_hint_y=0.1)
with self.bar.canvas:
# Make it so you can reference the border image
self.border_image = BorderImage(border=(10, 10, 10, 10), pos=(bar.x, bar.center_y), size=(self.width, 8), source='0.png')
self.add_widget(self.bar)
# Make it so whenever the widget's pos or size changes, we update the border image
self.bar.bind(pos=self.update_border_image, size=self.update_border_image)
# make a function to update border image
def update_border_image(self, *args):
self.border_image.size = (self.width, 8) # Should this 8 be 0.8 by the way?
self.border_image.pos = (self.bar.x, self.bar.center_y)
(1) 通过使用以下 kv 文件版本能够将 BorderImage
小部件放置在指定位置。 .
<Screen>:
ProgressBar:
max: 100
pos_hint: {'top':0.86, 'x':0.01}
size_hint_x: 0.49
size_hint_y: 0.1
canvas:
BorderImage:
border: (10, 10, 10, 10)
pos: self.x, self.center_y
size: self.width, 8
source: '0.png'
(2) 但是,下面的 纯 Python 应该实现与 (1) 相同功能的代码没有无法正常工作。BorderImage
小部件位于屏幕的底部。
pos_hint={'top':0.86,'x':0.01}
不起作用。
我认为如何指定 pos=(bar.x, bar.center_y)
不好,因为 bar.center_y
值与 (1).
class BarWidget(FloatLayout):
def __init__(self, **kwargs):
super(BarWidget, self).__init__(**kwargs)
self.build()
def build(self):
bar = ProgressBar(pos_hint={'top':0.86,'x':0.01}, max=100, size_hint_x=0.49, size_hint_y=0.1)
with bar.canvas:
BorderImage(border=(10, 10, 10, 10), pos=(bar.x, bar.center_y), size=(self.width/2, 8), source='0.png')
self.add_widget(bar)
我该如何修改bar.center_y
?
(1):screen shot
(2):screen shot
在 kv 语言中,当您设置像 size: self.size
这样的属性时,只要 'self' 小部件改变形状的大小,该属性就会自动更新。当事情在 screen/layout 中加载时,它们从时髦的位置和大小开始,然后移动到正确的位置。如果您在 kv 中工作,由于更改 sizes/positions 后事情会自动更新,因此它会按您预期的那样工作。
在 python 中,您必须显式绑定一些函数来更新 canvas 如果小部件的大小或位置从中继承其大小和位置发生变化。您可以使用 bind
函数(在 most/all kivy 小部件中可用)来做到这一点。使用 bind
,你可以说 bind(<attribute>=<function>)
,这意味着只要小部件的 <attribute>
,也就是大小或位置发生变化,它就会调用 <function>
我没有使用您的代码对此进行准确测试,因为并未发布所有代码,但这就是我为我的项目所做的。让我知道它是如何工作的。如果它不起作用,请将您的答案编辑为我可以 copy/paste 使用的代码片段,我会更新我的答案。
class BarWidget(FloatLayout):
def __init__(self, **kwargs):
super(BarWidget, self).__init__(**kwargs)
self.build()
def build(self):
# Make it so you can reference the bar and border image later
self.bar = ProgressBar(pos_hint={'top':0.86,'x':0.01}, max=100, size_hint_x=0.49, size_hint_y=0.1)
with self.bar.canvas:
# Make it so you can reference the border image
self.border_image = BorderImage(border=(10, 10, 10, 10), pos=(bar.x, bar.center_y), size=(self.width, 8), source='0.png')
self.add_widget(self.bar)
# Make it so whenever the widget's pos or size changes, we update the border image
self.bar.bind(pos=self.update_border_image, size=self.update_border_image)
# make a function to update border image
def update_border_image(self, *args):
self.border_image.size = (self.width, 8) # Should this 8 be 0.8 by the way?
self.border_image.pos = (self.bar.x, self.bar.center_y)