【问题标题】:Blank Space in Python\KivyMd custom MDDialogPython\KivyMd 自定义 MDDialog 中的空格
【发布时间】:2021-08-26 08:25:10
【问题描述】:

起初我是 stackoverflow 和 python\kivy 开发的新手,希望我能在这里做所有事情:D。

背景: 我尝试使用 python\kivymd 创建一个简单的可重用 DialogSelector,我可以在其中放入一些项目并弹出一个包含要选择的项目的弹出窗口。

一切都按预期工作,但我在 ScrollView 上方有一个空白区域,我不知道这是从哪里来的。

Dialog

我确信有更好的方法可以做到这一点,但这就是我想出的(DialogSelector.py):

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineListItem
from kivymd.uix.button import MDFlatButton


Builder.load_string( """
<CustomDLG>
    orientation: "vertical"
    spacing: "12dp"
    size_hint_y: None
    height: "200dp"
    pos_hint: { "top": 1 }

    ScrollView:
        id: scView
        
        MDList:
            id: itemList
""" )


class CustomDLG( BoxLayout ):
    pass


class DialogSelector():

    def __init__( self, title, items, callback ):
        self.items = items
        self.title = title
        self.callback = callback
        self.dialog = None
        self.displayed = None
        self.keyword = None

    def open(self, *args):
        if not self.dialog:
            self.dialog = MDDialog(
                title=self.title,
                type="custom",
                size_hint=(.8, .7),
                content_cls=CustomDLG(),
                buttons=[ MDFlatButton( text="Abbrechen",  on_release=self.close ) ]
            )

        self.dialog.content_cls.ids.itemList.clear_widgets()
        for entry in self.items:
            if type(entry) is str: entry = { "Keyword": entry, "Displayed": entry }
            if type(entry) is dict:
                if "Keyword" in entry and "Displayed" in entry:
                    self.dialog.content_cls.ids.itemList.add_widget( 
                        OneLineListItem( id=str(entry["Keyword"]), text=str(entry["Displayed"]), on_release=self.itemSelected ) 
                    )

        self.dialog.open()


    def close(self, *args):
        self.dialog.dismiss( force=True )


    def itemSelected(self, item):
        self.displayed = item.text
        self.keyword = item.id
        if self.dialog: self.dialog.dismiss( force=True )
        self.callback( self )

用法(其他一些.py):

from CustomWidgets.DialogSelector import DialogSelector

    def openCategoryDialog(self):
        self.dialog = DialogSelector( 
            title = "Kategorie wählen", 
            items = [{"Keyword":"A", "Displayed":"ABCDE"},{"Keyword":"B", "Displayed":"BEDF"}], 
            callback = self.categorySelected 
            )
        self.dialog.open()

    def categorySelected(self, dlg):
        print( dlg.keyword )
        print( dlg.displayed )

【问题讨论】:

    标签: python kivy kivy-language kivymd


    【解决方案1】:

    抱歉,发现它更容易。 如果我使用 OneLineAvatarIconListItem 它与确认对话框一起使用。 这对我有用,但仅使用 OneLineListItem 会更好,但这会导致错误。

        def openCategoryDialog(self):
            self.dialog = MDDialog( 
                title="Kategorie wählen",
                type="confirmation",
                size_hint = (.8, .7),
                items=[
                    OneLineAvatarIconListItem(id="A01", text="Callisto", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="B02", text="Luna", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="C03", text="Night", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="D04", text="Solo", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="E05", text="Phobos", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="F06", text="Diamond", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="G07", text="Sirena", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="H08", text="Red music", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="I09", text="Allergio", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="J10", text="Magic", on_release=self.categorySelected),
                    OneLineAvatarIconListItem(id="K11", text="Tic-tac", on_release=self.categorySelected)
                ],
                buttons=[
                    MDFlatButton( text="Abbrechen", on_release=self.closeCategoryDialog ),
                ]
            )
            self.dialog.open()
    
        def closeCategoryDialog(self, *args):
            self.dialog.dismiss( force=True )
    
        def categorySelected(self, dlg):
            print( dlg.id )
            print( dlg.text )
            self.dialog.dismiss( force=True )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 2020-12-14
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多