简体   繁体   中英

Kivy : how to selected date from calendar put in TextBox?

How to use DatePicker based on TextInput ?When textinput field focused shows popup with CalendarWidget .Once data selected then selected date don't put into textinput field.

demo.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from KivyCalendar import CalendarWidget
from kivy.core.window import Window

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 250)

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

    def setDate(self):
        self.cal = CalendarWidget(as_popup=True)
        self.popup = Popup(title='Calendar', content=self.cal, size_hint=(1, 1))
        self.popup.open()


class Demo(App):
    def build(self):
        return SetIndex()


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

demo.kv

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

        BoxLayout:
            orientation: "horizontal"
            padding: 10, 10
            spacing: 10, 10
            size_hint_x: .6

            Label:
                text: "TEXT"
                text_size: self.size
                valign: 'middle'
                size_hint_x: .2

            TextInput:
                size_hint_x: .4
                id: old_date
                on_focus: root.setDate()

Which python version do you use?
KivyCalendar is a dated library written with 2.7 python version in mind, it does not work with 3.6 python at all.
Unless you are willing to use an unsupported, dated library, I would advice you to find an alternative.

UPD: Okay, so I could not find any alternatives, here is how this library work: You need to setup a DatePicker which is a textinput with all the necessary popup/value handling

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

        DatePicker:
            pHint: 1,1
            size_hint_x: .4
            id: old_date

You actual code only needs to import DatePicker, that is all there is to it

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)
Window.size = (400, 250)

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

class Demo(App):
    def build(self):
        return SetIndex()


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM