海龟与Tkinter碰撞
问题描述:
我必须将文件:一个叫曲线,另一个主要。主要我想打开一个按钮窗口,然后每当按钮按下。它开始使用乌龟在曲线上画东西。这是简化的脚本:海龟与Tkinter碰撞
主:
import tkinter
master = tkinter.Toplevel()
def callback():
print("click!")
master.withdraw()
b.quit()
import curve
b = tkinter.Button(master, text="OK", command=callback)
b.pack()
tkinter.mainloop()
曲线:
import turtle
turtle.bgpic("somefile.gif")
#do some other stuff
然而,当我运行此我得到这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:/Users/MYNAME/PycharmProjects/hilbert/main.py", line 7, in callback
import curve
File "C:\Users\MYNAME\PycharmProjects\hilbert\curve.py", line 3, in <module>
turtle.bgpic("images/processed.gif")
File "<string>", line 1, in bgpic
File "C:\Python34\lib\turtle.py", line 1474, in bgpic
self._setbgpic(self._bgpic, self._bgpics[picname])
File "C:\Python34\lib\turtle.py", line 737, in _setbgpic
self.cv.itemconfig(item, image=image)
File "<string>", line 1, in itemconfig
File "C:\Python34\lib\tkinter\__init__.py", line 2380, in itemconfigure
return self._configure(('itemconfigure', tagOrId), cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 1261, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
答
由于龟与实现tkinter,当你将两者混合在一起时,你正在走钢丝。你的代码的返工似乎做你的描述,包括bgpic()
电话:
main.py
import tkinter
import turtle
turtle.Screen()
root = tkinter.Toplevel()
def callback():
print("click!")
root.withdraw()
b.quit()
import curve
b = tkinter.Button(root, text="OK", command=callback)
b.pack()
tkinter.mainloop()
curve.py
import turtle
turtle.bgpic('somefile.gif')
# do some other stuff
turtle.circle(100)
turtle.mainloop()
我stll不知道如何这工作,但我设法让我自己的代码工作没有bgpic()。另外:我知道它应该更像'root = tkinter.Tk()' 'root.withdraw()' 'master = tkinter.Toplevel(root)' 'master.protocol(“WM_DELETE_WINDOW”,root。摧毁)'但这也行不通 –