【问题标题】:Python/Kivy : select date from calendar and put into TextInputPython/Kivy:从日历中选择日期并放入 TextInput
【发布时间】:2018-07-24 12:50:49
【问题描述】:

我正在使用python-2.7 和 kivy。当我点击cross.png 然后日历打开。当我点击日历时,如何获得选定的日期?实际上我需要选择日期放入TextInput。谁能告诉我如何使它成为可能?

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from KivyCalendar import DatePicker
from kivy.core.window import Window

Window.clearcolor = (0.5, 0.5, 0.5, 1)

class Calendar(BoxLayout):
    def __init__(self):
        super(Calendar, self).__init__()

    def show_calendar(self):
        datePicker = DatePicker()
        datePicker.show_popup(1,.3)


class Test(App):
    def build(self):
        return Calendar()


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

test.kv

<Calendar>:
    BoxLayout:
        orientation: "vertical"
        padding : 20, 20
        size_hint_y: .5

        Button:
            on_press: root.show_calendar()
            Image:
                y: self.parent.y
                center_x: self.parent.center_x
                allow_stretch: True
                source: 'cross.png'

        TextInput:
            text:""

<DatePicker>:
    pHint: .3, .3

【问题讨论】:

    标签: python python-2.7 kivy kivy-language


    【解决方案1】:

    解决方案

    1. 在 kv 文件中,将 id: ti 添加到 TextInput 小部件。
    2. 在 Python 脚本中,创建一个自定义 DatePicker 类 并覆盖 update_value() 方法。

    详情请参考示例。

    示例

    main.py

    ​​>
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from KivyCalendar import DatePicker
    from kivy.core.window import Window
    
    Window.clearcolor = (0.5, 0.5, 0.5, 1)
    
    
    class CustomDatePicker(DatePicker):
    
        def update_value(self, inst):
            """ Update textinput value on popup close """
    
            self.text = "%s.%s.%s" % tuple(self.cal.active_date)
            self.focus = False
            App.get_running_app().root.ids.ti.text = self.text
    
    
    class Calendar(BoxLayout):
    
        def show_calendar(self):
            datePicker = CustomDatePicker()
            datePicker.show_popup(1, .3)
    
    
    class Test(App):
        def build(self):
            return Calendar()
    
    
    if __name__ == '__main__':
        Test().run()
    

    test.kv

    #:kivy 1.11.0
    
    <Calendar>:
        BoxLayout:
            orientation: "vertical"
            padding : 20, 20
            size_hint_y: .5
    
            Button:
                on_press: root.show_calendar()
                Image:
                    y: self.parent.y
                    center_x: self.parent.center_x
                    allow_stretch: True
                    source: 'cross.png'
    
            TextInput:
                id: ti
    
    <DatePicker>:
        pHint: .3, .3
    

    输出

    【讨论】:

    • 与您的输出不同,第 1 个月的尾随天数和第 1 个月的前导天数不会被停用。因此,如果选择的月份是 2019 年 1 月,则从 2018 年 12 月选择“31”会返回 2019 年 1 月 31 日。从 2019 年 2 月选择“1”会返回 2019 年 1 月 1 日。选择 2018 年 9 月 31 日不会返回任何内容。 Linux、Python 2.7.13、Kivy 1.10.1、KivyCalendar 0.1.3。读取文件“cross.png”时出错。如何平息这些不开心的日子?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多