本文实例讲述了Python tkinter实现的图片移动碰撞动画效果。分享给大家供大家参考,具体如下:
先来看看运行效果:
具体代码如下:
#!/usr/bin/python # -*- coding: utf-8 -*- import time try: from tkinter import * except ImportError: #Python 2.x PythonVersion = 2 from Tkinter import * from tkFont import Font from ttk import * from tkMessageBox import * import tkFileDialog else: #Python 3.x PythonVersion = 3 from tkinter.font import Font from tkinter.ttk import * from tkinter.messagebox import * # 配置 # 要打开的图像 image1 = "open.gif" # 初始坐标 x0 = 50.0 y0 = 50.0 # 列表将包含所有的x和y坐标.到目前为止,他们只包含初始坐标 x = [x0] y = [y0] # 每次移动的速度或距离 vx = 1.0# x 速度 vy = 0.5# y 速度 # 边界,这里要考虑到图片的大小,要预留一半的长和宽 x_min = 46.0 y_min = 46.0 x_max = 754.0 y_max = 554.0 # 图片间隔时间,要动画效果,此处设为每秒40帧 sleep_time = 0.025 # 运行步数 range_min = 1 range_max = 2000 # 创建500次的x和y坐标 for t in range(range_min, range_max): # 新坐标等于旧坐标加每次移动的距离 new_x = x[t - 1] + vx new_y = y[t - 1] + vy # 如果已经越过边界,反转方向 if new_x >= x_max or new_x <= x_min: vx = vx * -1.0 if new_y >= y_max or new_y <= y_min: vy = vy * -1.0 # 添加新的值到列表 x.append(new_x) y.append(new_y) # 开始使用tk绘图 root = Tk() root.title(" tkinter动画测试") #在这里修改窗口的标题 canvas = Canvas(width=800, height=600, bg='white') canvas.pack() photo1 = PhotoImage(file=image1) width1 = photo1.width() height1 = photo1.height() image_x = (width1) / 2.0 image_y = (height1) / 2.0 # 每次的移动 for t in range(range_min, range_max): canvas.create_image(x[t], y[t], image=photo1, tag="pic") canvas.update() # 暂停0.05妙,然后删除图像 time.sleep(sleep_time) canvas.delete("pic") root.mainloop()
附:完整实例代码点击此处本站下载。
注:tkinter只能识别gif格式图片,将jpg或png格式图片后缀名简单改成gif是不能识别的!
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]