KivyMD delete line in file when button ist pressed

I’m having a screen showing me items saved in a file. Every line should be shown on the screen. This works well.

What I want to acchieve is, when the button to the right of an element (icon: ‘delete’) is pressed, the line in that file is removed and the screen is refreshed.

I really do not know how to do this. Please help 🙂

Here’s the python code

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
Window.keyboard_anim_args = {'d': .2, 't': 'in_out_expo'}
Window.softinput_mode = "below_target"
Window.size = (450,800)

from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.navigationbar import MDNavigationBar, MDNavigationItem
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.divider import MDDivider
from kivymd.uix.button import MDButton, MDButtonText, MDButtonIcon
from kivymd.uix.textfield import MDTextField

import os

KV = '''
<MainScreen>:
    MDScreen:
        md_bg_color: app.theme_cls.backgroundColor

<FavoritenScreen>:
    MDScreen:
        ScrollView:
            id: favoriten_sv
            size_hint: None, None
            size: root.width, root.height
            pos_hint: {'center_x': 0.5, 'center_y': 0.3}

            MDBoxLayout:
                id: favoriten_layout
                orientation: 'vertical'
                size_hint_y: None
                height: self.minimum_height

MDBoxLayout:
    orientation: "vertical"

    ScreenManager:
        id:sm
        MainScreen:
            name:
                'Suche'
        FavoritenScreen:
            name:
                'Favoriten'

    MDNavigationBar:
        on_switch_tabs: app.wechsel(*args)

        MDNavigationItem:
            MDNavigationItemIcon:
                icon:'text-search'
            MDNavigationItemLabel:
                text:'Suche'

        MDNavigationItem:
            MDNavigationItemIcon:
                icon:'heart'
            MDNavigationItemLabel:
                text:'Favoriten'

'''


class FavoritenScreen(Screen):
    def on_enter(self):
        self.ids.favoriten_layout.clear_widgets()

        fav = self.manager.get_screen('Suche').favoriten_laden()

        for entry in range(len(fav)):
            if fav[entry].strip():
                if entry > 0:
                    e = fav[entry].split(';')
                    
                    button_layout = MDBoxLayout(orientation='horizontal',size_hint_y=None,height=126)

                    favorite_label = MDTextField(
                        text=e[1] + 'n' + e[0] + 'n' + e[2] + 'n' + e[3],
                        disabled=True,
                        multiline=True,
                        #halign="center",
                    )

                    favorite_button = MDButton(
                        pos_hint={'center_y': 0.45}
                    )


                    favorite_button.add_widget(MDButtonIcon(
                        icon='check'
                    ))

                    delete_button = MDButton(
                        pos_hint={'center_y': 0.45})

                    delete_button.add_widget(MDButtonIcon(
                        icon='delete'
                    ))

                    button_layout.add_widget(favorite_label)
                    button_layout.add_widget(favorite_button)
                    button_layout.add_widget(delete_button)

                    self.ids.favoriten_layout.add_widget(button_layout)

class MainScreen(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        
        self.fav = self.favoriten_laden()

    def favoriten_laden(self):
        if not os.path.exists('f'):
            with open('f', 'w', encoding='utf-8') as w:
                w.write('stadt;marke;adresse;kraftstoff')

        with open('f', 'r', encoding='utf-8') as r:
            fav = r.read()

        return fav.split('n')

class MyApp(MDApp):
    def wechsel(self, bar: MDNavigationBar, item: MDNavigationItem, item_icon: str, item_text: str):
        if item_text == 'Suche':
            self.root.ids.sm.transition.direction = 'right'
        elif item_text == 'Favoriten':
            self.root.ids.sm.transition.direction = 'left'
        self.root.ids.sm.current = item_text

    def build(self):
        self.title = 'Test'
        self.theme_cls.theme_style = 'Dark'
        self.theme_cls.primary_palette = 'Whitesmoke'
        self.screen = Builder.load_string(KV)
        return self.screen

if __name__ == '__main__':
    MyApp().run()

And here’s what the file looks like

A;D;G;J

B;E;H;K

C;F;I;L

You can add an on_release to your MDButton:

                delete_button = MDButton(
                    pos_hint={'center_y': 0.45},
                    on_release=partial(self.do_delete, entry, e))

Now, when that Button is released, the do_delete() method will be called with some entry information.

The do_delete() method then re-writes the file (with that entry removed) and calls the on_enter() method to refresh the screen. Something like this:

def do_delete(self, entry_num, e, button):
    print('delete entry number', entry_num, ':', e, 'from file')
    fav = self.manager.get_screen('Suche').favoriten_laden()
    del fav[entry_num]
    with open('f', mode='wt') as fd:
        for entry in fav:
            fd.write(entry + 'n')
    self.on_enter()

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật