I followed the Kivymd 1.1.1 docs for how to make a list and add an item to it, and I spent days trying to fix the it, but I did not find any solution. That's my code. What's the problem with the code?
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.list import IRightBodyTouch, ThreeLineAvatarIconListItem
from kivymd.uix.pickers import MDDatePicker
from kivymd.uix.selectioncontrol import MDCheckbox
from kivy.properties import StringProperty
class TaskScreen(Screen):pass
class AddTask(Screen):pass
class Manager(ScreenManager):pass
class ListItemWithCheckbox(ThreeLineAvatarIconListItem):
"""'''Custom list item.'''
The list item will be added to the task list"""
icon = StringProperty("android")
class RightCheckbox(IRightBodyTouch, MDCheckbox):
'''Custom right container.'''
class TasksApp(MDApp):
# def __init__(self, **kwargs):
# super().__init__(**kwargs)
def add_task(self,title, task, date):
title = self.root.ids.add_task.ids.title.text
task = self.root.ids.add_task.ids.task.text
date = self.root.ids.add_task.ids.date.text
task_item =ListItemWithCheckbox(text=title,
secondary_text=task,
tertiary_text=date,
)
self.root.ids.task_screen.ids.container.add_widget(task_item)
self.clean_fields(title,task,date)
def clean_fields(self,title,task,date):
self.root.ids.add_task.ids.title.text = ''
self.root.ids.add_task.ids.task.text = ''
self.root.ids.add_task.ids.date.text = ''
def open_date_picker(self):
date_dialog = MDDatePicker()
date_dialog.bind(on_save=self.on_save)
date_dialog.open()
def on_save(self, instance, value, date_range):
value = value.strftime('%A %d %B %Y')
self.root.ids.add_task.ids.date.text = str(value)
def remove_item(self,task_item):
self.root.ids.task_screen.ids.container.remove_widget(task_item)
def build(self):
self.theme_cls.primary_palette = 'Teal'
return Builder.load_file('tasks.kv')
TasksApp().run()
and the kv file:
#: import CardTransition kivy.uix.screenmanager.CardTransition
#: import datetime datetime
# :set date_text datetime.now().strftime('%A %d %B %Y')
Manager:
id:sm
TaskScreen:
id: task_screen
AddTask:
id: add_task
<TaskScreen>:
name: 'task_screen'
MDFloatLayout:
orientation:'vertical'
MDLabel:
text: "[size=40][u][b]Tasks[/b][/u][/size]"
color: 'teal'
markup:True
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y': 0.95}
MDLabel:
id: date_now
text:str(datetime.datetime.now().strftime('[b][size=25]Today: [/size][/b][b][size=20]%A %d %B %Y -%I:%M[/size][/b]'))
color: 'teal'
markup:True
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y': 0.88}
ScrollView:
MDList:
id: container
MDFloatingActionButton:
elevation:3
icon: 'plus-outline'
pos_hint: {'center_x': 0.5,'center_y': 0.09}
on_release:
# app.root.transition.direction = 'left'
app.root.transition = CardTransition(mode='pop')
app.root.current = 'add_task'
<AddTask>:
name: 'add_task'
MDLabel:
text: "[size=40][u][b]Add Task[/b][/u][/size]"
color: 'teal'
markup:True
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y': 0.95}
MDLabel:
id: date_now
text:str(datetime.datetime.now().strftime('[b][size=25]Today: [/size][/b][b][size=20]%A %d %B %Y - %I:%M[/size][/b]'))
color: 'teal'
markup:True
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y': 0.88}
MDLabel:
id: date
text: '[u][size=15]___[/size][/u]'
color: 'teal'
markup:True
pos_hint: {'center_x': 0.65,'center_y': 0.75}
MDIconButton:
icon:'calendar-multiselect'
icon_size:'48sp'
theme_icon_color: "Custom"
icon_color: app.theme_cls.primary_color
pos_hint: {'center_x':0.75,'center_y': 0.75}
on_release: app.open_date_picker()
MDLabel:
text: "[size=20][u][b]Title[/b][/u][/size]"
color: 'teal'
markup: True
pos_hint: {'center_x': 0.65,'center_y': 0.6}
MDTextField:
id: title
pos_hint:{"center_x": 0.5,'center_y': 0.5}
size_hint_x: 0.7
max_text_length: 20
font_size: 20
bold:True
MDLabel:
text: "[size=20][u][b]Enter Task[/b][/u][/size]"
color: 'teal'
markup:True
pos_hint: {'center_x': 0.65,'center_y': 0.4}
MDTextField:
id: task
pos_hint:{"center_x": 0.5,'center_y': 0.3}
size_hint_x: 0.7
max_text_length:60
font_size:19
bold:True
MDRaisedButton:
text:'Save'
pos_hint:{"center_x": 0.72,'center_y': 0.15}
on_release:
app.add_task(title, task, date)
app.root.transition = CardTransition(mode='pop')
app.root.current = 'task_screen'
MDIconButton:
icon: 'arrow-u-right-top'
icon_size:'48sp'
theme_icon_color: "Custom"
icon_color: app.theme_cls.primary_color
pos_hint:{'center_x':0.5,'center_y': 0.15}
on_release:
# app.root.transition.direction = 'right'
app.root.transition = CardTransition(mode='pop')
app.root.current = 'task_screen'
app.clean_fields(title, task, date)
<ListItemWithCheckbox>:
id: task_item
RightCheckbox:
id : checbox
IconLeftWidget:
icon: 'trash-can-outline'
theme_text_color: "Custom"
text_color: 'teal' #1, 0, 0, 1
on_release:app.remove_item(task_item)
and the pic for the app to icons and to check boxes
search on the web reading docs try to change the list items and positions of widgets and check for any code mistakes or typo ....
Not sure if this is the answer, but the line in your build()
method:
return Builder.load_file('tasks.kv')
Is loading tasks.kv
for the second time. That kv
file is loaded automatically by the App
because of its name (see the documentation). You should either change the name of your kv
file, or just eliminate the call to Build.load_file()
.