Tkinter custom create buttons Tkinter custom create buttons python python

Tkinter custom create buttons


It's possible!

If you check out the button documentation, you can use an image to display on the button.

For example:

from tkinter import *root = Tk()button = Button(root, text="Click me!")img = PhotoImage(file="C:/path to image/example.gif") # make sure to add "/" not "\"button.config(image=img)button.pack() # Displaying the buttonroot.mainloop()

This is a simplified example for adding an image to a button widget, you can make many more cool things with the button widget.


For a Tkinter project I created a TkinterCustomButton class build on top of a tkinter.Frame. It draws all shapes on a tkinter.Canvas. You can use it like a normal tkinter.Button, but it's highly customizable. You can change the corner-radius, the border width, and all the colors:

Tkinter modern rounded custom buttons

So if you want your tkinter buttons to look customized and more modern like the examples in the question, it's not always necessary to use images.

Although this does not really answer the question, maybe this example is helpful for someone.

You can find the TkinterCustomButton class and the example program here:https://github.com/TomSchimansky/GuitarTuner/tree/master/documentation

The most simple example would be:

import tkinterfrom tkinter_custom_button import TkinterCustomButtonapp = tkinter.Tk()app.geometry("300x200")app.title("TkinterCustomButton")def button_function():    print("Button pressed")button_1 = TkinterCustomButton(text="My Button", corner_radius=10, command=button_function)button_1.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)app.mainloop()

which gives:

enter image description here

On Windows I experienced that the render-quality of the tkinter canvas is extremely awful, so rounded shapes, especially with thin borders don't look good...