如何在Kivy中动态调整窗口大小?

时间:2019-03-30 02:31:21

标签: python window kivy size

在构建桌面应用程序时遇到问题,因为在不同的屏幕中我需要不同的窗口大小(例如登录名,菜单等) 我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以在屏幕on_pre_enter事件中更改窗口大小。切换屏幕时更改窗口大小时最好不要过渡。
试试这个例子:

from kivy.app import App
from kivy.lang import Builder


KV = """
#:import Window kivy.core.window.Window
#:import NoTransition kivy.uix.screenmanager.NoTransition

ScreenManager:
    transition: NoTransition()
    Screen:
        on_pre_enter:
            Window.size = (400, 400)
        name: "scr1"
        Button:
            font_size: "40sp"
            text: "Next screen"
            on_release:
                root.current = "scr2"
    Screen:
        on_pre_enter:
            Window.size = (200, 50)
        name: "scr2"
        Button:
            font_size: "20sp"
            text: "Previous screen"
            on_release:
                root.current = "scr1"

"""


class MyApp(App):
    def build(self):
        return Builder.load_string(KV)

MyApp().run()