我需要在 aThreeLineListItem內部顯示字典中的資料MDDialog,但我的代碼似乎不起作用。
這是我的代碼:
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()
我是否需要MDList從 KV 檔案中匯入 的值?
uj5u.com熱心網友回復:
你不能像 : 那樣呼叫你的 MDDialog 類DialogContent().check_conflicts(product_dict)。
您需要訪問self.dialog。因為您是使用自定義設定和自定義內容創建的。否則,您只需呼叫另一個名為 DialogContent 的 BoxLayout。您可以使用 self.dialog名稱來呼叫您創建的對話框。
你需要check_conflicts像這樣運行你的函式:
def show_dialog(self):
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.content_cls.check_conflicts(product_dict)
self.dialog.open()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/371032.html
