龟与Tkinter发生冲突

时间:2017-04-13 07:40:29

标签: tkinter python-3.4 turtle-graphics

我需要文件:一个叫做曲线,另一个叫做主。在主要我试图打开一个按钮窗口,然后按下按钮。它开始用乌龟在曲线上绘制一些东西。这是简化的脚本:

主:

 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

1 个答案:

答案 0 :(得分:0)

由于乌龟是用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()