Python/Kivy : select 来自日历的日期并放入 TextInput
Python/Kivy : select date from calendar and put into TextInput
我正在使用 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
解决方案
- 在 kv 文件中,将
id: ti
添加到 TextInput 小部件。
- 在 Python 脚本中,创建 DatePicker 的自定义 class 并覆盖
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
输出
我正在使用 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
解决方案
- 在 kv 文件中,将
id: ti
添加到 TextInput 小部件。 - 在 Python 脚本中,创建 DatePicker 的自定义 class 并覆盖
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