首先,我对stackoverflow和python\kivy开发还不熟悉,希望我在这里做的一切都是正确的:D
背景: 我试图用python\kivymd创建一个简单的可重用对话框选择器,在这里我可以抛出一些项目,并获得一个包含要选择的项目的弹出窗口
一切正常,但我在ScrollView上面有一个空白,我不知道这是从哪里来的
我确信有更好的方法可以做到这一点,但这正是我想到的(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 )
抱歉,我刚发现这容易多了。 如果我使用OneLineavariconListItem,它将与确认对话框一起工作。 这对我来说是可行的,但如果只使用一个LineListItem会更好,但这会导致错误
相关问题 更多 >
编程相关推荐