CTkButton: Buttons In CustomTkinter

Hello, Pythonistas Welcome Back. Today we will see how to make a fully functional modern Button in CustomTkinter.

We will use the CTkButton Widget.

How Does It Look?

CTkButton

Basic Code

This is how you can make a simple button in CustomTkinter (or CTk).

from customtkinter import *

# Either this way
app = CTk()
button = CTkButton(app)
button.pack(padx = 10, pady = 10)
app.mainloop()

# Or this way
# In OOP
class App(CTk):
    def __init__(self):
        super().__init__()

        self.button = CTkButton(self)
        self.button.pack(padx = 10, pady = 10)

        
if __name__ == "__main__":
    app = App()
    app.mainloop()

Don’t write both together.

Here I have shown how to make and display a button in CTk.

Like any other widget in CTk, a button is first created and then it is pushed to the window.

It takes a compulsory argument master. This will specify where the button will stay.

A Sample Modern CTkButton

We will see how you can make a button similar to the one on this website’s homepage using CTkButton.

We would need to set a few configurations to make it done:

class App(CTk):
    def __init__(self):
        super().__init__()

        self.button = CTkButton(
            self, text="Blogs", #Text to be displayed
            fg_color="#ec3642", #color of the button
            hover_color="white", #color of the button when mouse is over
            font=("Montserrat", 16), #font used
            corner_radius=12, width=100, #radius of edges and total width
            command=self.open) #Command to run when button is clicked
        self.button.pack(padx = 10, pady = 10)

    def open(self):
        import webbrowser #library to open a specific URL
        # Opening the given link.
        webbrowser.open("https://python-hub.com/")

if __name__ == "__main__":
    app = App()
    app.mainloop()

You can also add these configurations using the configure() method of CTkButton. Provide attributes in attribute=”value”. Only if the value will be a string.

You can add any of the functions to be executed in the command argument.

To make it an exit button you can write self.quit.

All Configurations

Here are a few configurations you can add to the button:

ArgumentValue
masterThis is where the button will be displayed
widthprovide width in px
heightprovide height in px
corner_radiusprovide corner radius in px
border_widthbutton border’s width in px
fg_colorbutton’s color
hover_colorbutton’s color on mouse hover
border_colorborder’s color
text_colorColor of text in the button
text_color_disabledtext color when button is disabled
texttext color when the button is disabled
fontfont of button text, tuple: (“font_name”, size)
imageTo put an image on the button, it removes the text, image must be an instance of the class PhotoImage
state“normal” or “disabled” (can’t click it, darker in color)
hoverTo all effects on hover: True, else False
commandfunction to be executed on button click

I am not going to give any challenges at the end of such articles as they are just for your quick reference.

Note I haven’t included all the methods and attributes. You can always get that on the documentation.

Leave a Reply