【问题标题】:How can I define kivy dropodown list max height according to the screen height?如何根据屏幕高度定义 kivy 下拉列表最大高度?
【发布时间】:2019-08-28 22:31:44
【问题描述】:

我创建了一个下拉列表,不希望它出现在整个屏幕上。所以我尝试设置 max_height 值,但我只能输入绝对值。相反,我希望它像使用 size_hint 一样随屏幕调整大小。

我也尝试用 kv lang 传递它。创建一个 DropDown 类并将 max_height 设置为 (root.height -20),但它不起作用,因为它试图获取 DropDown 类的高度而不是 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

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    在你的MainWindow__init__(),你可以替换

    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 属性。

    【讨论】:

    • 再次感谢。先生,您真是个奇葩的神。你知道现在用下拉列表中按钮的宽度来做同样的事情吗?我无法根据屏幕大小正确调整大小。
    猜你喜欢
    • 2018-03-18
    • 2014-10-11
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多