我希望程序将按钮中存储的时间与当前时间进行比较。当我运行程序时,如果出现“早期”按钮,则按钮应更改为黄色;如果“按时”按钮,则应更改为绿色;如果是“延迟”,则按钮应更改为红色:
honey = float(self.streak_button.id)
AttributeError: 'MainApp' object has no attribute 'streak_button'
这是我的代码:
class MainApp(App):
def build(self): # build() returns an instance
self.store = JsonStore("streak.json") # file that stores the streaks:
Clock.schedule_interval(self.check_streak, 1/30.)
return presentation
def check_streak(self, dt):
honey = float(self.streak_button.id)
#print(f"\tdelta={honey}")
# used to give 30 minute grace period
delay = honey + 1800
check = False
if honey > time.time() and honey < delay:
check = True
if honey > time.time():
self.streak_button.color = 0,1,0
if check == True:
self.streak_button.color = 0,0,1
if honey < time.time():
self.streak_button.color = 1,0,0
def display_btn(self):
# display the names of the streaks in a list on PageTwo
with open("streak.json", "r") as read_file:
data = json.load(read_file)
for value in data.values():
if value['delta'] is not None:
print(f"action={value['action']}, delta={value['delta']}")
self.streak_button = StreakButton(id=str(value['delta']), text=value['action'],
on_press=self.third_screen)
self.root.screen_two.ids.streak_zone.add_widget(self.streak_button)
答案 0 :(得分:0)
使用以下命令更改Button的颜色:
background_normal = ''
#设置纯色background_color = [1, 0, 0, 1]
#背景颜色,格式为(r,g,b,a)background_color
背景色,格式为(r,g,b,a)。
这是纹理颜色的乘数。默认纹理 是灰色的,因此只需设置背景颜色即可使颜色更深 结果。要设置纯色,请将background_normal设置为”。
background_color是ListProperty,默认为[1, 1,1,1]。
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.button import Button
from kivy.storage.jsonstore import JsonStore
from kivy.lang import Builder
from kivy.clock import Clock
import time
import json
class ScreenTwo(Screen):
pass
Builder.load_string("""
<ScreenTwo>:
GridLayout:
id: streak_zone
cols: 2
""")
class StreakButton(Button):
# rgba = ListProperty([])
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
print(f"\nCustomButton.on_touch_down: text={self.text}")
self.dispatch('on_press')
return True
return super(StreakButton, self).on_touch_down(touch)
class TestApp(App):
def build(self):
self.store = JsonStore("streak.json")
Clock.schedule_interval(self.check_streak, 1/30.)
return ScreenTwo()
def check_streak(self, dt):
print(f"\ncheck_streak:")
# check all buttons
for child in reversed(self.root.ids.streak_zone.children):
honey = float(child.id)
print(f"\tdelta={honey}")
# used to give 30 minute grace period
delay = honey + 1800
check = False
if honey > time.time() and honey < delay:
check = True
if honey > time.time():
print("\t\tearly")
child.background_normal = ''
child.background_color = [1, 1, 0, 1] # Yellow colour
elif check == True:
print("\t\ton time")
child.background_normal = ''
child.background_color = [0, 1, 0, 1] # Green colour
elif honey < time.time():
print("\t\tlate")
child.background_normal = ''
child.background_color = [1, 0, 0, 1] # Red colour
def display_btn(self):
print(f"\ndisplay_btn:")
with open("streak.json", "r") as read_file:
data = json.load(read_file)
for value in data.values():
if value['delta'] is not None:
print(f"action={value['action']}, delta={value['delta']}")
streak_button = StreakButton(id=str(value['delta']), text=value['action'], on_press=self.third_screen)
self.root.ids.streak_zone.add_widget(streak_button)
if __name__ == '__main__':
TestApp().run()