简体   繁体   中英

Why won't my dot show up?

I am writing a turtle and Tkinter program to display a screen wide canvas to set one dot on which I will then put through a loop, can someone please read my code. Here it is with the error:

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"

I also get other errors complaining that "white" is a bad string in respect to t.dot. Please help.

There are two things to fix your code.

First, your drawing happen after mainloop (once you have exited your window), you can fix it my either drawing before the call of root.mainloop, either using a callback to be called during the mainloop.

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

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

Second, the diameter you ask for (1 pixel) makes your dot not visible...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM