Kivy,ScreenManager 尺寸问题

Kivy, ScreenManager sizing issue

我正在尝试使用 Kivy 为 Android 创建游戏。现在的想法是拥有主菜单,然后是导航按钮。 (即屏幕尺寸需要 9:16)


起初,我设计了整个主菜单,(按钮的位置等),一切都很完美。

然而,为了继续前进,我不得不开始使用 'ScreenManager'。一旦我这样做了,所有的按钮和尺寸都非常错误和巨大。

经过进一步检查,我意识到只有将“class MainMenu(Widget):”更改为“class MainMenu(Screen)",我不知道为什么或如何会产生如此大的差异,解决方法是。

我将尝试提供屏幕截图以帮助进一步解释我的意思。

Py 之前:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.config import Config

Config.set('graphics', 'width', '900')
Config.set('graphics', 'height', '1600')    


class MainMenu(Widget):
    pass


class BackGardenApp(App):
    def build(self):
        return MainMenu()


BackGardenApp().run()

Kv 之前:

<MainMenu>:
    name: "MainMenu"
    canvas:
        Rectangle:
            source: 'Background.Jpg'
            size: root.width, root.height

    Label:
        bold: 'true'
        font_name: 'Misty Cotton.ttf'
        font_size : root.width * 0.25
        center_x : root.width / 2
        top: root.top * .92
        text: "Back"
    Label:
        bold: 'true'
        font_name: 'Misty Cotton.ttf'
        font_size : root.width * 0.25
        center_x : root.width / 2
        top: root.top * .75
        text: "Garden"

    Button:
        text: "Play"
        font_name: 'font3.ttf'
        font_size: root.width * .06
        size: root.width*.8 + 100, root.height*0.1
        background_normal: 'ButtonLong.png'
        background_down: 'ButtonLongPressed.png'
        pos: ((root.width/2)-((root.width*.8 + 100)/2)), root.height * 0.18

    Button:
        text: "Settings"
        font_name: 'font3.ttf'
        font_size: root.width * .06
        size: root.width*.4, root.height*0.1
        background_normal: 'Button.png'
        background_down: 'ButtonPressed.png'
        pos: root.width*0.5 + 50, root.height * 0.0625

    Button:
        text: "Shop"
        font_name: 'font3.ttf'
        font_size: root.width * .06
        size: root.width*.4, root.height*0.1
        background_normal: 'Button.png'
        background_down: 'ButtonPressed.png'
        pos: root.width*0.1 - 50, root.height * 0.0625

Py 之后:

from kivy.app import App
# from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

width = 900
height = 1600


def screensize():
    Window.size = (width, height)


class LoadingIn(Screen):
    screensize()
    pass


class MainMenu(Screen):
    screensize()
    pass


class ScreenManagement(ScreenManager):
    screensize()
    pass


kv = Builder.load_file("BackGarden.kv")


class BackGardenApp(App):
    def build(self):
        return kv


if __name__ == "__main__":
    BackGardenApp().run()

Kv 之后: (数字是百分比,因为它允许它以任何 window 尺寸提供。)

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    LoadingIn:
    MainMenu:

<LoadingIn>
    name: "Loading"
    Button:
        on_release: app.root.current = "MainMenu"

<MainMenu>:
    name: "MainMenu"
    canvas:
        Rectangle:
            source: 'background.jpg'
            size: root.width, root.height

    Label:
        id: logo1
        bold: 'true'
        font_name: 'Misty Cotton.ttf'
        font_size : (root.width * 0.35)
        center_x : root.width / 2
        top: root.top * .90
        text: "Back"
    Label:
        id: logo2
        bold: 'true'
        font_name: 'Misty Cotton.ttf'
        font_size : (root.width * 0.35)
        center_x : root.width / 2
        top: root.top * .75
        text: "Garden"

    Button:
        text: "Play"
        font_name: 'font3.ttf'
        font_size: root.width * .06
        size: root.width*.8 + 100, root.height*0.1
        background_normal: 'ButtonLong.png'
        background_down: 'ButtonLongPressed.png'
        pos: ((root.width/2)-((root.width*.8 + 100)/2)), root.height * 0.18

    Button:
        text: "Settings"
        font_name: 'font3.ttf'
        font_size: root.width * .06
        size: root.width*.4, root.height*0.1
        background_normal: 'Button.png'
        background_down: 'ButtonPressed.png'
        pos: root.width*0.5 + 50, root.height * 0.0625

    Button:
        text: "Shop"
        font_name: 'font3.ttf'
        font_size: root.width * .06
        size: root.width*.1, root.height*0.1
        background_normal: 'Button.png'
        background_down: 'ButtonPressed.png'
        pos: root.width*0.1 - 50, root.height * 0.0625

我怀疑问题是由于屏幕 class 识别了 size_hint 之类的东西,而小部件 class 却没有。因此,Shop 按钮的大小将由其 size_hint 而不是其大小来定义。尝试更改大小:root.width*.1、root.height*0.1 到 size_hint:0.1、0.1,其他按钮也类似。