#python #kivy #kivy-language #kivymd
#питон #киви #киви-язык #кивимд
Вопрос:
Мне нужно записать некоторую информацию о продукте в диалоговом окне для отправки списка MDL, пользователь должен ввести код продукта, и есть отключенное текстовое поле, в котором я хочу, чтобы отображалось название продукта, я хотел бы динамически отображать это имя в его текстовом поле, когда вводится другой код. Коды и имена связаны в словаре.
Должен ли я подходить к этому из файла kv или из файла py?
Вот приложение, похожее на то, что мне нужно:
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: lt;DialogContentgt; 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 lt;MainWindowgt; 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
Ответ №1:
Вам необходимо подключить диалоговое окно с выводом из файла .kv в файл .py.Для этого нам нужно использовать ObjectProperty
. Теперь мы можем проверить написанный код.
файл .py:
from kivy.properties import ObjectProperty() ........ class DialogContent(BoxLayout): name = ObjectProperty() def texting(self,text): if text == '': self.name.text='' else: for key,value in product_dict.items(): if text == key: self.name.text = product_dict[text] break else: self.name.text = '(Code not Valid)' ........
файл .kv:
........ lt;DialogContentgt; name:name 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 on_text: root.texting(self.text) ........