[Tutor] click and line

Chris F.A. Johnson cfaj at freeshell.org
Mon Nov 7 06:27:23 CET 2005


On Sun, 6 Nov 2005, Shi Mu wrote:

> frame = c needs to be put before the click function or the lines can
> not be drawn.

     Not at all. I have everything after the click function and it works.

     This is my version (includes a small addition):

def click(event):
     global lastX, lastY
     if lastX != "":
         c.create_line(lastX,lastY,event.x,event.y,fill="white")
     lastX = event.x
     lastY = event.y

def newline(event):
     global lastX, lastY
     lastX=""
     lastY=""

from Tkinter import *

lastX=""
lastY=""
root = Tk()
c = Canvas(root, bg='#0e2e0e', height=500, width=1000)
frame = c
c.bind('<Button-1>',click)
c.bind('<Button-3>',newline)

c.pack()

root.mainloop()

> On 11/6/05, Chris F.A. Johnson <cfaj at freeshell.org> wrote:
>> On Sun, 6 Nov 2005, Shi Mu wrote:
>>
>>> based on the following rewritten code, why the lines still can not be
>>> drawn? (there is no error report and the canvas appears).
>>
>>     It works for me.
>>
>>
>>> from Tkinter import *
>>>
>>> root = Tk()
>>>
>>> c = Canvas(root, bg='#0e2e0e', height=500, width=1000)
>>>
>>> lastX=""
>>> lastY=""
>>> def click(event):
>>>  global lastX, lastY
>>>  if lastX != "":
>>>     c.create_line(lastX,lastY,event.x,event.y,fill="white")
>>>  lastX = event.x
>>>  lastY = event.y
>>>
>>> frame = c
>>> c.bind('<Button-1>',click)
>>>
>>> c.pack()
>>>
>>> root.mainloop()


-- 
     Chris F.A. Johnson                     <http://cfaj.freeshell.org>
     ==================================================================
     Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
     <http://www.torfree.net/~chris/books/cfaj/ssr.html>


More information about the Tutor mailing list