Kivy使用篇之模拟时钟小程序
- 使用
canvas
来绘制widget
- 绑定
widget
的pos
和size
来重绘widget
- 使用
math
来计算正/余弦值 Color
Rectangle
Ellipse
Line
的使用
在数字时钟的基础上添加一个数字时钟面板
设置模拟时钟面板高度为总高度的70%
AnalogClock:
id: analog_clock_layout
size_hint: (1, .7)
使用kv语言来设置模拟时钟的背景和表盘
在python中定义一个AnalogClock
类
class AnalogClock(Widget):
"""模拟时钟控件"""
设置背景为白色,长方体
# 背景
Color:
rgb: (1, 1, 1)
Rectangle:
size: self.size
pos: self.pos
使用椭圆来定义一组线段
# 表盘
Color:
rgb: (0, 0, 0)
Line:
width: 6
ellipse: (self.center_x - min(self.width, self.height) / 2, \
self.center_y - min(self.width, self.height) / 2, \
min(self.width, self.height), \
min(self.width, self.height))
使用椭圆来绘制一个中心点
# 中心
Ellipse:
size: (6, 6)
pos: (self.center_x - 3, self.center_y - 3)
在python中绘制时钟的刻度和表针
构造函数如下:
def __init__(self, **kwargs):
"""绘制模拟时钟"""