CTkToplevel is brought to the front and then goes back

228 Views Asked by At

I am encountering a really weird problem in customtkinter.

I created a CTkToplevel window and brought it to the front using lift() method. It got to the front, and immediately after was sent behind my root window.

Simplified Code Using customtkinter:

import customtkinter as ctk

class Win(ctk.CTkToplevel):
    def __init__(self, parent):
        super().__init__(parent)
        ctk.CTkLabel(self, text="Top window").pack()


def show_win():
    global top_win
    if not top_win or not top_win.winfo_exists():
        top_win = Win(root)
    top_win.lift()
    top_win.focus_force()

root = ctk.CTk()
root.geometry('500x500')
top_win:Win = None
ctk.CTkButton(root, text="Show Top Window", command=show_win).pack()

root.mainloop()

I converted the code to tkinter and it worked perfectly.

Using Standard tkinter

import tkinter as tk

class Win(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        tk.Label(self, text="Top window").pack()

def show_win():
    global top_win
    if not top_win or not top_win.winfo_exists():
        top_win = Win(root)
    top_win.lift()
    top_win.focus_force()

root = tk.Tk()
root.geometry('500x500')
top_win:Win = None
tk.Button(root, text="Show Top Window", command=show_win).pack()

root.mainloop()

The code worked well in customtkinter once I disabled the root window:

Using -disabled

import customtkinter as ctk

class Win(ctk.CTkToplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.master.attributes('-disabled', True)
        ctk.CTkLabel(self, text="Top window").pack()
    
    def destroy(self):
        self.master.attributes('-disabled', False)
        return super().destroy()

def show_win():
    global top_win
    if not top_win or not top_win.winfo_exists():
        top_win = Win(root)
    top_win.lift()
    top_win.focus_force()

root = ctk.CTk()
root.geometry('500x500')
top_win:Win = None
ctk.CTkButton(root, text="Show Top Window", command=show_win).pack()

root.mainloop()

The problem is that I need the root window enabled.


I also tried to use the topmost attribute, but it didn't work as well:

Using -topmost

class Win(ctk.CTkToplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.attributes('-topmost', True)
        self.attributes('-topmost', False)
        ctk.CTkLabel(self, text="Top window").pack()

How can I ensure that my CTkToplevel window stays on top without disabling the root window?

(I want it to still go back when the root window is focused)

0

There are 0 best solutions below