Python代码编程基础第09课 创意时钟

知识要点

  1. datetime模块
  2. turtle模块中的函数
  3. 创意时钟设计
  4. 键盘事件的监听
  5. 创意时钟改进

一、PPT课件

二、知识要点

1.datetime模块

datetime是处理日期和时间的模块。

1
2
3
4
5
6
import datetime
# 获取当前时间
currentTime = datetime.datetime.now()
hour = currentTime.hour # 时
minute = currentTime.minute # 分
secent = currentTime.second # 秒

2.turtle模块中的函数

turtle.Turtle()函数

功能:创建一个新的海龟。

1
2
3
4
5
6
import turtle as t1
t1.right(90)
t1.forward(100)
t2 = t1.Turtle()
t2.color('red')
t2.forward(100)

turtle.tracer()函数

功能:开启或者关闭绘制过程动画,并设置动画播放速度。

  1. turtle.tracer(0)略过绘制过程。
  2. turtle.tracer()括号中的数字越大,绘制过程动画播放速度越快。

turtle.setheading()函数

功能:设置海龟的朝向,可简写成turtle.seth()。

1
2
3
4
turtle.seth(0)  # 朝右
turtle.seth(90) # 朝上
turtle.seth(180) # 朝下
turtle.seth(270) # 朝左

turtle.clear()函数

功能:清除海龟绘制的图案。

3.监听按键事件

onkeypress()

功能:把按键事件函数和某个按键绑定。

onkeypress(按键事件函数名,按键名),第一个参数是按键事件函数的名字,第二个参数是按键名,要加上引号。

listen()

功能:监听按键输入

不管调用多少次onkeypress()函数,只需要调用1次listen()函数就可以了。

1
2
3
4
5
6
7
8
9
10
11
import turtle as t1
def drawCircle():
# 定义画圆形函数
t1.clear()
t1.fillcolor('blue')
t1.begin_fill()
t1.circle(30)
t1.end_fill()
# 在小写输入状态下按下‘c’进行绘制圆形
t1.onkeypress(drawCircle,'c')
t1.listen()

三、代码示例

1.case1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
# 环节3 “创意时钟之效果优化”参考代码

import datetime
import turtle as t1


def colorGreen():
t1.color('green')


def colorBlue():
t1.color('blue')


def drawClock():
cur = datetime.datetime.now()
h = cur.hour
m = cur.minute
s = cur.second

t1.tracer(0)

# 秒针
t1.clear()
t1.penup()
t1.goto(0, 0)
t1.seth(90)
t1.right(6 * s)
t1.pendown()
t1.pensize(4)
t1.forward(100)

t1.tracer(1)


# 此处易错:监听按键事件的代码需要放在while True循环之前,
# 否则会因为一直在无限循环中而不被执行。
t1.onkeypress(colorGreen, 'g')
t1.onkeypress(colorBlue, 'b')
t1.listen()

while True:
drawClock()

t1.done()

2.case2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
# 时钟案例2
import datetime
import turtle as t1

t1.pensize(25)
t1.colormode(255)
t1.bgcolor(0, 0, 0)

t2 = t1.Turtle()
t2.pensize(25)

t3 = t1.Turtle()
t3.pensize(25)


def drawSquare(para, t):
for i in range(4):
t.forward(para)
t.right(90)


def drawClock():
cur = datetime.datetime.now()
h = cur.hour
m = cur.minute
s = cur.second
dh = (h % 12) * 6
dm = m * 4
ds = s * 4
print(ds, dm, dh)

# 画出分针
t1.tracer(0)
t1.hideturtle()
t1.color(200, 100, 0)
t1.pu()
t1.goto(-dm, dm)
t1.pd()
drawSquare(dm * 2, t1)

# 画出时针
t2.hideturtle()
t2.color(180, 80, 50)
t2.pu()
t2.goto(-dh, dh)
t2.pd()
drawSquare(dh * 2, t2)

# 画出秒针
t3.hideturtle()
t3.color(100, 60, 0)
t3.clear()
t3.pu()
t3.goto(-ds, ds)
t3.pd()
drawSquare(ds * 2, t3)

t1.tracer(1)


while True:
drawClock()

t1.done()

3.case3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
# 创意时钟案例3

import datetime
import turtle as t1

t1.colormode(255)
t1.bgcolor(171, 171, 171)

t2 = t1.Turtle()
t3 = t1.Turtle()


def drawRect(t, x, y, length, width):
t.hideturtle()
t.pu()
t.goto(x, y)
t.pd()
t.pensize(5)
for i in range(2):
t.forward(length)
t.right(90)
t.forward(width)
t.right(90)


def drawClock():
cur = datetime.datetime.now()
h = cur.hour
m = cur.minute
s = cur.second

dh = (h % 12) * 50
dm = m * 10
ds = s * 10
print(s, m, dh)

t1.tracer(0)

# 画出秒针
t1.clear()
t1.fillcolor(192, 255, 62)
t1.begin_fill()
drawRect(t1, 250, -300 + ds, 200, 50)
t1.end_fill()

# 画出分针
t2.clear()
t2.fillcolor(255, 127, 80)
t2.begin_fill()
drawRect(t2, -100, -300 + dm, 250, 50)
t2.end_fill()

