乌龟与 Tkinter 发生冲突
Turtle clashing with Tkinter
我要归档:一个叫curve,一个叫main。主要是我试图打开一个按钮 window,然后每当按下按钮时。它开始使用 turtle 绘制曲线。这是简化的脚本:
主要内容:
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
由于 turtle 是使用 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()
我要归档:一个叫curve,一个叫main。主要是我试图打开一个按钮 window,然后每当按下按钮时。它开始使用 turtle 绘制曲线。这是简化的脚本:
主要内容:
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
由于 turtle 是使用 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()