耐心研究了好久,终于实现小球的自由移动,以及边框检测结果
from tkinter import *
from tkinter import messagebox #必须另外导入该模块messagebox
class MovingBall:
def __init__(self):
window=Tk()
window.title('移动小球')
BallImage=PhotoImage(file='c:/pythondata/2.gif') #小球的图片
self.canvas=Canvas(window, bg='white', width=400, height=400) #制作画布
self.canvas.pack()
self.canvas.create_image((200,200),image=BallImage,tags='image') #初始位置居中
self.x=200
self.y=200
frame=Frame(window)
frame.pack(padx=0,pady=0)
btLeft = Button(window, text='Left', command=self.Left)
btLeft.pack(side=LEFT)
btRight = Button(window, text='Rigth', command=self.Right)
btRight.pack(side=LEFT)
btUp = Button(window,