【发布时间】:2022-01-07 22:41:10
【问题描述】:
我需要在MDDialog 内的ThreeLineListItem 中显示字典中的数据,但我的代码似乎不起作用。
这是我的代码:
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDRaisedButton, MDFlatButton
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.list import ThreeLineListItem
from kivymd.uix.snackbar import Snackbar
from kivy.properties import ObjectProperty
KV='''
WindowManager:
#LoginWindow:
MainWindow:
SecondWindow:
<DialogContent>
confirm_check_in_list: confirm_check_in_list
id: confirm_check_in_dialog
orientation: "vertical"
spacing: dp(20)
size_hint_y: None
size_hint_x: None
height: self.height
width: self.width
AnchorLayout:
adaptive_height: True
ScrollView:
MDList:
id: confirm_check_in_list
<MainWindow>
name: 'main'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'test'
MDBoxLayout:
orientation:'vertical'
spacing: dp(10)
padding: dp(20)
MDRaisedButton:
text: 'Click me'
on_release:
app.show_dialog()
'''
product_dict={'name 1': (1, 2), 'name 2': (3,4), 'name 3':(4,2)}
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class DialogContent(BoxLayout):
confirm_check_in_list=ObjectProperty()
def check_conflicts(self, conflicts):
for name, quantity in conflicts.items():
self.ids.confirm_check_in_list.add_widget(
ThreeLineListItem(text=f'{name}',
secondary_text=f'q1: {quantity[0]}',
tertiary_text=f'q2: {quantity[1]}',
)
)
class MainApp(MDApp):
dialog=None
def build(self):
self.theme_cls.theme_style="Dark"
self.theme_cls.primary_palette="Green"
return Builder.load_string(KV)
def close_dialog(self, obj):
self.dialog.dismiss()
def confirm_selection(self, obj):
#check number in quantity field
Snackbar(text='Success').open()
def show_dialog(self):
DialogContent().check_conflicts(product_dict)
if not self.dialog:
self.dialog=MDDialog(
title='Check products',
type='custom',
content_cls=DialogContent(),
buttons=[
MDFlatButton(
text='CANCEL',
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
on_release=self.close_dialog
),
MDRaisedButton(
text='OK',
theme_text_color="Custom",
on_release=
self.confirm_selection
)
],
)
self.dialog.open()
MainApp().run()
我需要从 KV 文件中导入 MDList 的值吗?
【问题讨论】:
标签: python python-3.x kivy kivy-language kivymd