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()