簡體   English   中英

Kivy 下拉菜單未顯示

[英]Kivy dropdown isn't showing

我正在 kivy 中制作一個類似於 HTML 的“SelectBox”小部件。 我希望能夠在 kvlang 中這樣定義它:

(test.kv)

#:import SelectBox widgets

GridLayout:
    cols: 1
    Button:
        text: "hello"
        size_hint_x: None
        size_hint_y: None
    SelectBox:
        id: appeui
        size_hint_x: None
        size_hint_y: None
        width: root.width - 100
        height: '32dp'
        label: "Select an application"
        SelectOption:
            label: 'Option 1'
            value: '1'
        SelectOption:
            label: 'Option 2'
            value: '2'
        SelectOption:
            label: 'Option 3'
            value: '3'
    Button:
        text: "hello2"
        size_hint_x: None
        size_hint_y: None
    Widget:
        size_hint_x: None

我制作了如下所示的小部件,但是由於某種原因,當我單擊按鈕時,什么也沒有出現。 openDropDown確實被調用了,並且DropDown似乎確實有子小部件,但它們沒有出現。

(widgets.py)

from kivy.event import EventDispatcher
from kivy.graphics import Color, Rectangle
from kivy.properties import StringProperty
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.label import Label
from kivy.uix.stacklayout import StackLayout


class SelectOption(Label):
    value = StringProperty('')

    def __init__(self,
                 *args,
                 **kwargs):
        super().__init__(*args, **kwargs)
        self.background_color = Color(rgba=(1, 1, 1, 1))
        self.bind(on_touch_down=self.select)
        with self.canvas.before:
            Color(self.background_color)
            Rectangle(pos=self.pos, size=self.size)

    def select(self, *args):
        self.parent.select(self.value)


class SelectBox(StackLayout, EventDispatcher):
    label = StringProperty('Select an option')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.dropdown = DropDown()
        self.value = None
        self.button = Button(text=self.label)
        self.button.id = 'dropdown_label'
        self.add_widget(self.button)
        self.button.bind(on_release=self.openDropDown)
        self.dropdown.bind(on_select=self.set_value)

    def on_label(self, instance, value):
        self.button.text = value

    def add_widget(self, child):
        if isinstance(child, SelectOption):
            self.dropdown.add_widget(child)
        else:
            super().add_widget(child)

    def openDropDown(self, *args):
        self.dropdown.open(self)

    def set_value(self, instance, value):
        self.ids.dropdown_label.text = value

這將是應用程序: (TestApp.py)

from kivy.app import App


class TestApp(App):
    pass

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

問題是SelectOption的高度。 他們需要將size_hint_y設置為 None 並設置一個固定的高度。

一旦我將它們更改為

class SelectOption(Label):
    value = StringProperty('')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.size_hint_y = None
        self.height = '32dp'

有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM