Python100例 我的实现展示(56-60例)
from tkinter import *
import turtle
'''56、画图,学用circle画圆形。'''
def test_exam_56():
x1, y1 = 100, 100
x2, y2 = 100, -100
x3, y3 = -100, -100
x4, y4 = -100, 100
# 绘制折线
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
turtle.circle(10)
turtle.goto(x2, y2)
turtle.circle(20)
turtle.goto(x3, y3)
turtle.circle(30)
turtle.goto(x4, y4)
turtle.circle(50)
turtle.done()
'''57、画图,学用line画直线。'''
def test_exam_57():
x1, y1 = 100, 100
x2, y2 = 100, -100
x3, y3 = -100, -100
x4, y4 = -100, 100
# 绘制折线
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
turtle.goto(x2, y2)
turtle.goto(x3, y3)
turtle.goto(x4, y4)
turtle.done()
'''58、画图,学用rectangle画方形。(这题不会,模仿学习的代码)'''
def test_exam_58():
tk = Tk()
tk.title('rectangle')
canvas = Canvas(tk, width=400, height=400, bg='red')
x0 = 263
y0 = 263
x1 = 275
y1 = 275
for i in range(19):
canvas.create_rectangle(x0, y0, x1, y1)
x0 -= 5
y0 -= 5
x1 += 5
y1 += 5
canvas.pack()
tk.mainloop()
'''59、画图,综合例子。(程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。)'''
def test_exam_59():
canvas = Canvas(width=300, height=300, bg='green')
canvas.pack(expand=YES, fill=BOTH)
x0 = 150
y0 = 100
canvas.create_oval(x0 - 10, y0 - 10, x0 + 10, y0 + 10)
canvas.create_oval(x0 - 20, y0 - 20, x0 + 20, y0 + 20)
canvas.create_oval(x0 - 50, y0 - 50, x0 + 50, y0 + 50)
b = 0.809
for i in range(16):
a = 2 * math.pi / 16 * i
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * b)
canvas.create_line(x0, y0, x, y, fill='red')
canvas.create_oval(x0 - 60, y0 - 60, x0 + 60, y0 + 60)
for k in range(501):
for i in range(17):
a = 2 * math.pi / 16 * i + (2 * math.pi / 180) * k
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * b)
canvas.create_line(x0, y0, x, y, fill='red')
for j in range(17):
a = 2 * math.pi / 16 * j + (2 * math.pi / 180) * k
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * b)
canvas.create_line(x0, y0, x, y, fill='red')
mainloop()
'''60、计算字符串长度。'''
def test_exam_60():
s = input("请输入一行字符串,程序将计算并输出该字符串的长度\n")
print("输入字符串的长度为:{0}".format(len(s)))
if __name__ == '__main__':
# test_exam_56()
# test_exam_57()
# test_exam_58()
# test_exam_59()
test_exam_60()
test_exam_56跑出来的结果如下图:
test_exam_57跑出来的结果如下图:
test_exam_58跑出来的结果如下图:
test_exam_59跑出来的结果如下图: