Make GUI apps with Python CustomTkinter
By Swee
In this tutorial, I will show you how to make a GUI app in Python. The example here will be a registration form.
You’ll need:
- A Computer with Python 3 (Doesn’t matter which platform)
- GUI capability (X11 or Wayland on Linux)
Why CustomTkinter?
CustomTkinter is a fork of Tkinter but it has modernized GUI, you can’t use this code with Tkinter but the code will be similar.
Tkinter and CustomTkinter are GUI toolkits for Python, they’re really easy to program and design.
Getting ready
To install CustomTkinter, you must run the following command on your machine:
pip3 install customtkinter
Now that you installed the module, it’s time to make the code. Here’s what we need to get started.
# Code made by Swee ([email protected])
import customtkinter
customtkinter.set_default_color_theme("green")
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("600x500")
self.title("CTk example")
# Widgets
self.button = customtkinter.CTkButton(self, command=self.button_click)
self.button.grid(row=0, column=0, padx=20, pady=10)
# Functions
def button_click(self):
print("button click")
app = App()
app.mainloop()
For the button, fg_color
and bg_color
can be both a known color name and a HEX color.
The allowed themes are blue
, green
, and dark-blue
You can also make custom themes based on this json file.
Your code should now output something like this
And whenever you click the button, it outputs button click
into the console.
Adding more items
In this section, we will be adding a few more widgets to our application, we will be adding a label, text box, message box, and a switch alongside with our button to make a registration form. Doing this is pretty simple, we just have to add these widgets according to the documentation.
For the message box, you’ll need to run this command first:
pip3 install CTkMessagebox
Here’s the code
# Code made by Swee ([email protected])
import customtkinter
from CTkMessagebox import CTkMessagebox
customtkinter.set_default_color_theme("green")
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("410x150")
self.title("Registration")
# add widgets to app
self.label = customtkinter.CTkLabel(self, text="Name:", height=25)
self.label.grid(row=0, column=0, padx=20, pady=10)
self.textbox = customtkinter.CTkTextbox(self, height=25)
self.textbox.grid(row=0, column=1, padx=20, pady=10)
self.switch_var = customtkinter.StringVar(value="off")
self.switch = customtkinter.CTkSwitch(self, text="Agree to Terms?", variable=self.switch_var, onvalue="on", offvalue="off")
self.switch.grid(row=1, column=0, padx=20, pady=10)
self.button = customtkinter.CTkButton(self, text="Submit", command=self.button_click)
self.button.grid(row=2, column=0, padx=20, pady=10)
# add methods to app
def button_click(self):
if self.switch_var.get() == "on":
CTkMessagebox(title="Message", message="Welcome to the team, " + self.textbox.get('1.0', '32767.0'), icon="info")
else:
CTkMessagebox(title="Error", message="You must agree to the terms.", icon="cancel")
app = App()
app.mainloop()
You’ll get an output like this
Changing appearance
In this section, we will be adding some custom colors to our registration form. If we want to make the submit button blue, we can change the button’s code like this
self.button = customtkinter.CTkButton(self, text="Submit", command=self.button_click, fg_color="blue")
the output should now look like this:
But that’s a strong blue, we’d probably need a lighter blue. how about this HEX color?
Well, no problem! we’d just modify the code like this.
self.button = customtkinter.CTkButton(self, text="Submit", command=self.button_click, fg_color="#2196f3")
Now our form will look like this
But the problem is, when we hover the mouse over the button, it changes to the default green like this.
Change the button code to add “hover_color” like this
self.button = customtkinter.CTkButton(self, text="Submit", command=self.button_click, fg_color="#2196f3", hover_color="skyblue")
Now the code should set the hover color to sky blue.
Now what?
Now that you have a GUI registration form, you can add some more code to change it from a registration form to something else, or you can make it actually insert your name into a database.
If you liked this tutorial, please like this post and subscribe to SweeBlogs to receive more tutorials like this.