Вот моя работоспособная версия.
test5.py
import os
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty, ListProperty
from kivy.uix.listview import ListItemButton
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.screenmanager import Screen, ScreenManager
import simplejson as json
from kivy.uix.popup import Popup
strnow = lambda : str(datetime.now())
# init
base_path = os.getcwd()
pic_path = base_path + '/pic/'
conf_path = base_path + '/conf/'
kivy.resources.resource_add_path(base_path + '/font')
class ScheduleListButton(ListItemButton):
def __init__(self,**kwargs):
super(ScheduleListButton, self).__init__(**kwargs)
self.font_name = 'DroidSansFallback'
self.height = 40
pass
class ImageButton(ButtonBehavior, Image):
pass
class ScheduleDB(BoxLayout):
# Connects the value in the TextInput widget to these
# fields
# first_name_text_input = ObjectProperty()
# last_name_text_input = ObjectProperty()
schedule_list = ObjectProperty()
week_day_text = StringProperty()
start_hour_text = StringProperty()
start_min_text = StringProperty()
end_hour_text = StringProperty()
end_min_text = StringProperty()
scheduleTexts = ListProperty()
week_day_num = 0
start_hour_num = 0
start_min_num = 0
end_hour_num = 0
end_min_num = 0
def __init__(self, **kwargs):
super(ScheduleDB, self).__init__(**kwargs)
self.week_days = ["星期一 ", "星期二 ", "星期三 ", "星期四 ", "星期五 ", "星期六", "星期日 " ]
self.init_value()
self.schedules = []
with open(conf_path + "rule.json", "r") as fp:
self.schedules = json.load(fp)
print("Load from config : %s " % str(self.schedules))
self.scheduleTexts = []
for s in self.schedules:
self.scheduleTexts.append("%s%s至%s" % (self.week_days[s["weekDay"]], s["beginTime"], s["endTime"]))
#self.ids.schedule_list_view.adapter.data = self.scheduleTexts
# Initial all properties
def init_value(self):
self.week_day_text = "星期一 "
self.start_hour_text = "00"
self.start_min_text = "00"
self.end_hour_text = "00"
self.end_min_text = "00"
self.week_day_num = 0
self.start_hour_num = 0
self.start_min_num = 0
self.end_hour_num = 0
self.end_min_num = 0
def up_week_day(self):
# print("up_week_day, %d" % self.week_day_num)
if self.week_day_num == 6:
self.week_day_num = 0
else:
self.week_day_num += 1
self.week_day_text = self.week_days[self.week_day_num]
def down_week_day(self):
# print("down_week_day, %d" % self.week_day_num)
if self.week_day_num == 0:
self.week_day_num = 6
else:
self.week_day_num -= 1
self.week_day_text = self.week_days[self.week_day_num]
def up_start_hour(self):
# print("up_start_hour, %d" % self.start_hour_num)
if self.start_hour_num >= 23:
self.start_hour_num = 0
else:
self.start_hour_num += 1
self.start_hour_text = "%02d" % self.start_hour_num
def down_start_hour(self):
# print("down_start_hour, %d" % self.start_hour_num)
if self.start_hour_num <= 0:
self.start_hour_num = 23
else:
self.start_hour_num -= 1
self.start_hour_text = "%02d" % self.start_hour_num
def up_start_min(self):
# print("up_start_min, %d" % self.start_min_num)
if self.start_min_num >= 59:
self.start_min_num = 0
else:
self.start_min_num += 1
self.start_min_text = "%02d" % self.start_min_num
def down_start_min(self):
# print("down_start_min, %d" % self.start_min_num)
if self.start_min_num <= 0:
self.start_min_num = 59
else:
self.start_min_num -= 1
self.start_min_text = "%02d" % self.start_min_num
def up_end_hour(self):
# print("up_end_hour, %d" % self.end_hour_num)
if self.end_hour_num >= 23:
self.end_hour_num = 0
else:
self.end_hour_num += 1
self.end_hour_text = "%02d" % self.end_hour_num
def down_end_hour(self):
# print("down_end_hour, %d" % self.end_hour_num)
if self.end_hour_num <= 0:
self.end_hour_num = 23
else:
self.end_hour_num -= 1
self.end_hour_text = "%02d" % self.end_hour_num
def up_end_min(self):
# print("up_end_min, %d" % self.end_min_num)
if self.end_min_num >= 59:
self.end_min_num = 0
else:
self.end_min_num += 1
self.end_min_text = "%02d" % self.end_min_num
def down_end_min(self):
# print("down_end_min, %d" % self.end_min_num)
if self.end_min_num <= 0:
self.end_min_num = 59
else:
self.end_min_num -= 1
self.end_min_text = "%02d" % self.end_min_num
class ScheduleScreen(Screen):
def __init__(self, **kwargs):
super(ScheduleScreen, self).__init__(**kwargs)
def build(self):
pass
class test5App(App):
def build(self):
return ScheduleScreen()
#return ScheduleDB()
dbApp = test5App()
dbApp.run()
test5.kv
# Reference test5.py
#: import main test5
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton
ScheduleDB:
<ScheduleDB>:
orientation: "vertical"
#first_name_text_input: first_name
#last_name_text_input: last_name
schedule_list: schedule_list_view
padding: 2
spacing: 2
BoxLayout:
orientation: "horizontal"
size_hint_y: None
height: "64dp"
BoxLayout:
orientation: "horizontal"
padding: 2
spacing: 2
size_hint_x: 0.4
canvas:
Color:
rgba: 1, 1, 1, .5
BoxLayout:
orientation: "vertical"
ImageButton:
id: UpWeekDay
source: "pic/up.png"
size_hint_y: None
height: 30
on_press: root.up_week_day()
ImageButton:
id: DownWeekDay
source: "pic/down.png"
size_hint_y: None
height: 30
on_press: root.down_week_day()
Label:
id: week_day
font_name: 'DroidSansFallback'
font_size: 32
text: root.week_day_text
BoxLayout:
orientation: "horizontal"
padding: 2
spacing: 2
BoxLayout:
size_hint_x: 1.8
canvas:
Color:
rgba: 1, 1, 1, .5
Label:
id: start_hour
font_name: 'DroidSansFallback'
font_size: 32
text: root.start_hour_text
BoxLayout:
orientation: "vertical"
ImageButton:
id: UpStartHour
source: "pic/up.png"
size_hint_y: None
height: 30
on_press: root.up_start_hour()
ImageButton:
id: DownStartHour
source: "pic/down.png"
size_hint_y: None
height: 30
on_press: root.down_start_hour()
Label:
id: l1
font_name: 'DroidSansFallback'
font_size: 32
text: ":"
Label:
id: start_min
font_name: 'DroidSansFallback'
font_size: 32
text: root.start_min_text
BoxLayout:
orientation: "vertical"
ImageButton:
id: UpStartMin
source: "pic/up.png"
size_hint_y: None
height: 30
on_press: root.up_start_min()
ImageButton:
id: DownStartMin
source: "pic/down.png"
size_hint_y: None
height: 30
on_press: root.down_start_min()
Label:
id: till
font_name: 'DroidSansFallback'
font_size: 32
size_hint_x: 0.6
text: "至"
BoxLayout:
size_hint_x: 1.8
canvas:
Color:
rgba: 1, 1, 1, .5
Label:
id: end_hour
font_name: 'DroidSansFallback'
font_size: 32
text: root.end_hour_text
BoxLayout:
orientation: "vertical"
ImageButton:
id: UpEndHour
source: "pic/up.png"
size_hint_y: None
height: 30
on_press: root.up_end_hour()
ImageButton:
id: DownEndHour
source: "pic/down.png"
size_hint_y: None
height: 30
on_press: root.down_end_hour()
Label:
id: l2
font_name: 'DroidSansFallback'
font_size: 32
text: ":"
Label:
id: end_min
font_name: 'DroidSansFallback'
font_size: 32
text: root.end_min_text
BoxLayout:
orientation: "vertical"
ImageButton:
id: UpEndtMin
source: "pic/up.png"
size_hint_y: None
height: 30
on_press: root.up_end_min()
ImageButton:
id: DownEndMin
source: "pic/down.png"
size_hint_y: None
height: 30
on_press: root.down_end_min()
BoxLayout:
size_hint_y: None
height: "48dp"
Button:
font_name: 'DroidSansFallback'
font_size: 32
text: "新增"
size_hint_x: 15
on_press: root.submit_schedule()
Button:
font_name: 'DroidSansFallback'
font_size: 32
text: "刪除"
size_hint_x: 15
on_press: root.delete_schedule()
Button:
font_name: 'DroidSansFallback'
font_size: 32
text: "更改"
size_hint_x: 15
on_press: root.replace_schedule()
# Define starting data and point to the ListItemButton
# in the Python code
ListView:
id: schedule_list_view
adapter:
ListAdapter(data=root.scheduleTexts, cls=main.ScheduleListButton)
<ScheduleScreen>:
name: "Schedule"
ScheduleDB:
FloatLayout:
Button:
id: buttonQuit
size_hint: (0.2, 0.1)
pos_hint: {'right':1,'top':0.95}
font_name: 'DroidSansFallback'
text: "離開"