KivyMD DatePicker 保存多个日期

KivyMD DatePicker save multiple dates

我希望您可以添加和选择多个日期。我试图通过单击 button_nr 按钮发送一个数字,但我不知道如何将它传递给 on_save 函数,以便我可以更改 self.ids.date_label(button_nr).text 。如果有人对如何保存多个日期有更好的想法,我愿意征求建议。 谢谢

def on_save(self, instance, value, date_range):
    date_value = datetime.datetime.strptime(str(value), "%Y-%m-%d")
    date_value_eu = date_value.strftime("%d.%m.%Y")
    self.ids.date_label1.text = str(date_value_eu)

def show_date_picker(self, button_nr):
    print(button_nr)
    date_dialog = MDDatePicker()
    date_dialog.bind(on_save=self.on_save, on_cancel=self.on_cancel)
    date_dialog.open()

kv文件部分

MDRaisedButton:
        on_release: root.show_date_picker(1)

你可以使用我的代码,我设计这个应用程序是为了有一个回收视图和一个搜索栏,这个例子中的回收列表项被移动到选择日期功能中,它改变了列表项的文本小部件(检查图片),这是jbsidis的代码(我在评论你的问题后发布):

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivymd.uix.list import OneLineIconListItem

from kivy.properties import (
    StringProperty,
    BooleanProperty,
    ObjectProperty,
    NumericProperty,
    ListProperty,
    OptionProperty,
)
Window.size = (330, 660)


kv = '''
#:import MDTextField kivymd.uix.textfield.MDTextField
<MyTile@SmartTileWithLabel>
    size_hint_y: None
    height: "240dp"

<S>:
    MDTextFieldRound:
        pos_hint: {"center_x": .5, "center_y": .95}
        normal_color : [1,1,1,.1]
        color_active : [1,1,1,1]
        size_hint: .8, .07
        hint_text : 'Search a product...'
        icon_left : 'magnify'



<JBSIDIS>
    on_release: app.show_date_picker(root)
    IconLeftWidget:
        icon: "Photos/pro.jpg"

Screen:
    name: "pantalla1"
    MDToolbar:
        id: tb1
        title: "jbsidis"
        pos_hint: {"top": 1}
        md_bg_color: (.1, .1, .5, 1)

    GridLayout:
        cols: 2
        padding: dp(20)
        FloatLayout:
            MDTextFieldRound:
                id: search_field
                pos_hint: {"center_x": .5, "center_y": .87}
                hint_text : 'jbsidis...'
                icon_left : 'magnify'
                normal_color : (100, 100, 50, .5)
                color_active : (255, 255, 255, 0)
                on_text: app.search(root.ids.rvvv9,self.text)

    BoxLayout:
        id: m5
        spacing: dp(10)
        #padding: dp(20)
        pos_hint: {"center_x": .5, "center_y": .32}
        orientation: "vertical"
        BoxLayout:
            id: s_res9
            size_hint_y: None
            height: self.minimum_height
            pos_hint: {"center_x": .5, "center_y": .4}
            orientation: "vertical"
        RecycleView:
            id: rvvv9
            key_viewclass: 'viewclass'
            key_size: 'height'
            RecycleBoxLayout:
                padding: dp(10)
                spacing: dp(5)
                default_size: None, dp(75)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'

    MDToolbar:
        id: success_screen_toolbar
        title: "jbsidis"
        pos_hint: {"top": 1}
        right_action_items: [["progress-check", lambda: print(6)]]


    MDBottomAppBar:
        MDToolbar:
            id: success_screen_bottomappbar
            icon: "magnify"
            on_action_button: app.eq(root.ids.search_field,True)
            type: 'bottom'
            mode: 'center'
            #elevation: '8dp'
            left_action_items: [["calendar-text", lambda x: print(6)], ["account-group", lambda x: print(6)]]
            right_action_items: [["magnify", lambda x: app.eq(root.ids.search_field,True)], ["menu", lambda x: print(6)]]

'''
authors="jbsidis"
class JBSIDIS(OneLineIconListItem):
    datex=StringProperty()
    pass
class blanks1(BoxLayout):
    pass
class S(FloatLayout):
    pass

authors4="jbsidis"
books="""
Austen, Jane (72159) jbsidis
Du Bois, W. E. B. (William Edward Burghardt) (7364) jbsidis
Stowe, Harriet Beecher (7362) jbsidis
Vatsyayana (7343) jbsidis
Schopenhauer, Arthur (7317) jbsidis
Foote, Mary Hallock (7308) jbsidis
Bhide, Shivaram Parashuram (7256) jbsidis
Indrajit, Bhagavanlal (7256) jbsidis
"""
import time
import datetime
class Main(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Green"
        return Builder.load_string(kv)
    def get_date(self, date):
        print(date)
        t=str(date).split("-")
        text="[b]"+str(t[-1])+"/"+str(t[1])+"/"+str(t[0])

        year=time.strftime("%Y")
        mes=time.strftime("%m")
        dia=time.strftime("%d")

        bx.text=text
        afecha=str(t[-1])+"/"+str(t[1])+"/"+str(t[0])
                            
        return date
    def show_date_picker(self,b):
        from kivymd.uix.picker import MDDatePicker
        global bx
        bx=b

        yearx=time.strftime("%Y")
        mes=time.strftime("%m")
        dia=time.strftime("%d")

        current=time.strftime("%Y:%m:%d")

        min_date = datetime.datetime.strptime(current, '%Y:%m:%d').date()
        date_dialog = MDDatePicker(
            callback=self.get_date,
            min_date=min_date
            )
        date_dialog.open()

    def all_pdfs(self):
        return books.splitlines()
    def eq(self,a,b):
        a.focus=True
    def search(self,a,b):
        a.data=[]
        for x in self.all_pdfs():
            if b in str(x):
                a.data.append(
                {
                    "viewclass": "JBSIDIS",
                    "markup": True,
                    "text": "[b][size=33]jbsidis: [/b][/size]"+str(x)
                }
                    )
        pass


Main().run()

这是您可以使用 kivy 和 kivymd、jbsidis 做事的众多方法之一,Python 和 Kivy 一切皆有可能,这是图片,来自萨尔瓦多的问候: