Nirdesh Kumawat
Nirdesh Kumawat

Reputation: 406

Python/Kivy : select date from calendar and put into TextInput

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

Upvotes: 0

Views: 5001

Answers (1)

ikolim
ikolim

Reputation: 16041

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

Upvotes: 1

Related Questions