問題描述
下述代碼:
from tkinter import *
import random
import time
class SnakeHead:
def __init__(self,canvas,color):
self.canvas = canvas
self.id = canvas.create_oval(10,10,25,25,fill=color)
self.canvas.move(self.id,245,100)
self.x = -3
self.y = -3
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
self.curPos = self.canvas.coords(self.id)
self.prePos = None
def move_ball(self):
self.canvas.move(self.id,self.x,self.y)
pos = self.canvas.coords(self.id)
if self.curPos != pos:
self.prePos = self.curPos
self.curPos = pos
print("----------") #調試
print(self.curPos)
print(self.canvas.winfo_height())
print(self.prePos)
print("----------")
if self.curPos[0] <= 0:
self.x = 3
if self.curPos[2] >= self.canvas_width:
self.x = -3
if self.curPos[1] <= 0:
self.y = 3
if self.curPos[3] >= self.canvas_height:
self.y = -3
self.canvas.after(100,self.move_ball)
root = Tk()
root.title("Game")
root.resizable(0,0)
root.wm_attributes("-topmost",1)
canvas = Canvas(root,width=500,height=400,bd=0,highlightthickness=0)
canvas.pack()
#print("heigt:")
#print(canvas.winfo_height())
# test
sh = SnakeHead(canvas,'red')
sh.move_ball()
root.mainloop()
運行後,print(self.canvas.winfo_height())
前2次的循環返回值爲1,而不是實際的400,運行結果如下圖所示,
這是一個問題。若不解決,則小球的動畫演示不正確,其運動會越出邊界。
解決方案
通過網上查找資料,加上自己的思考,最終的問題出在Canvas
在創建後一定要及時對主窗口執行update
命令。具體來說:上述的canvas.pack()
後添加代碼:
root.update() # key code
這個問題,困擾了我斷斷續續2個晚上,加上我大量的思考,終於得以解決。實際上若不是動畫類的程序,不及時root.update()
也是可以的。