使用canvas.create_window时,Tk Canvas上没有任何内容

时间:2014-03-26 17:43:07

标签: python button canvas tkinter window

我正在制作一个游戏,按钮放在我的主画布上。但是,无论何时绘制这些按钮,每个按钮都会创建另一个较小的空白画布。他们一直走在游戏前面,非常讨厌。

有没有人知道如何在我的画布上放置这些按钮,但是没有这些其他窗口(我认为它们可能是按钮通常会被绘制的默认窗口)出现?此外,这些窗口在关闭时会导致错误(如下)。

我不确定是什么导致它,我的代码长度超过1500行 - 所以我没有放入任何内容(除了我的一个按钮的代码)

btncont = Button(text = "Continue", command = Continue, anchor = W)
btncont.configure(width = 10, activebackground = "#33B5E5")
btncont_window =  canvas.create_window(300, 600, anchor=NW, window=btncont)
>>> Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/idlelib/run.py", line 121, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/queue.py", line 175, in get
    raise Empty
queue.Empty

>>>During handling of the above exception, another exception occurred:

>>>Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1475, in __call__
    return self.func(*args)
  File "/Users/Admin/Desktop/NewPomg/Pong - Revolutions [Version 6.0.1].py", line 1179, in TwoPlayer
    tk.update_idletasks()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 970, in update_idletasks
    self.tk.call('update', 'idletasks')
_tkinter.TclError: can't invoke "update" command:  application has been destroyed

1 个答案:

答案 0 :(得分:0)

如果您使用create_window,该按钮应该是画布的子项。例如:

btncont = Button(canvas, text = "Continue", command = Continue, anchor = W)

我怀疑是什么引起了堆栈跟踪,但它可能是问题的一部分。由于你正在使用队列,我猜你是使用线程,而tkinter不是线程安全的。因此,如果您正在使用线程,并且您尝试从多个线程在画布上绘制,那么这绝对是问题的一部分。

如果你正在写一些像乒乓球一样简单的东西,你就根本不需要线程。因此,第一步可能是删除线程并使用after来制作动画。本网站上有几个例子说明如何做到这一点。