1

I would like to smooth the edges of the button, change the default rectangular shape.

This is the code I used to create the button:

axImg = plt.axes([0.25, 0.45, 0.2, 0.09])

bnI = Button(axImg, 'Enter', color='0.9')

bnI.hovercolor = "blue"

bnI.label.set_fontsize(18),

1 Answer 1

1

You cannot shape the button itself, because it is essentially just an axes, and axes are rectangular. (Of course one could use different axes, like polar ones, but that would be completely round then.)

One can however place a FancyBboxPatch into the axes, and give that some round edges.

import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import matplotlib.patches as mpatches

bttn_ax = plt.axes([0.25, 0.45, 0.2, 0.09])
bttn_ax.set_frame_on(False)

bttn = Button(bttn_ax, 'Enter')
bttn.label.set_fontsize(18)

fancybox = mpatches.FancyBboxPatch((0,0), 1,1, 
                                   edgecolor="black",
                                   facecolor="0.9",
                                   boxstyle="round,pad=0.1", 
                                   mutation_aspect=3, 
                                   transform=bttn_ax.transAxes, clip_on=False)
bttn_ax.add_patch(fancybox)

bttn.on_clicked(lambda x: print("Click!"))

plt.show()

enter image description here

Note that hovering will not work in this case, because what is shown is the patch, not the axes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.