4

Is there a way to make a shape a button in a tkinter canvas?

 button = Canvas.create_rectangle(100, 100, 200, 200)

Or, to put it simply, is there a way to figure out if the user clicked the rectangle drawn above?

Quantalabs
  • 379
  • 2
  • 14
  • Seems to be already asked question. here you go - https://stackoverflow.com/questions/29211794/how-to-bind-a-click-event-to-a-canvas-in-tkinter – DS_ Sep 13 '20 at 23:08
  • 1
    Use `canvas.tag_bind(button, '', callback)`. – acw1668 Sep 13 '20 at 23:22

1 Answers1

1

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...

button = Canvas.create_rectangle(100, 100, 200, 200, fill = 'red', activefill = 'blue')

this might not be exactly what you are looking for, but it is another option...

also if u just want a button:

from tkinter import *

def say_hello():
    print("Hello")

root = Tk()
btn1 = Button(root, text="Hello", command=say_hello)
btn1.pack()
root.mainloop()

when pressed it will print hello!

pickle
  • 92
  • 10