【问题标题】:KivyMD DatePicker save multiple datesKivyMD DatePicker 保存多个日期
【发布时间】:2021-08-18 12:23:50
【问题描述】:

我希望您可以添加和选择多个日期。我尝试通过单击按钮发送一个号码 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)

【问题讨论】:

  • 我差不多有答案了,我只是想问一下,你需要将已经选择的相同日期转换为不同的日期格式,还是两者都需要,将该日期转换为不同的日期格式并同时更新 self.ids.date_label 小部件中的文本?

标签: python datepicker kivy kivymd


【解决方案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 一切皆有可能,这是来自萨尔瓦多的图片,问候:

【讨论】:

  • 它真的不适合我。我希望您可以使用按钮打开日历并将日期保存在文本上。然后在下一个按钮上再次执行相同的操作。用户还应该能够添加更多按钮。所以不同的按钮应该能够使用“show_date_picker”功能。我的问题是我不知道如何将 button_nr 传递给 'on_save' 函数
  • 好吧,我当然可以帮你做,你可以编辑你的问题并添加完整的代码,或者至少添加你需要的最小部分(正如你在评论中解释的那样),所以只需分享足够的代码,我会为您完成,否则,请查看我上面给出的答案并分析小部件(在您的情况下为 button_nr)如何作为参数传递给主类函数以及我如何分配列表中给出的文本的值,或共享更多代码(在您的代码中,您需要向我们提供有关 kv lang 部分的更多信息。或者只需将 id“button_nr”添加到小部件
  • 谢谢。我可以解决它。你知道有没有办法在日历中选择多个日期,而不是一个范围
  • 感谢您的回复,这是一个很好的问题,我知道最新的 KivyMD 具有选择日期范围的功能,但它没有选择多个日期并保存它们的选项(可能是在列表中),但这实际上是可能的,我将修改 MDDatePicker,也许我将添加一个额外的功能,以便能够选择多个日期而不是仅仅一个日期范围,这需要时间,但这是可能的.
猜你喜欢
  • 2011-04-05
  • 1970-01-01
  • 2017-02-17
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多