散点图大小和位置的问题 [英] Issues with Scatter size and position

查看:257
本文介绍了散点图大小和位置的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些与散布对象有关的问题. 从下面的代码中.调整Scatter(self.size_hint_xself.size_hint_y = 0.3, 0.3)的大小之后,Scatter内部的对象(canvaslabel)也没有调整大小.我确实尝试将size_hint=1应用于Scatter内的CanvasLabel,但是结果仍然相同.

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.

我遇到的另一个问题是在Scatter中检索Canvas/LabelX, 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:

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

回答您的第一个问题.它说的是使用规模,而不是大小.还有一种方法 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.xself.y位置添加一个(0,0)的矩形.因此,您的矩形位于该位置.如果拖动(使用手指或鼠标)您的窗口小部件.矩形的位置相对于Scatter保持不变.因此,除非您使用代码更改矩形的位置,否则它将始终位于(0,0)中.转换总是相对于散点图.

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.

此问题可能是相关的,并解释了一些不使用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()

这篇关于散点图大小和位置的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