这是风火轮编程Python初级教程的第22课,下面是教学演示中的一张图片。
用Python制作的幻灯片类型的作品。本课主要结合几何知识,推导了正多边形内角和公式。
以下是完整源代码。(并非本课程例子程序的代码,这是用于演示教学的程序)
本程序需要sprites模块支持,安装方法为在命令提示符下输入以下命令安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sprites –upgrade
"""
第二十二课 正多边形的秘密
"""
from sprites import *
s = '第二十二课 正多边形的秘密'
screen = Screen()
screen.bgcolor('green')
screen.titlebar(False)
root = screen._root # 窗口对象
root.wm_attributes('-alpha',0.7) # 设置窗口为全透明(0到1.0)
screen.setup(1000,800)
screen.tracer(0,0)
# 下面的代码让窗口可以拖动.
oldx = 0
oldy = 0
def startmove(event):
global oldx,oldy
oldx = event.x
oldy = event.y
def stopmove(event):
global oldx,oldy
oldx = 0
oldy = 0
def movewindow(event):
global oldx,oldy
dx = event.x - oldx
dy = event.y - oldy
root.move(dx,dy)
screen.cv.bind("", startmove)
screen.cv.bind("", stopmove)
screen.cv.bind("",movewindow)
# 下面的代码按方向箭头则窗口能上下左右移动
screen.onkeypress(lambda:root.move(10),'Right')
screen.onkeypress(lambda:root.move(-10),'Left')
screen.onkeypress(lambda:root.move(0,-10),'Up')
screen.onkeypress(lambda:root.move(0,10),'Down')
screen.listen()
# 下面的TK是就是tkinter
popup = TK.Menu(screen._root, tearoff=0)
popup.add_command(label="关于本程序",command=lambda:showinfo('关于','本程序由李兴球开发\n\nQQ:406273900\n\nwww.lixingqiu.com'))
popup.add_command(label="打开主页",command=lambda:os.system('explorer https://www.lixingqiu.com'))
popup.add_command(label="退出本程序",command=lambda:screen.bye())
def do_popup(event):
"""显示弹出菜单"""
try:
popup.tk_popup(event.x_root, event.y_root + 10, 0)
finally:
# 确保释放按键
popup.grab_release()
screen.cv.bind("", do_popup) # 画布绑定鼠标右键
# 单击鼠标左键开始
spacekey = Key('space') # 空格键
m1 = Mouse() # 鼠标左键
while not m1.down():screen.update()
ft_title = ('微软雅黑',30,'bold')
ft_context = ('宋体',15,'normal')
t = Sprite(visible=False,pos=(-500,300))
t.color('yellow')
clock = Clock()
for x in range(100):
t.clear()
t.write(s,align='center',font=ft_title)
t.wait()
t.fd(5)
clock.tick(60)
while not spacekey.down():screen.update()
# 简介
brief ="""
本课主要学习旋转一圈是360度这个基本常识,
还要学习正多形内角和公式是怎么来的。
"""
if brief!='': # 如果有课程简介,则生成一个角色,
ftx = ('宋体',17,'normal')
简介 = Sprite(visible=False,pos=(0,-300))
简介.color('white')
简介.write(brief,align='center',font=ftx)
while spacekey.down():screen.update()
while not spacekey.down():screen.update()
for x in range(66): # 标题向右消失
t.clear()
t.write(s,align='center',font=ft_title)
t.fd(10)
if brief!='':
简介.clear()
简介.write(brief,align='center',font=ftx)
简介.bk(10)
clock.tick(60)
简介.clear()
byebye = """
下次再见!
"""
t.clear()
t.color('cyan')
t.home()
t.write(byebye,align='center',font=('宋体',38,'bold'))
screen.mainloop()
以下是付费内容,包括播放视频、python源代码及幻灯片与素材等等。
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

