如何根据屏幕高度定义kivy dropodown列表的最大高度? [英] How can I define kivy dropodown list max height according to the screen height?

查看:107
本文介绍了如何根据屏幕高度定义kivy dropodown列表的最大高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个下拉列表,并且不希望它遍及整个屏幕.所以我试图设置max_height值,但我只能放入绝对值.相反,我希望它像在使用size_hint一样在屏幕上调整大小.

I've created a DropDown list and dont want it to go all over the screen. So I tried to set the max_height value but i can only put absolute values. Instead, I want it to resize with the screen like I do with size_hint.

我也试图用kv lang传递它.创建一个DropDown类并将max_height设置为(root.height -20),但是由于它尝试获取DropDown类而不是Screen的高度,因此无法正常工作.

I also tried to pass it in kv lang. Creating a DropDown class and setting max_height as (root.height -20), buit it doesnt work as it try to get the height of the DropDown class and not the Screen.

这是我的代码:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button


class MainWindow(Screen):
    min = ObjectProperty(None)
    seg = ObjectProperty(None)

    def __init__(self, **kw):
        super().__init__(**kw)
        self.min_down = DropDown()
        self.sec_down = DropDown()

        for x in range(61):
            btn_min = Button(text=str(x), size_hint_y=None, height=44)
            btn_min.bind(on_release=lambda btn: self.minute(btn.text))
            btn_min.bind(on_release=lambda dismiss: self.min_down.dismiss())
            self.min_down.add_widget(btn_min)

        self.min_down.auto_width = False
        self.min_down.max_height = 100

    def minute(self, texto):
        self.min.text = texto


class NumericInput(TextInput):

    def insert_text(self, string, from_undo=False):
        new_text = self.text + string
        self.input_filter = 'int'
        if new_text != "":
            try:
                if int(new_text) >= 0:
                    self.text = new_text
                    if int(new_text) > 60:
                        self.text = "60"
                    if len(new_text) > 2:
                        self.text = new_text[:-1]

            except ValueError:
                TextInput.insert_text(self, "", from_undo=from_undo)


class WindowManager(ScreenManager):
    pass


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


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


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

对于kv文件:

WindowManager:
    MainWindow:

<MainWindow>:
    name: "main"
    min: min

    FloatLayout:
        Button:
            pos_hint:{'center_x': .27, 'center_y': .6}
            size_hint: .05, .05
            on_release: root.min_down.open(self)

        NumericInput:
            id: min

            pos_hint:{'center_x': .4, 'center_y': .6}
            size_hint: .15, .1
            input_filter: "int"
            hint_text: '00'
            hint_text_color: (0, 0, 0, 1)
            font_size: (root.width/25 + root.height/25)
            halign: "center"
            multiline: False

推荐答案

在您的MainWindow__init__()中,您可以替换

In your MainWindow class __init__(), you can replace

self.min_down.max_height = 100

使用

self.bind(size=self.resizing)

并向该类添加resizing()方法:

def resizing(self, screen, new_size):
    self.min_down.max_height = new_size[1] * 0.5
    self.min_down.width = new_size[0] / 10

    # the child of the DropDownList is a GridLayout that contains the buttons
    for btn in self.min_down.children[0].children:
        btn.width = self.min_down.width

只要MainWindow更改大小,这就会调整max_height属性.

This will adjust the max_height property whenever the MainWindow change its size.

这篇关于如何根据屏幕高度定义kivy dropodown列表的最大高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