I am working on a couple ideas here:
- hovering over a button changes the colour (using cursor binding) - works fine
- when clicked, the button changes colour - issues with this
If the user is hovering over the button, the button changes colour. The first time it works, and then if the user click anywhere OUTSIDE of the button, the button changes colour. Any ideas?
from graphics import *
win = GraphWin("button", 500, 500)
win.master.attributes('-topmost', True)
def motion(event):
x1, y1 = event.x, event.y
if (x1 > 10 and x1 < 80) and (y1 > 20 and y1 < 80):
rectangle1.setFill("red")
if win.getMouse():
rectangle1.setFill("blue")
print("Clicked")
else:
rectangle1.setFill("white")
win.bind('<Motion>', motion)
rectangle1 = Rectangle(Point(10, 20), Point(80, 80))
rectangle1.draw(win)
win.mainloop()
EDIT : I found a slight workaround, but it is doesn't feel like it is workable for a game or even menu. It works as long as the mouse is completely still when you click on it. As I said, I am sure there are ways to make this better so any help is appreciated.
from graphics import *
import time
win = GraphWin("buttons", 500, 500)
win.master.attributes('-topmost', True)
def motion(event):
x1, y1 = event.x, event.y
if (x1 > 10 and x1 < 80) and (y1 > 20 and y1 < 80):
rectangle1.setFill("red")
else:
rectangle1.setFill("white")
click = win.getMouse()
if (click.getX()>10 and click.getX()<80) and (click.getY()>20 and click.getY()<80):
rectangle1.setFill("blue")
print("Clicked")
win.bind('<Motion>', motion)
rectangle1 = Rectangle(Point(10, 20), Point(80, 80))
rectangle1.draw(win)
win.mainloop()
In
motionI would use use global variableAnd I would use
<Button-1>to run function when mouse (left-button) was clicked. And it would check ifhoveredisTrueBut I don't know if this resolve all problems. Because when you keep press button then it sets
BLUEbut if I move then change color toREDbut it should keep this color until I release button. It may need to use<ButtonPress-1>and<ButtonRelease-1>EDIT:
it seems this works like real button
EDIT:
If I put code in class
Buttonthen I can simply create many buttons