Python, Kivy - установить размер кнопки относительно размера изображения - PullRequest
0 голосов
/ 04 апреля 2020

Я делаю игру с Киви. Я хотел бы установить размер кнопки, равный половине размера изображения.

Это мой .kv

<HomeScreen>:
Image:
    source: "data/background.png"

    Button:
        background_color: 0, 0, 0, 0
        width: root.width / 1.5
        height: root.height / 7.5
        pos: (root.width / 2) - (self.width / 2), root.height - root.height * 0.4

        on_press: app.change("PlayScreen") # change screen function

        Image:
            pos: self.parent.x, self.parent.y
            width: self.parent.width
            height: self.parent.height

            source: "data/button.png"

            Label:
                pos: self.parent.center_x - (self.width / 2), (self.parent.height / 2) - (self.height / 2.25) + self.parent.y

                color: 0, 0, 1, 1
                font_size: self.parent.height / 3
                text: "Play"

Это мой Python:

class handler(App):
    def __init__(self, **kwargs):
        Builder.load_file("data/layouts.kv")
        self.sm = getSM()
        self.places = {0: "HomeScreen"}

        super().__init__(**kwargs)

    def build(self):

        Window.size = (1000, 800)
        Window.minimum_width, Window.minimum_height = Window.size
        self.title = "Digital Drummer"

        return self.sm

    def change(self, to):
        print(self.root.children[0].children[0].size)

        if to == "Back":
            print("Changing from " + self.sm.current + " to " + self.places[len(self.places) - 2])

            self.root.transition = FadeTransition(duration=0.5)

            self.sm.current = self.places[len(self.places) - 2]
            del self.places[len(self.places) - 1]


        else:
            print("Changing from " + self.sm.current + " to " + to)

            self.root.transition = FadeTransition(duration=0.5)

            self.sm.current = to
            self.places[len(self.places)] = self.sm.current

Когда я изменяю размер по вертикали, изображения кнопок увеличиваются и перемещаются вверх как мое фоновое изображение движется вниз, как он центрируется.

Я пытался использовать функцию -

def getFrame(self):
    print(self.children[0].width, self.children[0].height)
    return self.children[0]

и запускать root.getFrame(), но размеры были только размером окна, даже если показанное изображение не было.

Как я могу получить размер и положение фонового изображения?

...