Python中的空白\KivyMd自定义MDDialog

2024-10-01 22:27:40 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我对stackoverflow和python\kivy开发还不熟悉,希望我在这里做的一切都是正确的:D

背景: 我试图用python\kivymd创建一个简单的可重用对话框选择器,在这里我可以抛出一些项目,并获得一个包含要选择的项目的弹出窗口

一切正常,但我在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 )

Tags: fromimportselfiftitledefcallbackitems
1条回答
网友
1楼 · 发布于 2024-10-01 22:27:40

抱歉,我刚发现这容易多了。 如果我使用OneLineavariconListItem,它将与确认对话框一起工作。 这对我来说是可行的,但如果只使用一个LineListItem会更好,但这会导致错误

    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 )

相关问题 更多 >

    热门问题