2015-12-29 33 views
1

當我嘗試運行我的程序時,出現一個奇怪的錯誤。它應該在TK()窗口上繪製一個紅色圓圈。爲什麼tkinter會給我一個「invalid command name .37395760」的錯誤?

這裏是我的代碼:

from tkinter import * 

class Circle: 
    def __init__(self, radius, points = 0, xcoordinate = 0, ycoordinate = 0):  
     self.radius = radius 
     self.points = points 
     self.color = "red" 
     self.xcoordinate = xcoordinate 
     self.ycoordinate = ycoordinate 

class World: 
    def __init__(self): 
     self.constructor = Tk() 
     self.constructor.title("Circle") 
     self.canvas = Canvas(self.constructor, width = 200, height = 200,  borderwidth = 0, highlightthickness = 0, bg = "black") 
     self.canvas.grid() 
     self.constructor.mainloop() 

    def drawPlayer(self): 
     player = Circle(50) 
     self.canvas.create_oval(player.xcoordinate - player.radius, player.ycoordinate - player.radius, player.xcoordinate + player.radius, player.ycoordinate + player.radius, fill = player.color) 

c = World() 
c.drawPlayer() 

我得到這個錯誤:

File "C:\Python34\Lib\tkinter\__init__.py", line 2318, in _create 
    *(args + self._options(cnf, kw)))) 
_tkinter.TclError: invalid command name ".37395760" 

我重讀,甚至寫下了我的代碼,看看我要去哪裏錯了,但我只是找不到錯誤。

注:我運行它後出現此錯誤,並且出現一個窗口,並顯示一個黑色的畫布,但沒有圓圈。

謝謝!

+0

我得到同樣的錯誤,你有你的代碼,但不同的浮動...我後,我得到的錯誤關閉窗戶,對你來說是一樣的嗎?還有,你跑步了嗎? ning python 2或3?我假設3,因爲我認爲tkinter是蟒蛇2中的大寫。 – Others

+0

我發現這個問題謝謝。是的,它是在窗戶關閉後 – Katie2423

回答

2

一旦主循環退出(self.constructor.mainloop(),窗口小部件不再存在。當你做self.canvas.create_oval(...)(其由c.drawPlayer()觸發),你要訪問已刪除的部件。

Tkinter的根本不是爲了讓你繪製完成之後的元素在主窗口已被破壞後訪問控件。

呼叫mainloop()

+0

謝謝!這很有幫助 – Katie2423

相關問題