CustomTkinter provides an easy way to create modern and customizable buttons with an enhanced appearance. In this tutorial, we will learn how to create and customize buttons using the CTkButton widget.
How to Create a Simple Button in CustomTkinter
The following example creates a simple button centered in a window.
import customtkinter as ctk
# Main class
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.title("Creating Button")
# Function when button is clicked
def button_clicked():
print("Button was clicked!")
# Creating the button
self.button = ctk.CTkButton(self, text="Click Here", command=button_clicked)
self.button.place(relx=0.5, rely=0.5, anchor="center") # Centering the button
# Initializing the App
app = App()
app.mainloop()
Description:
-
The
place()
method is used to center the button in the window usingrelx=0.5
,rely=0.5
, andanchor="center"
. -
The button text is defined using the
text
argument. -
The click event is set up with the
command
argument, which calls thebutton_clicked()
function.
How to Customize a Button in CustomTkinter
You can also customize the button's appearance in various ways, such as background color, font, border radius, and more. See the following example:
import customtkinter as ctk
# Main class
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.title("Customized Button")
# Function when button is clicked
def button_clicked():
print("Customized Button Clicked!")
# Creating the customized button
self.button = ctk.CTkButton(
self,
text="Click Here",
command=button_clicked, # Function called on button click
width=200, # Button width
height=40, # Button height
fg_color=("white", "gray75"), # Background color
text_color=("black", "white"), # Text color
corner_radius=10, # Rounded corners
font=("Arial", 14) # Custom font
)
self.button.place(relx=0.5, rely=0.5, anchor="center") # Centering the button
# Initializing the App
app = App()
app.mainloop()
Customization Features in the Example:
-
fg_color
: Background color of the button (can be a tuple for light and dark modes). -
text_color
: Text color of the button. -
corner_radius
: Adds rounded corners to the button. -
font
: Defines the font and size of the button text.
CTkButton Arguments
Here are some key arguments you can use when creating a button with CTkButton
:
Argument | Description |
---|---|
master | Defines the parent widget (window or frame). |
text | Text displayed on the button. |
command | Function called when the button is clicked. |
width | Button width (in pixels). |
height | Button height (in pixels). |
fg_color | Button background color (can be a tuple of colors). |
text_color | Button text color. |
corner_radius | Button corner radius (for rounded borders). |
font | Button text font. |
anchor | Text alignment (e.g., "center", "left", "right"). |
padx, pady | Internal padding (horizontal and vertical). |
CTkButton Methods
Besides arguments, CTkButton
also has methods that allow you to manipulate its appearance and behavior:
-
.configure(attribute=value)
: Updates button attributes.
self.button.configure(text="New Text")
-
.cget(attribute_name)
: Retrieves the current value of an attribute.
current_text = self.button.cget("text")
print(current_text)
-
.bind(sequence, command)
: Binds events to the button, such as clicks.
self.button.bind("<Button-1>", lambda e: print("Button clicked!"))
Example: Dynamically Updating Button Text
In the following example, we create a button that updates its own text when clicked.
import customtkinter as ctk
# Main class
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.geometry("400x200")
self.title("Updating Button Text")
# Function to update the text
def update_text():
self.button.configure(text="Text Updated!")
# Creating the button
self.button = ctk.CTkButton(
self,
text="Click to Update",
command=update_text, # Function to update text
width=200,
height=40
)
self.button.place(relx=0.5, rely=0.5, anchor="center")
# Initializing the App
app = App()
app.mainloop()
Description:
-
The button updates dynamically when clicked, changing its text using the
.configure()
method.
Now you have learned how to create and customize buttons with CustomTkinter, as well as how to dynamically change button text! With this knowledge, you can create rich and interactive user interfaces with ease.
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/customtkinter/how-to-create-buttons-in-customtkinter/
License Agreement:Attribution-NonCommercial 4.0 International License