在Kivy中显示并在Raspberry PI上运行时,我得到一个非常奇怪的结果。
每个BoxLayout
都会显示两次,当我更改标签的文本时,
单词似乎重叠。我对为什么出现这些结果感到非常困惑。
如何解决此问题?
BoxLayout显示两次
当我更改标签时,标签的文本重叠
同时更改上方和下方的文字
这是我的Python代码:
class ScheduleScreen(Screen):
def __init__(self, **kwargs):
super(ScheduleScreen,self).__init__(**kwargs)
def build(self):
pass
def on_quit(self):
global sm, event_idle
sm.current = 'Normal'
event_idle = Clock.schedule_once(BackMain, 60)
class ScheduleListItemButton(ListItemButton):
def __init__(self, **kwargs):
super(ScheduleListItemButton, self).__init__(**kwargs)
self.height = "64dp"
class ScheduleMan(BoxLayout):
week_day = StringProperty()
week_day_up = Image()
week_day_down = Image()
start_hour = StringProperty()
start_hour_up = Image()
start_hour_down = Image()
start_min = StringProperty()
start_min_up = Image()
start_min_down = Image()
end_hour = StringProperty()
end_hour_up = Image()
end_hour_down = Image()
end_min = StringProperty()
end_min_up = Image()
end_min_down = Image()
week_day_num = 0
start_hour_num = 0
start_min_num = 0
end_hour_num = 0
end_min_num = 0
schedule_list = ObjectProperty()
def __init__(self, **kwargs):
super(ScheduleMan, self).__init__(**kwargs)
self.week_days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
self.init_value()
self.schedules = []
# Initial all properties
def init_value(self):
self.week_day = "星期日"
self.start_hour = "00"
self.start_min = "00"
self.end_hour = "00"
self.end_min = "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):
if self.week_day_num == 6:
self.week_day_num = 0
else:
self.week_day_num += 1
self.week_day = self.week_days[self.week_day_num]
def down_week_day(self):
if self.week_day_num == 0:
self.week_day_num = 6
else:
self.week_day_num -= 1
self.week_day = self.week_days[self.week_day_num]
def up_start_hour(self):
if self.start_hour_num >= 23:
self.start_hour_num = 0
else:
self.start_hour_num += 1
self.start_hour = "%02d" % self.start_hour_num
def down_start_hour(self):
if self.end_hour_num <= 0:
self.end_hour_num = 23
else:
self.start_hour_num -= 1
self.start_hour = "%02d" % self.start_hour_num
def up_end_hour(self):
if self.end_hour_num >= 23:
self.end_hour_num = 0
else:
self.end_hour_num += 1
self.end_hour = "%02d" % self.end_hour_num
def down_end_hour(self):
if self.end_hour_num <= 0:
self.end_hour_num = 23
else:
self.end_hour_num -= 1
self.end_hour = "%02d" % self.end_hour_num
def up_start_min(self):
if self.start_min_num >= 59:
self.start_min_num = 0
else:
self.start_min_num += 1
self.start_min = "%02d" % self.start_min_num
def down_start_min(self):
if self.end_min_num <= 0:
self.end_min_num = 59
else:
self.start_min_num -= 1
self.start_min = "%02d" % self.start_min_num
def up_end_min(self):
if self.end_min_num >= 59:
self.end_min_num = 0
else:
self.end_min_num += 1
self.end_min = "%02d" % self.end_min_num
def down_end_min(self):
if self.end_min_num <= 0:
self.end_min_num = 59
else:
self.end_min_num -= 1
self.end_min = "%02d" % self.end_min_num
这是我的kv文件:
<ScheduleScreen>:
name: "Schedule"
ScheduleMan:
FloatLayout:
Button:
id: buttonQuit
size_hint: (0.2, 0.1)
pos_hint: {'right':1,'bottom':0.9}
font_name: 'DroidSansFallback'
text: "離開"
on_press: root.on_quit()
<ScheduleListItemButton>:
font_name: 'DroidSansFallback'
font_size: 32
size_hint_y: None
height: "64dp"
<ScheduleMan>:
orientation: "vertical"
schedule_list: schedules_list_view
padding: 20
spacing: 20
BoxLayout:
orientation: "horizontal"
size_hint_y: None
height: "64dp"
spacing: 2
Label:
id: l_week_day
font_name: 'DroidSansFallback'
font_size: 32
text: root.week_day
BoxLayout:
orientation: "vertical"
size_hint_y: None
padding: 2
spacing: 15
ImageButton:
source: "../pic/up.png"
on_press: root.up_week_day()
size_hint_y: None
width: 24
height: 16
ImageButton:
source: "../pic/down.png"
on_press: root.down_week_day()
size_hint_y: None
width: 24
height: 16
Label:
id: l_start_hour
font_name: 'DroidSansFallback'
font_size: 32
text: root.start_hour
BoxLayout:
orientation: "vertical"
size_hint_y: None
padding: 2
spacing: 15
ImageButton:
source: "../pic/up.png"
on_press: root.up_start_hour()
size_hint_y: None
width: 24
height: 16
ImageButton:
source: "../pic/down.png"
on_press: root.down_start_hour()
size_hint_y: None
width: 24
height: 16
Label:
text: ":"
font_name: 'DroidSansFallback'
font_size: 32
Label:
id: l_start_min
font_name: 'DroidSansFallback'
font_size: 32
text: root.start_min
spacing: 5
BoxLayout:
orientation: "vertical"
size_hint_y: None
padding: 2
spacing: 15
ImageButton:
source: "../pic/up.png"
on_press: root.up_start_min()
size_hint_y: None
width: 24
height: 16
ImageButton:
source: "../pic/down.png"
on_press: root.down_start_min()
size_hint_y: None
width: 24
height: 16
Label:
text: " 至 "
font_name: 'DroidSansFallback'
font_size: 32
Label:
id: l_end_hour
font_name: 'DroidSansFallback'
font_size: 32
text: root.end_hour
BoxLayout:
orientation: "vertical"
size_hint_y: None
padding: 2
spacing: 15
ImageButton:
source: "../pic/up.png"
on_press: root.up_end_hour()
size_hint_y: None
width: 24
height: 16
ImageButton:
source: "../pic/down.png"
on_press: root.down_end_hour()
size_hint_y: None
width: 24
height: 16
Label:
text: ":"
font_name: 'DroidSansFallback'
font_size: 32
Label:
id: l_end_min
font_name: 'DroidSansFallback'
font_size: 32
text: root.end_min
BoxLayout:
orientation: "vertical"
size_hint_y: None
padding: 2
spacing: 15
ImageButton:
source: "../pic/up.png"
on_press: root.up_end_min()
size_hint_y: None
width: 24
height: 16
ImageButton:
source: "../pic/down.png"
on_press: root.down_end_min()
size_hint_y: None
width: 24
height: 16
BoxLayout:
size_hint_y: None
height: "64dp"
spacing: 15
spacing: 5
Button:
text: "儲存修改"
size_hint_x: 32
font_name: 'DroidSansFallback'
font_size: 32
on_press: root.submit_schedule()
Button:
text: "剛除"
size_hint_x: 32
font_name: 'DroidSansFallback'
font_size: 32
on_press: root.delete_schedule()
Button:
text: "修改"
size_hint_x: 32
font_name: 'DroidSansFallback'
font_size: 32
on_press: root.update_schedule()
ListView:
id: schedules_list_view
adapter:
ListAdapter(data=[], cls=main.ScheduleListItemButton)
答案 0 :(得分:0)
这是我可行的版本。
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: "離開"