The CTk class creates the foundation of any CustomTkinter program. It generates the main application window. During the execution of a program, there should be only one instance of this class with a single call to the .mainloop() method, which starts the application. Additional windows are created using the CTkToplevel class.

Code Example:

Structure of a CustomTkinter Program Without a Class (Not Recommended):

import customtkinter

app = customtkinter.CTk()
app.geometry("600x500")
app.title("CTk example")

# ... program ...

app.mainloop()

Structure of a Simple Program With Classes (Recommended):

import customtkinter

# Main class
class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.geometry("400x200")
        self.title("CTk Example")

        # Add widgets to the application
        self.button = customtkinter.CTkButton(self, command=self.button_click)
        self.button.grid(row=0, column=0, padx=20, pady=10)

    # Add methods to the application (function)
    def button_click(self):
        print("Button clicked")

# Initializing the App
app = App()
app.mainloop()

Arguments

  • fg_color - Background color of the window, tuple: (light_color, dark_color) or a single color.

Methods

  • .configure(attribute=value, ...)

    All attributes can be configured and updated.

    app.configure(fg_color='#4fa882')
  • .cget(attribute_name)

    Pass the attribute name as a string to get its current value.

    fg_color = app.cget("fg_color")
  • .title(string) - Sets the window title.

  • .geometry("geometry_string") - Defines the geometry and window position like: "<width>x<height>" or "<width>x<height>+<x_pos>+<y_pos>".

  • .minsize(width, height) - Sets the minimum window size.

  • .maxsize(width, height) - Sets the maximum window size.

  • .resizable(width, height) - Defines whether the width and/or height should be resizable with boolean values.

  • .after(milliseconds, command) - Executes the command after milliseconds without blocking the main loop.

  • .withdraw() - Hides the window and its icon. Restore it with .deiconify().

  • .iconify() - Iconifies the window. Restore it with .deiconify().

  • .deiconify() - Deiconifies the window.

  • .state() - Returns the window state: 'normal', 'iconic', or 'withdrawn'.