-
Notifications
You must be signed in to change notification settings - Fork 1.1k
CTkToplevel
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.
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:
argument | value |
---|---|
fg_color | window background color, tuple: (light_color, dark_color) or single color |
-
All attributes can be configured and updated.
toplevel.configure(fg_color=new_fg_color)
-
Pass attribute name as string and get current value of attribute.
fg_color = toplevel.cget("fg_color") ...
-
Set title of window.
-
Set geometry and positions of the window like this:
"<width>x<height>"
or"<width>x<height>+<x_pos>+<y_pos>"
-
Set minimal window size.
-
Set max window size.
-
Define, if width and/or height should be resizablee with bool values.
-
Execute command after milliseconds without blocking the main loop.
-
Hide window and icon. Restore it with .deiconify().
-
Iconifies the window. Restore it with .deiconify().
-
Deiconify the window.
-
Returns the window state:
'normal', 'iconic' or 'withdrawn'
CustomTkinter by Tom Schimansky 2022
The Github Wiki is outdated, the new documentation can be found at: