This repository simulates a development roadmap for creating a "Custom_Tkinter" Application namely, Scribe_Smart Notes.
📌 For more information on "Scribe_Smart Notes" visit our Page
📌 Click Download to start using "Scribe_Smart Notes"
CustomTkinter is a modern GUI library for Python that enhances Tkinter with a sleek, customizable look using themes, rounded corners, and improved widgets. It is built on top of Tkinter but offers a more modern design with dark mode support.
Theming_Support:Light, Dark, and System themes.Custom_Widgets:Includes modern buttons, sliders, and checkboxes.Rounded_Corners and Hover_Effects:Aesthetic improvements over standard Tkinter.Built-in Colors & Styling:Easily customizable UI elements.Scalability:Responsive UI that adapts to different screen sizes.
📌 Visit the Documentation to know more about Custom_Tkinter.
| TITLE | SECTION_LINK |
|---|---|
| 1. Creating a Python Virtual Environment | >> 🔗 CONTENT |
| 2. Installing and Importing required Modules for the Application | >> 🔗 CONTENT |
| 3. Custom-Tkinter Window Themes | >> 🔗 CONTENT |
| 4. Custom-Tkinter Window Geometry(Width x Height) | >> 🔗 CONTENT |
| 5. Creating Custom-Tkinter Window Instance (CTk( )) | >> 🔗 CONTENT |
| 6. Set Application Logo i.e, Window Icon | >> 🔗 CONTENT |
| 7. Custom-Tkinter Frames and Scrollable-Frames | >> 🔗 CONTENT |
| 8. Displaying Text and Images using Custom-Tkinter Labels | >> 🔗 CONTENT |
| 9. Custom-Tkinter Buttons and Segmented-Buttons | >> 🔗 CONTENT |
| 10. Custom-Tkinter Check_Box and Combo_Box | >> 🔗 CONTENT |
| 11. Custom-Tkinter Switch and Slider | >> 🔗 CONTENT |
| 12. Custom-Tkinter Entry and Text_Box | >> 🔗 CONTENT |
| 13. Custom-Tkinter Widget Attributes_List | >> 🔗 CONTENT |
14. Manipulating Widget Attributes at Runtime using .configure() |
>> 🔗 CONTENT |
15. Widget Layout Methods (Pack, Place and Grid) |
>> 🔗 CONTENT |
| 16. Creating a Tabbed Interface using Tkinter's Notebook Widget | >> 🔗 CONTENT |
| 17. Styling Tkinter's Notebook Widget using Style() Function | >> 🔗 CONTENT |
| 18. Packaging a Python Script into an EXE using PyInstaller | >> 🔗 CONTENT |
| 19. Packaging a Python Script into an EXE using Nuitka | >> 🔗 CONTENT |
- For
WindowsSystems:
Ensure Python is installed by running the following prompt:python --version - For
Linux / MacOSSystems:
Ensure Python is installed by running the following prompt:python3 --version
- For
WindowsSystems:
Create aVirtual Environmentin Python by running the following prompt:python -m venv myenv - For
Linux / MacOSSystems:
Create aVirtual Environmentin Python by running the following prompt:python3 -m venv myenv
- For
WindowsSystems:
ActivatetheVirtual Environmentin Python by running the following prompt:# For Command Prompt: myenv\Scripts\activate # For Powershell: myenv\Scripts\Activate.ps1 - For
Linux / MacOSSystems:
ActivatetheVirtual Environmentin Python by running the following prompt:source myenv/bin/activate
- For
WindowsSystems:
After use,DeactivatetheVirtual Environmentin Python by running the following prompt:deactivate - For
Linux / MacOSSystems:
After use,DeactivatetheVirtual Environmentin Python by running the following prompt:deactivate
-
os(Operating System) Library:- Provides functions to interact with the operating system.
- Used for file handling, environment variables, and system commands.
Use the following
Import Statementin your code:import os -
sys(System) Library:- Provides system-specific functions and parameters.
- Used for command-line arguments, interpreter interaction, and module management.
- Ex:
sys.exit(): For Exiting the program.sys.path(): It returns a list of directories where Python looks for modules when importing them. Can dynamically add a directory to Python's Module_Search_Paths usingsys.path.append().
Use the following
Import Statementin your code:import sys -
tkinterLibrary:- Python's built-in GUI toolkit for creating graphical user interfaces.
- Ex:
tkinter.filedialog: A module in Tkinter for Opening and Saving files viaFile_Dialog_Windows.

