【问题标题】:Kivy, ScreenManager sizing issueKivy,ScreenManager 尺寸问题
【发布时间】:2020-12-23 20:10:03
【问题描述】:

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


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

但是,要继续前进,我必须开始使用“ScreenManager”。一旦我这样做了,所有的按钮和尺寸都非常错误和巨大。

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

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

之前的:

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 之后: (数字是百分比,因为它可以在任何窗口大小下使用。)

#: 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

【问题讨论】:

  • 我怀疑问题是由于Screen 类识别出size_hint 之类的东西,而Widget 类却没有。因此Shop 按钮的大小将由其size_hint 而非size 定义。尝试将size: root.width*.1, root.height*0.1 更改为size_hint: 0.1, 0.1,其他按钮类似。
  • @JohnAnderson,这就是我喜欢这个网站的原因 :) 它成功了,谢谢!

标签: python kivy kivy-language


【解决方案1】:

我怀疑问题是由于 Screen 类识别了 size_hint 之类的东西,而 Widget 类却没有。所以 Shop 按钮的大小将由它的 size_hint 而不是它的大小来定义。尝试将 size: root.width*.1, root.height*0.1 更改为 size_hint: 0.1, 0.1,其他按钮类似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多