gpt4 book ai didi

python - 散点大小和位置的问题

转载 作者:行者123 更新时间:2023-11-28 21:59:06 27 4
gpt4 key购买 nike

我遇到了一些与散点对象相关的问题。从我下面的代码。在我调整 Scatter(self.size_hint_x, self.size_hint_y = 0.3, 0.3)的大小后,对象 (canvas, label) inside Scatter 也没有调整大小。我确实尝试将 size_hint=1 应用于 Scatter 中的 CanvasLabel,但结果仍然是一样。

我遇到的另一个问题是检索 Canvas/LabelX, Y 位置(相对于父级) 分散。它总是给我 (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()

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

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

最佳答案

您是否阅读了 Scatter 的文档?根本。上面写着

...specific behavior makes the scatter unique, and there are some advantages / constraints that you should consider:

  1. The children are positioned relative to 0, 0. The scatter position has no impact of the position of children.
  2. This applies to the size too. If you want to resize the scatter, use scale, not size. (read #1.)

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

关于你的第二个问题。您正在 self.xself.y 位置添加一个 Rectangle,即 (0,0)。所以,你的 Rectangle 就在那个位置。如果您拖动(使用手指或鼠标)您的小部件。 Rectangle 的位置仍然相对于 Scatter。因此,除非您更改矩形的位置(使用代码),否则它将始终位于 (0,0) 中。转换始终与散点相关。

This question可能相关并解释了一些不使用 Kivy 语言添加顶点指令(即矩形)的问题。你应该考虑它,因为你所做的似乎是相关的。

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

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) 而不是:

self.size_hint_x, self.size_hint_y = 0.3, 0.3

使用:

self.scale = 0.3

2) 位置是相对散点的。因此,您需要将坐标转售给父级。

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()

关于python - 散点大小和位置的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17185800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com