我正在尝试创建一个带有 3 个弧的时钟,一个用于小时、分钟和秒。该应用程序当前绘制每条弧线,按预期使用不同的颜色和半径。弧线还根据应用程序启动的时间填充。红色圆弧代表小时,绿色圆弧代表分钟,蓝色圆弧代表秒。从这里开始,我试图更新画布,以便弧线根据时间变化(每秒一次)。我还想要一个在时钟中心显示时间的标签。谁能帮我解决这个问题?我对 Kivy 比较陌生,所以我不确定我应该做什么。
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
from datetime import datetime
# update canvas every second
# display time in center of clock
class drawClock(FloatLayout):
def __init__(self, **kwargs):
super(drawClock, self).__init__(**kwargs)
self.bind(pos=self.updateClock)
self.bind(size=self.updateClock)
def updateClock(self, *kwargs):
with self.canvas:
now = datetime.now()
hour = now.hour
minute = now.minute
second = now.second
hourAngle = (hour%12) * 30
minuteAngle = minute * 6
secondAngle = second * 6
Line(circle = (self.center_x, self.center_y, 80, 0, hourAngle), width = 2, color = Color(1,0,0))
Line(circle = (self.center_x, self.center_y, 65, 0, minuteAngle), width = 2, color = Color(0,1,0))
Line(circle = (self.center_x, self.center_y, 50, 0, secondAngle), width = 2, color = Color(0,0,1))
class mainApp(App):
def build(self):
return drawClock()
if __name__ == '__main__':
mainApp().run()
函数式编程
慕妹3242003
相关分类