drawline

B

Ben Bush

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()
 
E

Eugene Druker

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


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()
 
M

Magnus Lycka

Ben said:
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:

It's good that these postings are archived, so that teachers can
check them before grading their students. After all, the grades
belong to those who solved the problems, not to those who handed
them in...
 
B

Ben Bush

It's good that these postings are archived, so that teachers can
check them before grading their students. After all, the grades
belong to those who solved the problems, not to those who handed
them in...
I learned Python by myself and not seeking to hand in these results
for anyone. All the questions are not from any textbook. Please be
polite and do not assume something just in your mind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top