Tkinter custom create buttons

Question:

Can tkinter create custom buttons from an image or icon like this?

enter image description here

Asked By: bunyaminkirmizi

||

Answers:

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 button

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

Answered By: PyDer

I created a library called CustomTkinter, and with it you can create more or less exactly what is shown in the images above. CustomTkinter provides new widgets for Tkinter, which can be customised in color and shape. Here I tried to create something similar to the image above:

enter image description here

You can find the example code to the above image here.

There is not also a Button, but many other elements, and it also supports a dark and light theme:

enter image description here

You can check out the library here:

https://github.com/TomSchimansky/CustomTkinter

A simple example would be:

import tkinter
import customtkinter

customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("blue")

root_tk = customtkinter.CTk()  # create CTk window like the Tk window
root_tk.geometry("400x240")

def button_function():
    print("button pressed")

# Use CTkButton instead of tkinter Button
button = customtkinter.CTkButton(master=root_tk, command=button_function)
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)

root_tk.mainloop()

which gives the following on macOS:

enter image description here

Answered By: Tom
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.