Skip to content
Advertisement

How to make a shape a button in Tkinter

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?

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…

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!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement