代码拉取完成,页面将自动刷新
#__*__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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。