【问题标题】:How to change the screen size on button press in Kivy如何在 Kivy 中按下按钮时更改屏幕大小
【发布时间】:2018-03-24 08:30:04
【问题描述】:

我尝试使用 Window.size (height,widht) 和 Config,但它无法与屏幕管理器一起使用

【问题讨论】:

标签: python python-3.x kivy kivy-language


【解决方案1】:

解决方案

在每个屏幕中添加一个方法(on_enteron_pre_enter)并使用 Window.size 如下例所示。

示例

main.py

​​>
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window


class MyScreenManager(ScreenManager):
    pass


class Main(Screen):
    def on_pre_enter(self):
        Window.size = (900, 600)


class Login(Screen):
    def on_pre_enter(self):
        Window.size = (400, 300)

    def check_password(self, instance, password):
        if password == "pwd":
            instance.current = "screen2"


class Screen2(Screen):
    pass


class TestApp(App):
    def build(self):
        return MyScreenManager()


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

test.kv

#:kivy 1.10.0

<MyScreenManager>:
    Main:
    Login:
        id: login
    Screen2:

<Main>:
    name: "main"
    BoxLayout:
        orientation: "horizontal"
        Label:
            text: "Hello"
        Button:
            text: "Go to Login Screen"
            on_press: root.manager.current = "screen1"
<Login>:
    name: "screen1"
    GridLayout:
        size_hint: (0.5, 0.5)
        pos_hint: {"center_x": 0.5, "center_y": 0.6}
        rows: 3
        padding: 20

        Label:
            size_hint: (0.2, 0.2)
            text:"Password:"
            font_size: 30
            halign: "center"
            valign: "middle"

        TextInput:
            id: password
            size_hint: (0.2, 0.06)
            cursor_blink: True
            font_size: 20
            multiline: False
            password: True

        Button:
            text: "Continue"
            size_hint: (0.2, 0.08)
            on_release:
                root.manager.ids.login.check_password(root.manager, password.text)

<Screen2>:
    name: "screen2"
    BoxLayout:
        orientation: "horizontal"
        Label:
            text: "Hello"
        Button:
            text: "Go to screen 1"
            on_press: root.manager.current = "screen1"

输出

【讨论】:

  • 非常感谢兄弟。我找不到任何解决方案。
  • 你很受欢迎。请记住通过“单击解决您的问题的答案左侧的绿色轮廓复选标记将问题标记为已回答。这将答案标记为“已接受”,并将问题扩展为“已接受答案”。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 2020-08-14
  • 2016-08-28
  • 1970-01-01
相关资源
最近更新 更多