【发布时间】:2022-01-07 11:28:59
【问题描述】:
我需要在对话框中捕获一些产品信息以提供 MDList,用户必须输入产品代码,并且有一个禁用的文本字段,我希望在其中显示产品名称,我想显示这个当引入不同的代码时,在其文本字段中动态命名。代码和名称在字典中是相关的。
我必须从 kv 文件还是从 py 文件来处理这个问题?
这是我需要的类似应用:
一年
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 TwoLineListItem
from kivymd.uix.snackbar import Snackbar
product_dict={'1': 'name 1', '2': 'name 2', '3': 'name 3'}
product_dict_values=list(product_dict.values())
product_dict_keys=list(product_dict.keys())
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class DialogContent(BoxLayout):
pass
class MainApp(MDApp):
dialog=None
def build(self):
self.theme_cls.theme_style="Dark"
self.theme_cls.primary_palette="Green"
return Builder.load_file('exam.kv')
def close_dialog(self, obj):
self.dialog.dismiss()
def confirm_selection(self, obj):
#check number in quantity field
if self.dialog.content_cls.ids.code.text not in product_dict_keys:
self.dialog.content_cls.ids.code.text=""
Snackbar(text='Code not in product_dict').open()
else:
self.root.get_screen('main').ids.list.add_widget(
TwoLineListItem(text=f'Name: {self.dialog.content_cls.ids.name.text}',
secondary_text=f'Code: {self.dialog.content_cls.ids.code.text}'
)
)
#clear text fields after confirmation
self.dialog.content_cls.ids.name.text=""
self.dialog.content_cls.ids.code.text=""
Snackbar(text='Success').open()
def show_dialog(self):
if not self.dialog:
self.dialog=MDDialog(
title='Add details',
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()
千伏
WindowManager:
MainWindow:
SecondWindow:
<DialogContent>
id: dialog
orientation: "vertical"
spacing: "10dp"
size_hint_y: None
height: "160dp"
MDTextField:
id: name
text: self.text #self.text if code.text is None else product_dict[code.text]
disabled: True
hint_text: "Name"
#required: True
MDTextField:
id: code
hint_text: "Code"
required: True
<MainWindow>
name: 'main'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'test'
MDBoxLayout:
orientation:'vertical'
spacing: dp(10)
padding: dp(20)
MDRaisedButton:
text: 'fill'
on_release:
app.show_dialog()
AnchorLayout:
adaptive_height:True
ScrollView:
MDList:
id: list
【问题讨论】:
标签: python kivy kivy-language kivymd