Arthur Pereira
Arthur Pereira

Reputation: 1559

How can I define kivy dropodown list max height according to the screen height?

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.

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.

Here is my code:

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()

And for the kv file:

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

Upvotes: 1

Views: 292

Answers (1)

John Anderson
John Anderson

Reputation: 38992

In your MainWindow class __init__(), you can replace

self.min_down.max_height = 100

with

self.bind(size=self.resizing)

And add a resizing() method to that class:

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

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

Upvotes: 1

Related Questions