散点图大小和位置的问题

one*_*ace 2 python kivy

我遇到了一些与散布对象有关的问题。从下面的代码中。之后我调整一Scatterself.size_hint_xself.size_hint_y = 0.3, 0.3),对象(canvaslabel里面的)Scatter不调整为好。我也尝试应用size_hint=1CanvasLabel里面的Scatter,但是结果还是一样。

我遇到的另一个问题是检索/ 中/ 的X, Y位置(相对于父级)。它总是给我。CanvasLabelScatter(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()
Run Code Online (Sandbox Code Playgroud)

我错过了什么?感谢您的任何建议。

我是Kivy的新手。如果我的qns愚蠢,请原谅我。:P

tot*_*ico 5

您是否完全阅读了Scatter的文档。它说

...特定的行为使散布变得独特,您应该考虑一些优点/约束:

  1. 子代的位置相对于0,0。散布位置对子代的位置没有影响。
  2. 这也适用于大小。如果要调整散点图的大小,请使用比例尺,而不是尺寸。(阅读#1。)

那回答了你的第一个问题。它说的是使用规模,而不是大小。还有apply_transform方法,您可能会发现它对其他转换很有用。我从未尝试过这种方法,但是看不到转换值(可以看到“旋转和缩放”)

关于第二个问题。您正在self.xself.y位置(0,0)添加一个Rectangle 。因此,您的矩形位于该位置。如果拖动(使用手指或鼠标)您的窗口小部件。矩形的位置保持相对于Scatter。因此,除非您更改矩形的位置(使用代码),否则它将始终位于(0,0)中。转换总是相对于散点图。

这个问题可能是相关的,并解释了一些不使用Kivy语言添加顶点指令(即矩形)的问题。您应该考虑一下,因为您所做的事情似乎与之相关。

*编辑-根据我对您要实现的目标的理解,进行必要的更正*

1) 不要使用您正在使用的尺寸提示。

1.1)代替:

self.lbl = Label(text='Test', size_hint_x=1, size_hint_y=1)
Run Code Online (Sandbox Code Playgroud)

使用:

self.lbl = Label(text='Test', width=self.width, height=self.height)
Run Code Online (Sandbox Code Playgroud)

1.2)并且,代替:

self.size_hint_x, self.size_hint_y = 0.3, 0.3
Run Code Online (Sandbox Code Playgroud)

使用:

self.scale = 0.3
Run Code Online (Sandbox Code Playgroud)

2)位置相对于散点图。因此,您需要将坐标转换为父代。

2.1)代替:

print self.elf.lbl.pos  #<-- ISSUE: This not working.
Run Code Online (Sandbox Code Playgroud)

使用:

print self.elf.to_parent(*self.elf.lbl.pos)
Run Code Online (Sandbox Code Playgroud)

这是完整的代码:

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()
Run Code Online (Sandbox Code Playgroud)