KivyMD 中的调整大小对话框

问题描述 投票:0回答:1

我正在尝试创建一个包含 MDTextField 和键盘的对话框。我希望对话包含所有数据。但是,它告诉我,我通过在背景后面显示黑色阴影来对对话框应用一些大小。但是当我单击某个键时,我可以看到正确的格式快速弹出,然后离开屏幕。以下是它的外观以及我想要的外观的图片。

这是我正在使用的代码。

    def open_keyboard(self):
        if self.dialog:
            self.dialog.dismiss()

        keyboard_content = MDBoxLayout(orientation="vertical", adaptive_height = True)
        keyboard_content.add_widget(KeyboardContent())

        self.dialog = MDDialog(
            type="custom",
            content_cls=keyboard_content,
            size_hint=(0.8, .9) 
        )
        self.dialog.open()

这是 kivy 代码

<KeyboardContent>:
BoxLayout:
    orientation: "vertical"

    MDTextField:
        id: text_input
        hint_text: "Type here"
        required: True
        max_text_length: 50

    GridLayout:
        cols: 6
        spacing: "10dp"

        MDRectangleFlatButton:
            text: "A"
            on_release:
                root.add_text("A")
                app.speak_text("a")

        MDRectangleFlatButton:
            text: "B"
            on_release:
                root.add_text("B")
                app.speak_text(self.text)
python kivy dialog kivymd
1个回答
0
投票

如果我理解您要做什么,我认为您只需要在

minimum_height
代码的几个地方使用内置
kv
即可。由于您没有包含您的
KeyboardContent
课程,因此我创建了一个:

class KeyboardContent(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        for letter in ascii_uppercase:
            butt = MDRectangleFlatButton(text=letter, on_release=partial(self.add_text, letter))
            self.ids.grid.add_widget(butt)

    def add_text(self, letter, button_instance):
        self.ids.text_input.text += letter

然后我修改了你的

kv
:

<KeyboardContent>:
    orientation: "vertical"
    size_hint_y: None
    height: self.minimum_height

    MDTextField:
        id: text_input
        hint_text: "Type here"
        required: True
        max_text_length: 50

    GridLayout:
        id: grid
        cols: 6
        spacing: "10dp"
        size_hint_y: None
        height: self.minimum_height

我在

id
上添加了
grid
GridLayout
,并在两个地方使用了
self.minimum_height
。现在你的
open_keyboard()
方法可以更简单一点:

def open_keyboard(self, *args):
    if self.dialog:
        self.dialog.dismiss()

    self.dialog = MDDialog(
        type="custom",
        content_cls=KeyboardContent(),
        size_hint=(0.8, None)
    )
    self.dialog.open()

注意

size_hint_y
现在是
None
。这允许改变高度。

© www.soinside.com 2019 - 2024. All rights reserved.