Python tkinter实现的图片移动碰撞动画效果【附源码下载】

作者:chengqiuming 时间:2022-03-19 03:05:26 

本文实例讲述了Python tkinter实现的图片移动碰撞动画效果。分享给大家供大家参考,具体如下:

先来看看运行效果:

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程序设计有所帮助。

来源:http://blog.csdn.net/chengqiuming/article/details/78601062

标签:Python,tkinter
0
投稿

猜你喜欢

  • Microsoft Access项目不能压缩的原因

    2008-11-28 14:48:00
  • php关于array_multisort多维数组排序的使用说明

    2023-09-07 08:13:27
  • 解决PHP 7编译安装错误:cannot stat ‘phar.phar’: No such file or directory

    2023-09-05 06:07:44
  • JavaScript变量类型以及变量作用域详解

    2023-08-12 08:50:17
  • 透彻掌握ASP分页技术

    2009-03-09 18:26:00
  • 在ASP中使用SQL语句之9:表单操作

    2007-08-11 13:18:00
  • XML简易教程之四

    2008-09-05 17:19:00
  • CI操作cookie的方法分析(基于helper类库)

    2023-11-20 21:59:07
  • Python+Qt身体特征识别人数统计源码窗体程序(使用步骤)

    2021-06-03 10:40:54
  • 彻底弄懂CSS盒子模式之五(定位强化练习)

    2007-05-11 16:51:00
  • Python QQBot库的QQ聊天机器人

    2022-03-18 23:29:30
  • python处理文本文件实现生成指定格式文件的方法

    2022-01-07 21:41:43
  • Google Chrome的hack写法以及CSS的支持程度

    2008-09-04 12:28:00
  • 使用typescript快速开发一个cli的实现示例

    2023-08-30 07:25:25
  • 中国,美国,英国3国时间同步动态显示js代码

    2007-09-27 20:34:00
  • Python接口测试文件上传实例解析

    2023-04-19 15:59:28
  • uniapp页面跳转的五种方式总结

    2023-08-23 19:46:24
  • ASP表单验证方法总结

    2007-10-06 22:43:00
  • css学习笔记:div在IE6下无法遮盖select

    2009-04-30 13:21:00
  • ASP 多关键词查询实例代码

    2011-04-11 11:14:00
  • asp之家 网络编程 m.aspxhome.com