Skip to content

CTk (tkinter.Tk)

Tom Schimansky edited this page Feb 6, 2023 · 18 revisions

The CTk class creates the basis of any CustomTkinter program, it creates the main app window. During the runtime of a program there should only be one instance of these class with a single call of the .mainloop() method, which starts the app. Additional windows are created using the CTkToplevel class.

Example Code:

Structure of a CustomTkinter program without any class (not recommended):

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

... program ...

app.mainloop()

Structure of a simple program with classes (recommended):

import customtkinter


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.geometry("600x500")
        self.title("CTk example")

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

    # add methods to app
    def button_click(self):
        print("button click")


app = App()
app.mainloop()

Arguments:

argument value
fg_color window background color, tuple: (light_color, dark_color) or single color

Methods:

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

    All attributes can be configured and updated.

    app.configure(fg_color=new_fg_color)
  • .cget(attribute_name)

    Pass attribute name as string and get current value of attribute.

    fg_color = app.cget("fg_color")
    ...
  • .title(string)

    Set title of window.

  • .geomtry(geometry_string)

    Set geometry and positions of the window like this: "<width>x<height>" or "<width>x<height>+<x_pos>+<y_pos>"

  • .minsize(width, height)

    Set minimal window size.

  • .maxsize(width, height)

    Set max window size.

  • .resizable(width, height)

    Define, if width and/or height should be resizablee with bool values.

  • .after(milliseconds, command)

    Execute command after milliseconds without blocking the main loop.

  • .withdraw()

    Hide window and icon. Restore it with .deiconify().

  • .iconify()

    Iconifies the window. Restore it with .deiconify().

  • .deiconify()

    Deiconify the window.

  • .state()

    Returns the window state: 'normal', 'iconic' or 'withdrawn'

⚠️ Attention ⚠️

The Github Wiki is outdated, the new documentation can be found at:

https://customtkinter.tomschimansky.com

Clone this wiki locally