drawline
Eugene Druker
ledruker at rogers.com
Tue Nov 22 21:23:56 EST 2005
Hi Ben,
if I understood your questions properly, this code gives some answers (on
XP):
from Tkinter import *
lines = [
[ (100, 200, 350, 200), LAST, "red", '' ],
[ (100, 0, 100, 200), FIRST, "green", 'a' ],
[ (100, 200, 300, 100), LAST, "purple", 'b' ],
]
ClickMax = len(lines)
ClickNum = 0
lasttag = ''
def drawline(event):
global ClickNum,lasttag
canvas.delete(lasttag)
line = lines[ClickNum]
canvas.create_line(line[0], arrow=line[1], fill=line[2],tag=line[3])
lasttag = line[3]
ClickNum = (ClickNum+1) % ClickMax
ClickNum = ClickNum or 1
canvas = Canvas(Tk(), bg="white", bd=0, highlightthickness=0)
canvas.pack(fill=BOTH, expand=YES)
drawline(0)
canvas.bind("<1>", drawline)
canvas.mainloop()
Eugene
"Ben Bush" <pythonnew at gmail.com> wrote in message
news:mailman.1023.1132680320.18701.python-list at python.org...
I had the following code and when I clicked the left mouse button one
time. I got green line and the second click got a purple line and the
green disappeared.
I was confused by two questions:
First, Clicknum increases when every time I click the button. Is it
possible to reset Clicknum to 0?
Second, how to do something like: I click the left button the first
time, the green line appear then disappear, the purple line
appear(just as the code has done but become a loop; if a second-time
click is implied, the loop stops.
from Tkinter import *
ClickNum = 0
def drawline(event):
global ClickNum
if ClickNum == 0:
canvas.create_line(100, 0, 100, 200,
arrow=FIRST,fill="green",tag="a")
elif ClickNum == 1:
canvas.delete("a")
canvas.create_line(100, 50, 60, 300, arrow=FIRST,fill="purple")
ClickNum += 1
tk = Tk()
canvas = Canvas(tk, bg="white", bd=0, highlightthickness=0)
canvas.pack(fill=BOTH, expand=YES)
canvas.create_line(100, 200, 350, 200, arrow=LAST,fill='red')
canvas.bind("<1>", drawline)
tk.mainloop()
More information about the Python-list
mailing list