Is there a way to make a shape a button in a tkinter canvas?
JavaScript
x
2
1
button = Canvas.create_rectangle(100, 100, 200, 200)
2
Or, to put it simply, is there a way to figure out if the user clicked the rectangle drawn above?
Advertisement
Answer
I don’t know how to see if someone clicks the rectangle but you could have the color change if a cursor hovers over it…
I’m going to make the rectangle red, and then it will turn blue with a cursor hovering over…
JavaScript
1
2
1
button = Canvas.create_rectangle(100, 100, 200, 200, fill = 'red', activefill = 'blue')
2
this might not be exactly what you are looking for, but it is another option…
also if u just want a button:
JavaScript
1
10
10
1
from tkinter import *
2
3
def say_hello():
4
print("Hello")
5
6
root = Tk()
7
btn1 = Button(root, text="Hello", command=say_hello)
8
btn1.pack()
9
root.mainloop()
10
when pressed it will print hello!