我遇到了一些与分散对象相关的问题.从我下面的代码.在我调整 Scatter
(self.size_hint_x
, self.size_hint_y = 0.3, 0.3
) 的大小后,对象 (canvas
Scatter 中的 label
) 也不会调整大小.我确实尝试将 size_hint=1
应用于 Scatter
内的 Canvas
和 Label
,但结果仍然是一样.
I encountered some issue related to scatter object.
From my codes below. After I resize a Scatter
(self.size_hint_x
, self.size_hint_y = 0.3, 0.3
), the objects (canvas
, label
) inside the Scatter
is not resized as well. I did tried to apply size_hint=1
to the Canvas
and Label
inside the Scatter
, however the result still the same.
我遇到的另一个问题是检索 Canvas
/Label
中的 X, Y
位置(相对于父级)分散
.它总是给我(0,0)
.
Another issue I encountered is the retrieving of the X, Y
position (relative to the parent) for the Canvas
/Label
in a Scatter
. It always give me (0,0)
.
我的代码
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.graphics import Color, Rectangle, Canvas
class Avatar(Scatter):
def __init__(self, **kwargs):
super(Avatar, self).__init__(size_hint=(None,None), **kwargs)
with self.canvas:
Color(0, 0, 0)
Rectangle(pos=(self.x, self.y), size=(self.width, self.height))
self.lbl = Label(text='Test', size_hint_x=1, size_hint_y=1)
self.add_widget(self.lbl)
# Scatter size is 30% of the GameBackground
# ISSUE: After resize my Scatter, the objects inside is not resized as well.
self.size_hint_x, self.size_hint_y = 0.3, 0.3
class GameBackground(FloatLayout):
def __init__(self, **kwargs):
super(GameBackground, self).__init__(**kwargs)
with self.canvas:
Color(1, 0, 1)
Rectangle(pos = (0, 0), size = (Window.width,Window.height))
self.elf = Avatar()
self.add_widget(self.elf)
self.elf.x = 200
self.elf.y = 300
# Get the X, Y position of the Scatter and the label inside the Scatter relative to the parent.
print self.elf.pos #<-- This works.
print self.elf.lbl.pos #<-- ISSUE: This not working.
class GameApp(App):
def build(self):
return GameBackground()
if __name__ == '__main__':
GameApp().run()
我错过了什么吗?感谢您的任何建议.
Did I miss something? Thanks for any advise.
我是 Kivy 的新手.如果我的qns很笨,请原谅我.:P
您是否阅读了 分散.它说
...特定的行为使分散变得独一无二,您应该考虑一些优点/限制:
...specific behavior makes the scatter unique, and there are some advantages / constraints that you should consider:
这回答了你的第一个问题.它说使用比例,而不是大小.还有方法 apply_transform 您可能会发现它对其他转换很有用.虽然我从未尝试过这种方法,但我看不到平移值(我可以看到旋转和缩放)
That answer your first question. It says use scale, not size. There is also the the method apply_transform that you may find useful for other transformations. I have never tried this method though but I cannot see the translate value (I can see Rotate and Scale)
关于你的第二个问题.您在 self.x
和 self.y
位置添加一个矩形,即 (0,0).因此,您的 Rectangle 处于该位置.如果您拖动(使用手指或鼠标)您的小部件.Rectangle 的位置仍然相对于 Scatter
.因此,除非您更改 Rectangle 的位置(使用代码),否则它将始终位于 (0,0) 中.变换总是相对于 Scatter.
Regarding to your second question. You are adding a Rectangle in the self.x
and self.y
position that is (0,0). So, your Rectangle is in that position. If you drag (using your fingers or mouse) your widget. The position of the Rectangle remains relative to the Scatter
. So, unless you change the position of the Rectangle (with code) it will be always be in the (0,0). The transformations are always relative to the Scatter.
这个question 可能是相关的,并解释了一些不使用 Kivy 语言添加顶点指令(即矩形)的问题.你应该考虑一下,因为你所做的似乎是相关的.
This question might be related and explains a few issues with not using the Kivy Language to add Vertex Instructions (i.e. Rectangles). You should consider it because what you are doing seems to be related.
* 编辑 - 根据我对您要达到的目标的理解进行必要的更正 *
1) 不要像您正在使用的那样使用尺寸提示.
1.1) 代替:
self.lbl = Label(text='Test', size_hint_x=1, size_hint_y=1)
使用:
self.lbl = Label(text='Test', width=self.width, height=self.height)
1.2) 并且,而不是:
1.2) And, instead of:
self.size_hint_x, self.size_hint_y = 0.3, 0.3
使用:
self.scale = 0.3
2) 位置是相对于散点的.因此,您需要将坐标转换给父级.
2) The position is relative to the scatter. Therefore you need to transale the coordinates to the parent.
2.1) 代替:
print self.elf.lbl.pos #<-- ISSUE: This not working.
使用:
print self.elf.to_parent(*self.elf.lbl.pos)
完整代码如下:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.graphics import Color, Rectangle, Canvas
class Avatar(Scatter):
def __init__(self, **kwargs):
super(Avatar, self).__init__(size_hint=(None,None), **kwargs)
with self.canvas:
Color(0, 0, 0)
Rectangle(pos=(self.x, self.y), size=(self.width, self.height))
#self.lbl = Label(text='Test', size_hint_x=1, size_hint_y=1)
self.lbl = Label(text='Test', width=self.width, height=self.height)
self.add_widget(self.lbl)
# Scatter size is 30% of the GameBackground
# ISSUE: After resize my Scatter, the objects inside is not resized as well.
# self.size_hint_x, self.size_hint_y = 0.3, 0.3
self.scale = 0.3
class GameBackground(FloatLayout):
def __init__(self, **kwargs):
super(GameBackground, self).__init__(**kwargs)
with self.canvas:
Color(0, 0, 1)
Rectangle(pos = (0, 0), size = (Window.width,Window.height))
self.elf = Avatar()
self.add_widget(self.elf)
self.elf.x = 200
self.elf.y = 300
# Get the X, Y position of the Scatter and the label inside the Scatter relative to the parent.
print self.elf.pos #<-- This works.
print self.elf.lbl.pos #<-- ISSUE: This not working.
print self.elf.to_parent(*self.elf.lbl.pos)
class GameApp(App):
def build(self):
return GameBackground()
if __name__ == '__main__':
GameApp().run()
这篇关于Scatter 大小和位置的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!