Kivy按钮和标签尺寸基于显示尺寸

时间:2018-04-08 01:57:18

标签: python kivy

我正在尝试弄清楚如何根据我的显示尺寸使我的按钮和标签完美修复。因此,如果手机显示不同,它将始终保持固定大小。

Python代码:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


class OpeningScreen(Screen):
    pass

class LoginScreen(Screen):
    pass

class SignupScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass


AppKv = Builder.load_file("App.kv")


class MyApp(App):
    def build(self):
        return AppKv


if __name__ == '__main__':
    MyApp().run()

Kv代码:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import hex kivy.utils.get_color_from_hex

#------------------------------------------------------------#

ScreenManagement:
    transition: FadeTransition()
    OpeningScreen:
    LoginScreen:
    SignupScreen:


#------------------------------------------------------------#

<OpeningScreen>:
    name: "OpeningScreen"
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

    Label:
        text: "Welcome"
        color: 1,1,1,1
        font_size: 45
        size_hint: 0.2,0.1
        pos_hint: {"x":0.40, "top":0.995}

    Button:
        size: 100,75
        on_release: app.root.current = "LoginScreen"
        text: "Login"
        font_size: 50
        color: 1,1,1,1
        background_color: (0,0,0,1)
        background_normal: ""
        background_down: ""
        size_hint: 0.3,0.2
        pos_hint: {"x":0.35, "top":0.7}

    Button:
        size: 100,75
        on_release: app.root.current = "SignupScreen"
        text: "Sign up"
        font_size: 50
        color: 1,1,1,1
        background_color: (0,0,0,1)
        background_normal: ""
        background_down: ""
        size_hint: 0.3,0.2
        pos_hint: {"x":0.35, "top":0.4}

#------------------------------------------------------------#

<LoginScreen>:
    name: "LoginScreen"
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text: "Login In"
        color: 0,0,0,1
        font_size: 45
        size_hint: 0.2,0.1
        pos_hint: {"x":0.40, "top":0.995}

#------------------------------------------------------------#

<SignupScreen>:
    name: "SignupScreen"
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text: "Sign Up"
        color: 0,0,0,1
        font_size: 45
        size_hint: 0.2,0.1
        pos_hint: {"x":0.40, "top":0.995}

如果有人能帮助我,我真的很感激。我试图找出如何做到这一点,但我不能

1 个答案:

答案 0 :(得分:0)

可以使用几种不同的方法设置

ButtonLabel尺寸:

  1. 使用Kivy Metrics。您可以使用dp设置尺寸(例如dp(100)),这是与密度无关的像素尺寸。还有一个sp(类似地使用)是与比例无关的像素(通常用于字体大小)

  2. 使用self.texture_size。您可以使用此size: self.texture_size来设置尺寸。这会使您的ButtonLabel小部件足够大,以适应其中的文本。您可以使用以下内容添加一些填充:

    width: self.texture_size[0] + dp(10) height: self.texture_size[1] + dp(10)

  3. 使用size_hint。这将确保ButtonLabel窗口小部件占用相同百分比的显示器,无论设备如何。

  4. 请勿忘记将size_hint设置为None, None以使前两个工作正常。