Ai
1 Star 0 Fork 0

我在吃大西瓜呢/Python课程学习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ex5_10.py 944 Bytes
一键复制 编辑 原始数据 按行查看 历史
我在吃大西瓜呢 提交于 2020-04-01 16:05 +08:00 . second commit
#__*__coding:utf-8__*__
#python3
import time
from tkinter import *
x=50
y=50
#(1)定义窗口
win=Tk()
win.title("移动小矩形块")
#(2)定义画布
canvas=Canvas(win,width=400,height=400)
canvas.pack()#显示画布
#(3)定义矩形块
rect=canvas.create_rectangle(x, y, x+30, y+30,fill='red')
print(rect)
#(4)定义移动小矩形的函数
def moveRect(event):
if event.keysym=='Up':
canvas.move(rect,0,-3) # 移动指定组件,使得横坐标加0,纵坐标减3
elif event.keysym=='Down':
canvas.move(rect,0,+3)
elif event.keysym=='Left':
canvas.move(rect,-3,0)
elif event.keysym=='Right':
canvas.move(rect,3,0)
win.update()
time.sleep(0.05)
#(5)绑定方向键
canvas.bind_all('<KeyPress-Up>',moveRect)
canvas.bind_all('<KeyPress-Down>',moveRect)
canvas.bind_all('<KeyPress-Left>',moveRect)
canvas.bind_all('<KeyPress-Right>',moveRect)
win.mainloop()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wang_ming_er/python_course_learning.git
git@gitee.com:wang_ming_er/python_course_learning.git
wang_ming_er
python_course_learning
Python课程学习
master

搜索帮助