tkinter.messagebox: ProvidesPop_Up Dialog_Boxesfor messages, warnings, and errors.tkinter.font: HandlesFont-related Functionalityin Tkinter.tkinter.ttk:Themed_Tkinter_Widgets(ttk)for a more modern and consistent look. (Ex:ttk.Notebook,ttk.Button,ttk.Label,ttk.Entry,ttk.Checkbutton,ttk.Combobox,ttk.Progressbar)
Use the following
Import Statementin your code:import tkinterOR
from tkinter import filedialog, messagebox, font, ttk -
threadingLibrary:- Provides tools for running Multiple Threads in parallel.
- Useful for Performing Background Tasks without freezing the UI.
Use the following
Import Statementin your code:import threading
-
customtkinterLibrary:- An advanced version of Tkinter with modern UI elements and themes.
- Provides additional widgets and customization options.
Run the following Prompt to install Custom Tkinter:
pip install customtkinterUse the following
Import Statementin your code:import customtkinter as ctk -
google.generativeaiLibrary:- Google's Generative AI SDK(Software_Development_Kit) for interacting with AI models like Gemini.
- Used for Text_Generation, AI-Based tasks, and API_Interactions.
Run the following Prompt to install Custom Tkinter:
pip install google-generativeaiUse the following
Import Statementin your code:import google.generativeai as genai -
pyinstallerLibrary:- It is used to convert Python_Scripts into standalone Executables (.exe for Windows, .app for macOS, etc).
- It bundles all dependencies into a single package so that users can run the application without installing Python.
Run the following Prompt to install Custom Tkinter:
pip install pyinstaller -
Alternatively, Run this single command to install all required Libraries:
pip install customtkinter google-generativeai pyinstaller
- Don't Forget to
Activateyour Python_Virtual_Environment before package installations. ⚠️ Without a Virtual Environment, Python packages are installed globally, which can lead to "conflicts" between different Projects that require different versions of the same package.
In CustomTkinter, you can adjust the Theme using the set_appearance_mode() and set_default_color_theme() functions.
- The
set_appearance_mode()function in CustomTkinter is used to change the overall appearance of the UI, allowing you to switch between Light, Dark, or System Mode. - CustomTkinter supports the following Appearance_Modes:
- Light: Default light theme.
- Dark: Sets a Dark theme.
- System: Matches System theme settings.
- CODE:
ctk.set_appearance_mode("Dark") # Sets the appearance_mode to "Dark" # Other Options: "Light" (Default), "Dark", "System"
- The
set_default_color_theme()function in CustomTkinter is used to set the Color_Theme for widgets such as Buttons, Progress_Bars, and Sliders. - CustomTkinter supports the following Color_Themes:
- blue: Applies a Blue Color_Theme.
- dark-blue: Applies a Darker_Blue Color_Theme.
- green: Applies a Green Color_Theme.
- CODE:
ctk.set_default_color_theme("blue") # Sets the color_theme to "blue" # Other Options: "blue" (Default), "dark-blue", "green"
- Custom_Tkinter allows us to create our own Custom_Color_Theme by defining a
JSONfile with custom color themes. Step 1: Create a ".JSON" file (midnightOcean.json) and define the Custom_Color_Theme in the following format:
{ "CTk": { "fg_color": ["#e5e5e5", "#070c1c"] }, "CTkToplevel": { "fg_color": ["#e5e5e5", "#070c1c"] }, "CTkFrame": { "corner_radius": 6, "border_width": 2, "fg_color": ["#8ecae6", "#14213d"], "top_fg_color": ["#8ecae6", "#14213d"], "border_color": ["#b9cbd7", "#212529"] }, "CTkButton": { "corner_radius": 4, "border_width": 0, "fg_color": ["#14213D", "#fca311"], "hover_color": ["#25445f", "#fdb619"], "text_color": ["#fca311", "#14213D"], "border_color": ["#ffffff", "#e5e5e5"], "text_color_disabled": ["#CE93D8", "#7E57C2"] }, "CTkLabel": { "corner_radius": 0, "fg_color": "transparent", "text_color": ["#14213D", "#9bd1f2"] }, "CTkEntry": { "corner_radius": 2, "border_width": 2, "fg_color": ["#F3E5F5", "#070c1c"], "border_color": ["#fca311", "#335480"], "text_color": ["#32373b", "#e5e5e5"], "placeholder_text_color": ["#14213d", "#fca311"] }, "CTkCheckBox": { "corner_radius": 2, "border_width": 2, "fg_color": ["#14213d", "#335480"], "border_color": ["#14213d", "#335480"], "hover_color": ["#335480", "#1e2749"], "checkmark_color": ["#fca311", "#fca311"], "text_color": ["#14213d", "#9bd1f2"], "text_color_disabled": ["#CE93D8", "#B39DDB"] }, "CTkSwitch": { "corner_radius": 1000, "border_width": 3, "button_length": 0, "fg_color": ["#ffffff", "#28427b"], "progress_color": ["#14213d", "#fca311"], "button_color": ["#0096c7", "#f77f00"], "button_hover_color": ["#0077b6", "#fca311"], "text_color": ["#14213d", "#9bd1f2"], "text_color_disabled": ["#CE93D8", "#B39DDB"] }, "CTkSlider": { "corner_radius": 1000, "button_corner_radius": 1000, "border_width": 6, "button_length": 0, "fg_color": ["#ffffff", "#28427b"], "progress_color": ["#14213d", "#fca311"], "button_color": ["#0096c7", "#f77f00"], "button_hover_color": ["#0077b6", "#fca311"] }, "CTkComboBox": { "corner_radius": 2, "border_width": 1, "fg_color": ["#ffffff", "#070c1c"], "border_color": ["#fca311", "#28427b"], "button_color": ["#fca311", "#28427b"], "button_hover_color": ["#ffd60a", "#335480"], "text_color": ["#14213d", "#fca311"], "text_color_disabled": ["#CE93D8", "#B39DDB"] }, "CTkScrollbar": { "corner_radius": 800, "border_spacing": 7, "fg_color": "transparent", "button_color": ["#14213d", "#fca311"], "button_hover_color": ["#14213d", "#fca311"] }, "CTkSegmentedButton": { "corner_radius": 4, "border_width": 2, "fg_color": ["#14213d", "#fca311"], "selected_color": ["#25445f", "#ffcc00"], "selected_hover_color": ["#25445f", "#fdb619"], "unselected_color": ["#14213d", "#fca311"], "unselected_hover_color": ["#25445f", "#fdb619"], "text_color": ["#fca311", "#14213D"], "text_color_disabled": ["gray74", "gray60"] }, "CTkTextbox": { "corner_radius": 6, "border_width": 2, "fg_color": ["#f8f9fa", "#5e5e5e"], "border_color": ["#e5e5e5", "#212529"], "text_color": ["#ffffff", "#e5e5e5"], "scrollbar_button_color": ["#14213d", "#fca311"], "scrollbar_button_hover_color": ["#14213d", "#fca311"] }, "CTkScrollableFrame": { "label_fg_color": ["#14213D", "#9bd1f2"] }, "DropdownMenu": { "fg_color": ["#ffffff", "#070c1c"], "hover_color": ["#ffaa00", "#1e2749"], "text_color": ["#14213d", "#fca311"] }, "CTkFont": { "macOS": { "family": "SF Display", "size": 13, "weight": "normal" }, "Windows": { "family": "Roboto", "size": 13, "weight": "normal" }, "Linux": { "family": "Roboto", "size": 13, "weight": "normal" } } }📝Note: The RAW Color_Theme File follows the format:
{ "Widget_Name":{ "Widget_Attribute":["color_theme_for_LightMode","color_theme_for_DarkMode"], }, }Step 2: Pass this RAW file in theset_default_color_theme()function:
ctk.set_default_color_theme("./midnightOcean.json") root = ctk.CTk() root.mainloop()- 📌 Click Here to Download the
Custom ThemeFile.
In CustomTkinter, you can adjust the Initial Resolution of the Window using the geometry() function, Minimum Resize Dimensions for the Window using the minsize() function and Maximum Resize Dimensions for the Window using the maxsize() function.
- The
geometry()function in CustomTkinter sets the initial Size and Position of the Application Window. - CODE:
# Syntax: # root.geometry("<width> x <height> + <x_offset> + <y_offset>") root.geometry("600x350+750+100")
- The
minsize()function sets the Minimum Resizable Width and Height of the window, preventing the user from resizing it below the given dimensions. - CODE:
# Syntax: # root.minsize(<width>, <height>) root.minsize(350, 200)
- The
maxsize()function sets the Maximum Resizable Width and Height of the window, preventing the user from resizing it beyond the given dimensions. - CODE:
# Syntax: # root.maxsize(<width>, <height>) root.maxsize(1000, 450)
- In
CustomTkinter, theCTk()class is used to create the Main_Application Window, similar toTk()in StandardTkinter. - CODE:
import customtkinter as ctk app = ctk.CTk()
- In CustomTkinter,
mainloop()is a method that starts the event loop of the application. It keeps the GUI running and listens for user interactions like button clicks, text input, and window resizing. - In other words, When
mainloop()is called, the application enters an infinite loop that waits for events. - CODE:
import customtkinter as ctk app = ctk.CTk() app.mainloop() #Starts the Event Loop
- When using Object-Oriented Programming (OOP) in Custom_Tkinter, you define a Class for your application and create an Instance of that class to run the program.
- Using a class makes the application
ModularandScalable. - Steps to Create an Application Instance:
- Define a Class that inherits from
tk.Tk(for the Main_Application_Window). - Initialize the class using
__init__(). - Create an Instance of the Class.
- Run the Event_Loop using
.mainloop().
- Define a Class that inherits from
- CODE:
import customtkinter as ctk # Customize theme for the Custom-Tkinter App ctk.set_appearance_mode("Dark") ctk.set_default_color_theme("./midnightOcean.json") #1. Define a Class for the Application class Main(ctk.CTk): #2. Initialize the Class def __init__(self): super().__init__() # Set "Application_Title" self.title("Scribe") # Display-Window_Settings self.geometry("600x350") self.minsize(350,200) #3. Create an Instance of the Class app = Main() #4. Run the Event_Loop and Start the Application app.mainloop()Note:__init()__is a special method (also known as a constructor) that is automatically called when a new Object of a class is created. It typically contains:Parametersto accept input values.Default Attribute Valuesfor the Object Instance of the Class (self.attribute = value).Optional Logiclike validation or function calls.
super().__init__()is used in Python when a class inherits from a parent class. It helps our Application_Class to inherit features from Custom_Tkinter (CTk()) Parent Class Constructor.
- You can set a custom Icon for a Costom_Tkinter Window using the
iconbitmap()method for.icofiles. - CODE:
app = ctk.CTk() app.iconbitmap("icon.ico") app.mainloop()
- To dynamically set a Window_Icon at runtime in CustomTkinter, we use
wm_iconbitmap(), which allows specifying an.icofile for the Application_Window. - This method ensures compatibility across different execution environments, including standalone executables
.exefiles created withPyInstalleras well as Python Scripts.py. - CODE:
if getattr(sys, 'frozen', False): base_path = sys._MEIPASS else: base_path = os.path.dirname(os.path.abspath(__file__)) icon_path = os.path.join(base_path, "images", "scribe_logo.ico") self.wm_iconbitmap(icon_path) - Explanation:
- Above code ensures that the Icon is correctly applied in both development
.pyand packaged.exeexecution. getattr(sys, 'frozen', False): It checks whether the Python script is running as afrozen (compiled)executable.- The
sys.frozenonly exists when the script is compiled into an executable (e.g., with PyInstaller). - If
sys.frozenexists → ReturnsTrue. - If
sys.frozendoes not exist → ReturnsFalse (default value).
- The
sys._MEIPASS: It is used by PyInstaller to extract files for Executables i.e,.exe.os.path.dirname(os.path.abspath(__file__)): This code is used to access the parent folder/directory of the Python Script.pybeing developed.__file__: It is a special attribute in Python that represents thePath of the Current Script(.py)which is being executed.os.path.join(base_path, "images", "logo.ico"): This code is used to build the correct file path to the.icofile. It appends/images/logo.icoto eithersys._MEIPASSor the obtained parent directory fromos.path.dirname(os.path.abspath(__file__))depending upon the type of environment and execution.wm_iconbitmap(): This is a method of the Window_Manager (wm), which is why it is prefixed withwm_. It is an alias foriconbitmap()and works the same way i.e, it is same asiconbitmap()function.
- Above code ensures that the Icon is correctly applied in both development
- The
CTkFrame()method is used to create a Simple Container for grouping widgets together. - CODE:
import customtkinter as ctk app=ctk.CTk() # Create a container/Frame and assign it to the "frame" variable frame = ctk.CTkFrame(app, width=300, height=200) app.mainloop()
- The
CTkScrollableFrame()is similar to CTkFrame() but, it allows Scrolling when the content exceeds the available space. - CODE:
import customtkinter as ctk app=ctk.CTk() # Create a Scrollable_Frame and assign it to the "scrollableFrame" variable scrollableFrame = ctk.CTkScrollableFrame(app, width=300, height=200) app.mainloop()
- The
CTkLabel()is a widget in CustomTkinter that allows you to display text with a Window or Frame. - CODE:
import customtkinter as ctk app=ctk.CTk() # Create a Label and assign it to the "label" variable label = ctk.CTkLabel(app, text="Hello!") app.mainloop()
- The
CTkImage()is a CustomTkinter class that allows you to use images in light and dark mode dynamically. It is primarily used with widgets likeCTkLabel(),CTkButton()(rarely),etc. CTkImage()Attributes:light_image: This is the image that will be displayed when the application is in Light_Mode.dark image: This is the image that will be displayed when the application is in Dark_Mode.size(Optional): Defines the dimensions of the image in the format(width,height).
- Also, we need to import
Pillow (PIL)because CustomTkinter's CTkImage requires images to be in thePIL Image_Formatrather than direct file_paths.
Run the following Command in Terminal to installPillow:After installingpip install pillowPillow, import theImagemodule from the Pillow library, allowing you to open, manipulate, and save images in Python.from PIL import Image - CODE:
import customtkinter as ctk #Import PIL for Image_Handling from PIL import Image app=ctk.CTk() # Create a CTkImage Object my_image = ctk.CTkImage(light_image=Image.open("images/lightImage.png"), dark_image=Image.open("images/darkImage.png"), size=(50, 50)) # Create a Label to display the image and assign it to the "label" variable label = ctk.CTkLabel(app, image=my_image, text="") app.mainloop()
- The
CTkButton()is used for creating a modern, customizable button in CustomTkinter, designed to match the theme and appearance settings of the library. - We can also define a
Functionto be executed when the button is Clicked using thecommandAttribute. - CODE:
import customtkinter as ctk app=ctk.CTk() # Define a Function "onClick" that is executed whenever the "button" is clicked def onClick(): print("Hello User!"); # Create a Button and assign it to the "button" variable button = ctk.CTkButton(app, text="Click Me", command=onClick) app.mainloop()
- The
CTkSegmentedButton()in CustomTkinter is a button that consists of Multiple Selectable Segments. - It allows users to switch between different options, making it ideal for mode selection, filtering, or toggling between views.
- You can also use the
valuesAttribute inCTkSegmentedButtonto define the different selectable options available in the button. - CODE:
import customtkinter as ctk app=ctk.CTk() # Define a Function "onClick" that is executed whenever the "button" is clicked def onClick(value): print(f"You chose {value}") # Create a Segmented_Button and assign it to the "segmentedButton" variable segmentedButton = ctk.CTkSegmentedButton(app, values=["Rock", "Paper", "Scissors"], command=onClick) app.mainloop()
- The
Functionto be executed on Clicking the button must be defined before defining the button, or else thecommandAttribute might fail to recognize the function!
- A
CTkCheckBox()is a customizable checkbox widget in CustomTkinter that allows users to toggle between Checked and Unchecked states. - It can be used to let users select multiple options in a form, survey, or settings panel.
- CODE:
import customtkinter as ctk app = ctk.CTk() # Create a Function "onCheck" which performs operations depending upon the "checkbox's State" def onCheck(): print(f"Checkbox State: {checkbox.get()}") # 1 if checked, 0 if unchecked # Create a CheckBox checkbox = ctk.CTkCheckBox(app, text="Accept Terms", command=onCheck) app.mainloop()
- A
CTkComboBox()is a Dropdown_Menu that allows users to select one option from multiple choices. - The
valuesattribute is used for displaying multiple choices in Combo_Box. - CODE:
import customtkinter as ctk app = ctk.CTk() # Define a Function "onClick" that is executed whenever a "Choice" is selected in the "ComboBox" def onClick(choice): print(f"You chose {choice}") # Create a ComboBox combo = ctk.CTkComboBox( app, values=["Rock", "Paper", "Scissors"], command=onClick ) app.mainloop()
- A
CTkSwitch()is a widget in CustomTkinter that provides a Modern Toggle_Switch. - It allows users to switch between
ONandOFFstates. - Core Attributes:
command: Calls a function when toggled.variable: A temporary container which stores the Switch_State.onvalue/offvalue: Defines Values for ON and OFF states.
- CODE:
import customtkinter as ctk app = ctk.CTk() # Define the "Function" to be executed when the "Switch_State" is "Toggled" def toggleSwitch(): print("Switch is:", switch_var.get()) # Create a "Switch_Variable" switchState = ctk.StringVar(value="on") # Default to "on" or "off" Switch_State # Create a "CTkSwitch()" Instance switch = ctk.CTkSwitch(app, text="Switch", command=toggleSwitch, variable=switchState, onvalue="on", offvalue="off") app.mainloop()
- The
CTkSlider()is a modern, customizable Slider_Widget that allows users to select a value from a specified range. - Core Attributes:
command: Calls a function for "Slider_Event".from_: Defines the Minimum_Value for the Slider.to: Defines the Maximum_Value for the Slider.
- You can also set the Slider to a Default_Value using the
slider.set(value)function. Replace thevaluewith anInteger. - CODE:
import customtkinter as ctk app = ctk.CTk() # Define a Function "onSlide" that is executed eith a "Slider_Event" def onSlide(value): print(f"User Counts: {int(value)}") # Create a "CTkSlider()" instance slider = ctk.CTkSlider(app, from_=0, to=100, command=onSlide) # Set "Default_Value" for the Slider slider.set(50) app.mainloop()
- The
CTkEntry()is aSingle-Line Text InputWidget that allows users to enter aShort String, such as a name, email, or password. - You can also include a Placeholder using the
placeholder_textattribute. - CODE:
import customtkinter as ctk app = ctk.CTk() # Define a Function "get_input()" to get the input data from the "CTkEntry()" Widget def get_input(): print("Hello: ", entry.get()) # Create a "CTkEntry()" instance entry = ctk.CTkEntry(app, placeholder_text="Enter your name") app.mainloop() - Important Methods for manipulating Inputs:
.get(): Retrieves theCurrent Input_Textfrom the Widget.
# Retrieves the Current_Text from the entry entry_text = entry.get().insert(index,text): Inserts text at a Specific_Index.
# Insert Text at the Beginning entry.insert(0, "Text_Value")# Insert Text at the End entry.insert("end", "Text_Value").delete(start,end): Deletes text from the given Index_Range.
# Clear all Text entry.delete(0, "end")
- A
CTkTextbox()is aMulti-Line Text InputWidget used forLonger Stringinput, such as comments, messages, or notes. - CODE:
import customtkinter as ctk app = ctk.CTk() # Define a Function "get_text()" to get text input from the "CTkTextbox()" Widget def get_text(): print("User:", textbox.get("1.0", "end")) # ".get("1.0", "end")" is used to get all the Text from "row 1, column 01" (i.e, "1.0") to the "end" # Create a "CTkTextbox()" instance textbox = ctk.CTkTextbox(app, width=300, height=100) app.mainloop() - Important Methods for manipulating Input Text:
.get("1.0","end"): Retrieves theCurrent Input_Textfrom the Widget.
# Retrieves the Current_Text from the entry textbox_text = textbox.get("1.0", "end").insert(row_index.col_index,text): Inserts text at a Specific_Index.
# Insert Text at the Beginning textbox.insert("1.0", "Text_Value")# Insert Text at the End textbox.insert("end", "Text_Value").delete(start,end): Deletes text from the given Row.Column_Range.
# Clear all Text textbox.delete("1.0", "end")
| Attribute | Description | Example |
|---|---|---|
| 1. text | Sets the Text to be displayed for the Widgets |
button = ctk.CTkButton(app, text = "Click Me") |
| 2. width | Sets the Widget's Width in pixels |
button = ctk.CTkButton(app, width = 300) |
| 3. height | Sets the Widget's Height in pixels |
button = ctk.CTkButton(app, width = 300, height = 150) |
| 4. fg_color | Sets the Foreground_Color |
button = ctk.CTkButton(app, fg_color = "#fca311") |
| 5. bg_color | Sets the Background_Color |
button = ctk.CTkButton(app, bg_color = "transparent") |
| 6. text_color | Sets the Text_Color |
button = ctk.CTkButton(app, text_color = "#14213D") |
| 7. hover_color | Changes the Widget’s color when the user hovers over it |
button = ctk.CTkButton(app, hover_color = "#fdb619") |
| 8. corner_radius | Sets the Widget's Border_Curvature |
button = ctk.CTkButton(app, corner_radius = 5) |
| 9. border_width | Sets Border_Thickness |
button = ctk.CTkButton(app, border_width = 2) |
| 10. font | Sets Font_Family, Font_Size and Font_Style |
button = ctk.CTkButton(app, text = "Click Me", font = ("Arial", 12, "normal")) |
| 11. padx | Adds Padding along X-axis |
button = ctk.CTkButton(app, padx = 5) |
| 12. pady | Adds Padding along Y-axis |
button = ctk.CTkButton(app, pady = 10) |
| 13. anchor | Aligns the Widget's Content to w (Left or West), e (Right or East), n (Top or North), s (Bottom or South), nw (Top-Left or NorthWest), ne (Top-Right or NorthEast), sw (Bottom-Left or SouthWest), se (Bottom-Right or SouthEast) |
button = ctk.CTkButton(app, anchor = "w") |
| 14. command | Assigns a Function to be executed by the Widget |
button = ctk.CTkButton(app, command = onClick) |
- The
configure()method allows you to dynamically change Widget's Attributes after the widget is created. - Syntax:
<widget_name>.configure(<attribute> = <value>) - Example:
button = ctk.CTkButton(app, text="") button.pack(pady=10) button.configure(text="Click Me")
- We have learned how to define widgets, but to display them and enable interactions, we must use
LayoutMethods to position them within the window. - Widgets are not visible unless placed inside the window using the
LayoutMethods. - Some
LayoutMethods are:
- It stacks widgets Vertically or Horizontally.
- By default it arranges widgets in
Top-to-Bottomorder. - It is generally used to arrange widgets
horizontallyorverticallyinside a parent widget (Ex:CTkFrame). - Core Attributes:
side: It controls theWidget_Placement_Directioni.e, the direction in which the widgets are to be stacked. ("top","bottom","left","right")fill: Expands a widget to cover the screen in the given direction. ("x","y","both",None)expand: Makes widget fill the extra space on resizing. (TrueorFalse)padxandpady: AddsPadding (Spacing)around the widget.ipadxandipady: AddsInternal Paddingto the widget. Can be used for increasing the Widget's Size.anchor: Positions the Widget. (center,n,s,e,w,etc.)
- SAMPLE CODE:
import customtkinter as ctk app=ctk.CTk() # Define a Function "onClick" that is executed whenever the "button" is clicked def onClick(): print("Hello User!"); # Create a "CTkButton" and assign it to the "button" variable button = ctk.CTkButton(app, text="Click Me", command=onClick) # Pack the "CTkButton" instance using the "pack()" function button.pack( side="top", # Position button at the top anchor="center", # Align button to the center expand=True, # Allow resizing fill="none", # Keep original size padx=20, pady=20, # External spacing ipadx=10, ipady=5 # Internal spacing ) app.mainloop()
- It is used to precisely position widgets using
XandYCoordinates. - It is useful for
overlappingwidgets without causing errors during User Interactions. - It is ideal for
Absolute Positioningof the Widgets. - Core Attributes:
x: Sets theX-Coordinatei.e,Horizontal Positioning.y: Sets theY-Coordinatei.e,Vertical Positioning.relx: Defines the horizontal positionRelative to the Parent Widget’s Width.relx = 0.0stands for 0% of Parent Widget's Width i.e,Left Edge.relx = 0.5stands for 50% of Parent Widget's Width i.e,Horizontal Center.relx = 1.0stands for 100% of Parent Widget's Width i.e,Right Edge.
rely: Defines the vertical positionRelative to the Parent Widget’s Height.rely = 0.0stands for 0% of Parent Widget's Height i.e,Top Edge.rely = 0.5stands for 50% of Parent Widget's Height i.e,Vertical Center.rely = 1.0stands for 100% of Parent Widget's Height i.e,Bottom Edge.
width: Sets the Widget'sWidth.height: Sets the Widget'sHeight.relwidth: Defines Widget's Width as aFraction of the Parent’s Width. (0.0(0%) to1.0(100%))relheight: Defines Widget's Height as aFraction of the Parent’s Height. (0.0(0%) to1.0(100%))anchor: Sets theReference Pointfor positioning. (center,n,s,e,w,etc.)
- SAMPLE CODE:
import customtkinter as ctk app = ctk.CTk() # Define a Function "onClick" that is executed whenever the "button" is clicked def onClick(): print("Hello User!"); # Create a "CTkButton" and assign it to the "button" variable button = ctk.CTkButton(app, text="Click Me", command=onClick) # Place the "CTkButton" instance using the "place()" function button.place( # x=200, y=150, # Absolute position (unnecessary as "relx" and "rely" are being used!) relx=0.5, rely=0.5, # Positions button at the absolute center of the Viewing Window # width=100, height=50 # Fixed size (unnecessary as "relwidth" and "relheight" are being used!) relwidth=0.2, # Sets the relative width relheight=0.1, # Sets the relative height anchor="center", # Aligns the button to the "center" ) app.mainloop()
- This method is used for arranging widgets in a
Table-Likestructure (i.e,Rows&Columns). - It is ideal for
Form Layouts,Dashboards, andStructured UIs. - Core Attributes:
row: Specifies theRowNumber. (0-Based Indexing)column: Specifies theColumnNumber. (0-Based Indexing)rowspan: Expands widget acrossMultiple Rows.columnspan: Expands widget acrossMultiple Columns.padxandpady: AddsPadding (Spacing)around the widget.ipadxandipady: AddsInternal Paddingto the widget. Can be used for increasing the Widget's Size.relwidth: Defines Widget's Width as aFraction of the Parent’s Width. (0.0(0%) to1.0(100%))relheight: Defines Widget's Height as aFraction of the Parent’s Height. (0.0(0%) to1.0(100%))sticky: It is used forAligningthe Widget within itsGrid Cell. (center,n,s,e,w,etc.)
- We can also use the
grid_rowconfigure()andgrid_columnconfigure()methods to control how Rows and Columns Expand inside another Widget or the Viewing Window.grid_rowconfigure(index, weight = value): ControlsRow Expansion.grid_columnconfigure(index, weight = value): ControlsColumn Expansion.NOTE: Settingweight=1allowsResizing, making the row / column flexible.
- SAMPLE CODE:
import customtkinter as ctk app = ctk.CTk() # Define a Function "onClick" that is executed whenever the "button" is clicked def onClick(): print("Hello User!"); # Create a "CTkButton" and assign it to the "button" variable button = ctk.CTkButton(app, text="Click Me", command=onClick) # Place the "CTkButton" instance using the "grid()" function button.grid( row=0, column=0, # Position the button in row_0, column_0 rowspan=1, columnspan=1, # Span across 1 row and 1 column sticky="nsew", # Expand in all directions padx=20, pady=20, # External spacing ipadx=10, ipady=5 # Internal spacing ) # Configure the Rows and Columns to allow Expansion app.grid_rowconfigure(0, weight=1) app.grid_columnconfigure(0, weight=1) app.mainloop()
- A
Tabbed Interfaceallows users to switch between different sections of a GUI usingTabs, similar to a Web Browser. - In Tkinter / Custom_Tkinter, this can be achieved using
ttk.Notebook()from thetkinter.ttkmodule. - First, we need to import
Notebookfromtkinter.ttkmodule:from tkinter.ttk import Notebook - Then, we need to create a
Notebook (Tab Container):notebook = Notebook(app) - Finally, we need to position and display the notebook using
Layout Methods:notebook.pack(expand=True, fill="both")
add(<notebook_child>, text="Tab_name"): Adds a newTab (Frame)to the Notebook.notebook.add(tab1, text="Home")select(<existing_notebook_child>): Switches to an existingTab (Frame)which is specified in the arguments.notebook.select(tab1)forget(<existing_notebook_child>"): Completely removes an existingTab (Frame)from the Notebook.notebook.forget(tab1)hide(<existing_notebook_child>"): Hides a Tab without deleting it (can be shown later using theadd()Function).# Hide the Tab notebook.hide(tab1) # Display the Tab again notebook.add(tab1)index(<existing_notebook_child>"): Gets theNumerical_Indexof aTab.print(notebook.index(tab1)) # Output: 0 (if tab1 is the first tab)tab(<existing_notebook_child>, attribute = "Value"): It is used toGetorSetproperties of a specificTabdynamically.ORnotebook.tab(tab1, text="New Home", state="normal", padding=[10, 5])
We can also use thetab()Function to get all thePropertiesof a specificTabfrom the Notebook.tab_properties = notebook.tab(tab1) print(tab_properties)enable_traversal(): Allows users to switch between Tabs usingCtrl+TabandCtrl+Shift+Tab.notebook.enable_traversal()instate(["state"], <command>): It is used to check whether aTabis in a specific state likedisabled,selected, ornormal.ORif notebook.instate(["disabled"]): print("The Notebook is disabled.")def displayMessage(): print("Tab is Selected from Notebook.") notebook.instate(["selected"], displayMessage)
- The
ttk.Notebook()widget creates Tabbed Interfaces, but its default style is quite basic. You can Customize it using thettk.Style()class. - CODE:
# Create an instance of the "ttk.Style()" class style = ttk.Style() # Configure the "ttk.Notebook()" Attributes style.configure("TNotebook", background="#2e2e2e", foreground="#000000", borderwidth=0, padding=[0, 1]) # Style the ttk.Notebook() "Tabs (Frames)" style.configure("TNotebook.Tab", background="#242424", foreground="#ffc300", padding=[10, 6], font=("Comic Sans MS", 10, "normal"), corner_radius=0) # Configure the "Selected Tab's" Attributes style.map("TNotebook.Tab", background=[("selected", "#1d1d1d")], foreground=[("selected", "#ffd60a")], borderwidth="0")
PyInstalleris a Python tool that packages Python Scripts into Standalone Executable (.exe) files.- This allows you to run Python programs without requiring Python to be installed on your system.
- It is Customizable i.e, we can add
Icons,Hidden Imports, andAdditional Files.
- Paste the following prompt in your Terminal to install
PyInstaller:pip install pyinstaller
- Paste the following prompt in your Terminal to package a
Python ScriptintoStandalone Executable (.exe):pyinstaller --onefile --noconsole --icon=<filepath> --add-data "<filepath>" <filename>.pyNote:Replace<filename>with yourPython_Script'sName which you want to be bundled and<filepath>with yourApplication_Icon'sFile Path.
| Sr. No. | PyInstaller Flag | Description |
|---|---|---|
| 1 | --onefile |
Create a single executable (.exe). |
| 2 | --noconsole |
Hides Console Window (for GUI). |
| 3 | --icon=<filepath> |
Set Custom Icon for the executable. |
| 4 | --add-data "<filepath>" |
Adds additional files and resources to the bundled executable. |
Nuitkais a Python Compiler that translates your scripts intoC code, creating a highly optimized Standalone Executable (.exe).- This allows you to distribute and run
Pythonprograms without requiring Python to be installed on the target system. - It is highly customizable, allowing you to embed
icons, includedata files, and managedependencieswith plugins.
- Paste the following prompt in your Terminal to install
Nuitka:pip install nuitka
- Paste the following prompt in your Terminal to package a
Python ScriptintoStandalone Executable (.exe):nuitka --standalone --enable-plugin=<pluginName> --include-data-dir=<source>=<destination> --windows-icon-from-ico=<filepath> --windows-console-mode=disable <filename>.pyNote:Replace<filename>with yourPython_Script'sName which you want to be bundled and<filepath>with yourApplication_Icon'sFile Path.
| Sr. No. | PyInstaller Flag | Description |
|---|---|---|
| 1 | --standalone |
Create a single Executable (.exe) File. |
| 2 | --enable-plugin=<pluginName> |
It activates a specific Nuitka Plugin to correctly handle the complexities of certain Python Libraries. |
| 3 | --include-data-dir=<source>=<destination> |
This command includes Non-Code Data Files. It copies the entire directory from your |
| 4 | --windows-icon-from-ico=<filepath> |
Sets the Path for Custom Icon for the Executable. |
| 5 | --windows-console-mode=disable |
Hides Console Window (for GUI). |
