0

I am using python-2.7 and kivy. When i click on cross.png then calendar open. When i click into calendar then how to get selected date? Actually i need to selected date put into TextInput. Can someone tell me that how to make it possible?

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
Nirdesh Kumawat
  • 386
  • 2
  • 16
  • 56

1 Answers1

1

Solution

  1. In kv file, add an id: ti to TextInput widget.
  2. In Python script, create a custom class of DatePicker and override update_value() method.

Please refer to example for details.

Example

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

Output

Img01 Img02

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Unlike your output, the trailing days of month-1 and leading days of month+1 are not inactivated. So, if selected month is january 2019, selecting '31' from dec 2018 returns 31 jan 2019. Selecting '1' from feb 2019 returns 1 jan 2019. Selecting 31 sept 2018 returns nothing. Linux, Python 2.7.13, Kivy 1.10.1, KivyCalendar 0.1.3. Error reading file 'cross.png'. How to inactivate these unhappy days ? – user3526918 Jan 22 '19 at 15:46