# 画出时针
t3.clear()
t3.fillcolor(0, 178, 238)
t3.begin_fill()
drawRect(t3, -450, -300 + dh, 300, 50)
t3.end_fill()

t1.update()


while True:
drawClock()

t1.done()

4.case4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
# 创意时钟案例4

import datetime
import turtle as t1

t1.bgcolor('dark grey')
colorList = ['dark red', 'red', 'orange', 'gold', 'yellow', 'green', 'blue', 'dark blue', 'purple', 'black']
t2 = t1.clone()
t3 = t1.clone()
t4 = t1.clone()
t5 = t1.clone()
t6 = t1.clone()


def drawSquare(t, x, y, color):
t.clear()
t.penup()
t.seth(0)
t.goto(x, y)
t.pendown()
t.hideturtle()
t.pensize(5)
t.fillcolor(color)
t.begin_fill()
for i in range(4):
t.forward(80)
t.right(90)
t.end_fill()


def drawClock():
cur = datetime.datetime.now()
h = cur.hour
m = cur.minute
s = cur.second

t1.tracer(0)

# 秒针
drawSquare(t1, 120, 0, colorList[s // 10])
drawSquare(t2, 200, 0, colorList[s % 10])

# 分针
drawSquare(t3, -80, 0, colorList[m // 10])
drawSquare(t4, 0, 0, colorList[m % 10])

# 时针
drawSquare(t5, -280, 0, colorList[h // 10])
drawSquare(t6, -200, 0, colorList[h % 10])

t1.tracer(1)


while True:
drawClock()

t1.done()

5.case5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
# 创意时钟案例5

import datetime
import turtle as t1

t1.pensize(15)
t1.bgcolor('black')

t2 = t1.Turtle()
t3 = t1.Turtle()

secondColor = False


def colorSecond():
global secondColor
if secondColor == False:
t1.color('gold')
t1.pensize(15)
secondColor = True
else:
t1.color('black')
t1.pensize(20)
secondColor = False


minuteColor = False


def colorMinute():
global minuteColor
if minuteColor == False:
t2.color('medium purple')
t2.pensize(20)
minuteColor = True
else:
t2.color('black')
t2.pensize(30)
minuteColor = False


hourColor = False


def colorHour():
global hourColor
if hourColor == False:
t3.color('pale green')
t3.pensize(20)
hourColor = True
else:
t3.color('black')
t3.pensize(30)
hourColor = False


def drawClock():
# 获取系统时间
cur = datetime.datetime.now()
hour = cur.hour
minute = cur.minute
second = cur.second
h = (hour % 12) * 30
m = minute * 6
s = second * 6

# 绘制秒针
t1.hideturtle()
t1.pu()
t1.goto(0, 0)
t1.seth(0)
t1.pd()
t1.speed(40)

t1.hideturtle()
if s > 350:
t1.clear()
else:
t1.circle(-100, s)

# 绘制分针
t2.hideturtle()
t2.pu()
t2.goto(0, 50)
t2.seth(0)
t2.pd()
t2.speed(30)
if m > 350:
t2.clear()
else:
t2.circle(-160, m)

# 绘制时针
t3.hideturtle()
t3.pu()
t3.goto(0, 100)
t3.seth(0)
t3.pd()
t3.speed(30)
if h > 350:
t3.clear()
else:
t3.circle(-220, h)

# 按键交互


t1.onkeypress(colorSecond, 's')
t1.onkeypress(colorMinute, 'm')
t1.onkeypress(colorHour, 'h')
t1.listen()

while True:
drawClock()

t1.done()

6.homework

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import datetime
import turtle as t1

t1.pensize(25)
t1.color('purple')
t1.bgcolor('pink')

t2 = t1.Turtle()
t2.pensize(30)
t2.color('green')

t3 = t1.Turtle()
t3.pensize(45)
t3.color('red')

t4 = t1.Turtle()

t1.tracer(0)
t4.hideturtle()

def kedu():
i = 0
t4.clear()
t4.penup()
for w in range(60):
t4.penup()
t4.goto(0,0)
t4.seth(90)
t4.right(6 * w)
if w % 5 == 0:
t4.forward(160)
t4.pensize(9)
t4.pendown()
t4.forward(25)
else:
t4.forward(170)
t4.pensize(5)
t4.pendown()
t4.forward(15)
t4.penup()

def drawClock():
cur = datetime.datetime.now()
h = cur.hour
if h > 11:
h = h - 12
m = cur.minute
s = cur.second

t1.tracer(0)

# 秒针
t1.clear()
t1.penup()
t1.goto(0, 0)
t1.seth(90)
t1.right(6 * s)
t1.pendown()
t1.pensize(4)
t1.forward(150)

# 分针
t2.clear()
t2.penup()
t2.goto(0, 0)
t2.seth(90)
t2.right(6 * m)
t2.pendown()
t2.pensize(4)
t2.forward(130)

# 时针
t3.clear()
t3.penup()
t3.goto(0, 0)
t3.seth(90)
t3.right(30 * h + m//12*6)
t3.pendown()
t3.pensize(4)
t3.forward(100)

t4.goto(9,0)
t4.fillcolor('black')
t4.begin_fill()
t4.circle(9)
t4.end_fill()
t4.penup()

t1.tracer(1)

kedu()
while True:
drawClock()

t1.done()