I need to show the data from a dictionary in a ThreeLineListItem
inside a MDDialog
, but my code doesn’t seem to work.
Here’s my code:
JavaScript
x
127
127
1
from kivymd.uix.dialog import MDDialog
2
from kivymd.uix.button import MDRaisedButton, MDFlatButton
3
from kivy.lang import Builder
4
from kivymd.app import MDApp
5
from kivy.uix.screenmanager import Screen, ScreenManager
6
from kivy.uix.boxlayout import BoxLayout
7
from kivymd.uix.list import ThreeLineListItem
8
from kivymd.uix.snackbar import Snackbar
9
from kivy.properties import ObjectProperty
10
11
KV='''
12
WindowManager:
13
#LoginWindow:
14
MainWindow:
15
SecondWindow:
16
17
<DialogContent>
18
confirm_check_in_list: confirm_check_in_list
19
id: confirm_check_in_dialog
20
orientation: "vertical"
21
spacing: dp(20)
22
size_hint_y: None
23
size_hint_x: None
24
height: self.height
25
width: self.width
26
27
AnchorLayout:
28
adaptive_height: True
29
ScrollView:
30
31
MDList:
32
id: confirm_check_in_list
33
34
35
36
<MainWindow>
37
name: 'main'
38
MDBoxLayout:
39
orientation: 'vertical'
40
MDToolbar:
41
title: 'test'
42
43
44
45
MDBoxLayout:
46
orientation:'vertical'
47
spacing: dp(10)
48
padding: dp(20)
49
50
51
MDRaisedButton:
52
text: 'Click me'
53
on_release:
54
app.show_dialog()
55
56
'''
57
58
product_dict={'name 1': (1, 2), 'name 2': (3,4), 'name 3':(4,2)}
59
60
61
class MainWindow(Screen):
62
pass
63
64
class SecondWindow(Screen):
65
pass
66
67
class WindowManager(ScreenManager):
68
pass
69
70
class DialogContent(BoxLayout):
71
confirm_check_in_list=ObjectProperty()
72
def check_conflicts(self, conflicts):
73
for name, quantity in conflicts.items():
74
self.ids.confirm_check_in_list.add_widget(
75
ThreeLineListItem(text=f'{name}',
76
secondary_text=f'q1: {quantity[0]}',
77
tertiary_text=f'q2: {quantity[1]}',
78
)
79
)
80
81
82
class MainApp(MDApp):
83
84
dialog=None
85
86
def build(self):
87
self.theme_cls.theme_style="Dark"
88
self.theme_cls.primary_palette="Green"
89
return Builder.load_string(KV)
90
91
def close_dialog(self, obj):
92
self.dialog.dismiss()
93
94
def confirm_selection(self, obj):
95
#check number in quantity field
96
97
Snackbar(text='Success').open()
98
99
def show_dialog(self):
100
DialogContent().check_conflicts(product_dict)
101
102
if not self.dialog:
103
self.dialog=MDDialog(
104
title='Check products',
105
type='custom',
106
content_cls=DialogContent(),
107
buttons=[
108
MDFlatButton(
109
text='CANCEL',
110
theme_text_color="Custom",
111
text_color=self.theme_cls.primary_color,
112
on_release=self.close_dialog
113
),
114
MDRaisedButton(
115
text='OK',
116
theme_text_color="Custom",
117
on_release=
118
self.confirm_selection
119
120
)
121
],
122
)
123
self.dialog.open()
124
125
126
MainApp().run()
127
Do I need to import the values for the MDList
from the KV file?
Advertisement
Answer
You can’t call your MDDialog class like : DialogContent().check_conflicts(product_dict)
.
You need to reach self.dialog
.Because you created this with your custom settings and custom content. Otherwise, you just calling another BoxLayout which name is DialogContent. You can use self.dialog
name for call your created dialog.
You need to run your check_conflicts
function like that:
JavaScript
1
25
25
1
def show_dialog(self):
2
if not self.dialog:
3
self.dialog=MDDialog(
4
title='Check products',
5
type='custom',
6
content_cls=DialogContent(),
7
buttons=[
8
MDFlatButton(
9
text='CANCEL',
10
theme_text_color="Custom",
11
text_color=self.theme_cls.primary_color,
12
on_release=self.close_dialog
13
),
14
MDRaisedButton(
15
text='OK',
16
theme_text_color="Custom",
17
on_release=
18
self.confirm_selection
19
20
)
21
],
22
)
23
self.dialog.content_cls.check_conflicts(product_dict)
24
self.dialog.open()
25