为什么我的点不会出现?

时间:2014-02-09 18:09:53

标签: python tkinter turtle-graphics

我正在编写一个乌龟和Tkinter程序,以显示一个屏幕宽的画布,设置一个点,然后我将通过循环,有人可以请阅读我的代码。这是错误:

import turtle
import Tkinter

root = Tkinter.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
cv = Tkinter.Canvas(root,width=screen_width,height=screen_height)
t = turtle.RawTurtle(cv)
screen = t.getscreen()
frame = Tkinter.Frame(root)

def main():
   root.title("Scanner")
   cv.pack()
   screen.setworldcoordinates(0,0,screen_width,screen_height)
   screen.bgcolor("black")
   frame.pack()
   Tkinter.mainloop()

def setDefaultPos():
    t.penup()
    t.goto(80,80)


if __name__ == "__main__":
   main()
   setDefaultPos()
   t.dot(1, "white")

Traceback (most recent call last):
  File "./demo.py", line 27, in <module>
    setDefaultPos()
  File "./demo.py", line 21, in setDefaultPos
    t.penup()
  File "/usr/lib/python2.7/lib-tk/turtle.py", line 2021, in penup
    self.pen(pendown=False)
  File "/usr/lib/python2.7/lib-tk/turtle.py", line 2337, in pen
    self._newLine()
  File "/usr/lib/python2.7/lib-tk/turtle.py", line 3123, in _newLine
    self.screen._drawline(self.currentLineItem, top=True)
  File "/usr/lib/python2.7/lib-tk/turtle.py", line 575, in _drawline
    self.cv.tag_raise(lineitem)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2314, in tag_raise
    self.tk.call((self._w, 'raise') + args)
_tkinter.TclError: invalid command name ".3072604172L"

我还得到其他错误抱怨“白色”是关于t.dot的错误字符串。请帮忙。

1 个答案:

答案 0 :(得分:1)

修复代码有两件事。

首先,您的绘图发生在mainloop之后(一旦退出窗口),您可以在调用root.mainloop之前修复它,或者使用在mainloop期间调用的回调。

def callback():
  setDefaultPos()
  t.dot(1, "white")

#(...)
root.after(1, callback) #call 'callback' function after 1 millisecond
Tkinter.mainloop()

其次,你要求的直径(1个像素)使你的点不可见......