Skip to content

CTkToplevel

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

The CTkToplevel class is used to create additional windows. For a CTkToplevel window, there is no additional call of .mainloop() needed, it opens right when it's created.

Example Code:

The following example shows how to create a toplevel window, which can be opened from the main app widnow. Before the toplevel window gets created, it is checked if the window already exists, to prevent opening the same window multiple times.

import customtkinter

class ToplevelWindow(customtkinter.CTkToplevel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.geometry("400x300")

        self.label = customtkinter.CTkLabel(self, text="ToplevelWindow")
        self.label.pack(padx=20, pady=20)

class App(customtkinter.CTk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.geometry("500x400")

        self.button_1 = customtkinter.CTkButton(self, text="open toplevel", command=self.open_toplevel)
        self.button_1.pack(side="top", padx=20, pady=20)

        self.toplevel_window = None

    def open_toplevel(self):
        if self.toplevel_window is None or not self.toplevel_window.winfo_exists():
            self.toplevel_window = ToplevelWindow(self)  # create window if its None or destroyed
        else:
            self.toplevel_window.focus()  # if window exists focus it

if __name__ == "__main__":
    app = App()
    app.mainloop()

The example code results in the following windows:

Bildschirmfoto 2022-03-07 um 22 10 29

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.

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

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

    fg_color = toplevel.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