I have a code that implement friends list. When you click message icon it'll appear the popup with title of whom message it. I need to get friend's name from MDListItemHeadlineText where user click this icon. My kv-code is here:
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.dialog import MDDialog, MDDialogHeadlineText, MDDialogButtonContainer, MDDialogContentContainer
from kivymd.uix.screen import MDScreen
from kivymd.uix.textfield import MDTextField
from kivymd.uix.list import (MDListItem, MDListItemHeadlineText, MDListItemSupportingText,
MDListItemTertiaryText, MDListItemTrailingIcon)
from kivymd.uix.behaviors import RotateBehavior
from kivy.uix.behaviors import ButtonBehavior
KV = '''
ScreenManager:
Main:
FriendsList:
id: friends_list
name: 'friends_list'
<Main>
BoxLayout:
orientation: 'vertical'
spacing: 10
Button:
text: 'Friends'
on_press: root.list_click()
<FriendsList>
BoxLayout:
orientation: 'vertical'
spacing: 10
canvas:
Color:
rgba: 255/255, 255/255, 255/255, 1
Rectangle:
source: "./Images/main_background.jpg"
size: self.size
pos: self.pos
MDScrollView:
id:scroll
MDList:
id: lists
'''
and my py-file sample:
class TrailingPressedIconButton(ButtonBehavior, RotateBehavior, MDListItemTrailingIcon):
...
class FriendsList(Screen):
def on_enter(self):
file_reader = [['name_1', 'text_1', 'text_text_1'],
['name_2', 'text_2', 'text_text_2'],
['name_3', 'text_3', 'text_text_3']]
for row in file_reader:
self.ids.lists.add_widget(MDListItem(
MDListItemHeadlineText(text=row[0], theme_text_color='Custom', text_color='black'),
MDListItemSupportingText(text=row[1], theme_text_color='Custom', text_color='grey'),
MDListItemTertiaryText(text=row[2], theme_text_color='Custom', text_color='black'),
TrailingPressedIconButton(icon="email", on_release=lambda x: self.message_dialog_open(x.text))
)
)
def message_dialog_open(self, friend_name):
self.message_input_field = MDTextField(mode="filled", height="100dp", multiline=True)
self.message_dialog = MDDialog(
MDDialogHeadlineText(text=f'Message {friend_name}'),
MDDialogContentContainer(self.message_input_field),
MDDialogButtonContainer(
Widget(),
MDButton(
MDButtonText(text="Cancel"),
style="text",
on_release=lambda *args: self.message_dialog.dismiss()
),
MDButton(
MDButtonText(text="Send"),
style="text",
on_release=lambda *args: self.message_send_button_click()
),
spacing="8dp",
),
)
self.message_dialog.open()
class Main(Screen):
def list_click(self):
self.manager.current = 'friends_list'
self.manager.transition.direction = 'left'
class Test(MDApp, MDScreen):
def build(self):
return Builder.load_string(KV)
Kivy version: 2.0.1dev0
Python: 3.10
I tried to wrap the MDListItemHeadlineText as an object, but it didn't work either.
I will be glad to receive every answer and help in this
Try changing the line:
TrailingPressedIconButton(icon="email", on_release=lambda x: self.message_dialog_open(x.text))
to:
TrailingPressedIconButton(icon="email", on_release=lambda button, friend=row[0]: self.message_dialog_open(button, friend))
This will pass the name of the friend to your message_dialog_open()
method. That requires a small change to that method from:
def message_dialog_open(self, friend_name):
to:
def message_dialog_open(self, button, friend_name):
Now your message_dialog_open()
method will receive the friend name.