Tkinter/Zelle - Can I send an argument into a Tkinter Motion event?

90 views Asked by At

I am working on creating a drawing program. Presently I can DRAG and DRAW in a rectangle. While outside of the rectangle, I want to create several colours boxes to select different colours and send that colour into this Tkinter function :

win.bind('<B1-Motion>', motion)

The issue is that I am unsure how to do it. When I try to send the "colour" into the motion event, I get an error because I am unsure how to send an additional argument into it.

from graphics import *
import time
win = GraphWin("mouse", 500, 500)
win.master.attributes('-topmost', True)
rect=Rectangle(Point(20,20), Point(200,300))
rect.draw(win)

def motion(event): # how can I enter another argument?
    x1, y1 = event.x, event.y
    if (x1>20 and x1<200) and (y1>20 and y1<300):
        point1 = Point(x1, y1)
        point1.setFill("blue") # want to send a value into here
        point1.draw(win)
    else:
        pass

def drawingit():
    coord = win.checkMouse()
    if coord == None:
        print("nothing clicked")
    elif (coord.getX()>20 and coord.getX()<200) and (coord.getY()>20 and coord.getY()<300):
        win.bind('<B1-Motion>', motion)
    else:
        point1 = Point(coord.getX(), coord.getY())
        point1.setFill("red")
        point1.draw(win)
        time.sleep(.04)

while True:
    drawingit()

win.mainloop()

EDIT : added green colour rectangle

from graphics import *
import time
win = GraphWin("mouse", 500, 500)
win.master.attributes('-topmost', True)
rect=Rectangle(Point(20,20), Point(200,300))
rect.draw(win)
rectorange=Rectangle(Point(20,430), Point(200,460))
rectorange.setFill("green")
rectorange.draw(win)
pick_colour="black"

def motion(event):
    x1, y1 = event.x, event.y
    if (x1>20 and x1<200) and (y1>20 and y1<300):
        point1 = Point(x1, y1)
        point1.setFill(pick_colour)
        point1.draw(win)
    else:
        pass

def drawingit():

    coord = win.checkMouse()
    if coord == None:
        print("nothing clicked")
        #time.sleep(0.5)
    elif (coord.getX()>20 and coord.getX()<200) and (coord.getY()>20 and coord.getY()<300):
        win.bind('<B1-Motion>',  motion)
    elif (coord.getX()>20 and coord.getX()<200) and (coord.getY()>430 and coord.getY()<460):
        pick_colour="green"
    else:
        point1 = Point(coord.getX(), coord.getY())
        point1.setFill("black")
        point1.draw(win)
        time.sleep(.04)

while True:
    drawingit()

win.mainloop()
0

There are 0 answers