CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

CTkTopLevel not being focused upon the main window

Open Pgsaisuhas opened this issue 2 years ago • 13 comments
trafficstars

I am trying to add a feature to a password manager app where an 'open file' button opens a CTkTopLevel window displaying all saved passwords. The window is working, but it only stays on top for a second before hiding behind the main window. As a result, I have to move the main window and focus on the CTkTopLevel window to view it.

Pgsaisuhas avatar Apr 22 '23 05:04 Pgsaisuhas

Can you post example code where this appears?

TomSchimansky avatar Apr 23 '23 09:04 TomSchimansky

to fix this issue you can use .lift() like this

TopLevelOpj.after(100, TopLevelOpj.lift)

in the end of TopLevel displaying function

UserJoo9 avatar Apr 23 '23 12:04 UserJoo9

Can you post example code where this appears?

https://github.com/Pgsaisuhas/Password-Manager/blob/master/main.py line 7 to 17 i have tried to edit the same code given in wiki and line 98 i have used the def open_toplevel function

Pgsaisuhas avatar Apr 23 '23 13:04 Pgsaisuhas

Which platform are you using? On macOS its working fine.

TomSchimansky avatar Apr 24 '23 11:04 TomSchimansky

Which platform are you using? On macOS its working fine.

windows, it works fine for the very first time, but when you execute the code again, the error occurs

Pgsaisuhas avatar Apr 25 '23 15:04 Pgsaisuhas

i also facing same issues

mthasneem avatar May 01 '23 06:05 mthasneem

I'm also experiencing this. Here's a button handler that opens a settings window, in which the original window will immediately regain focus:

    def __on_settings_clicked(self):
        window = customtkinter.CTkToplevel(master=self)
        window.geometry("500x300")
        window.title("Settings")
        view = SettingsView(parent=window)
        view.pack(side="top", fill="both", expand=True, padx=20, pady=20)

The workaround mentioned above worked for me by adding the following at the end of this function:

        window.after(100, window.lift) # Workaround for bug where main window takes focus

kelltom avatar May 10 '23 20:05 kelltom

I'm also experiencing this with the basic tutorial code, my py file is launched from vscode powershell terminal, within a python 3.10.6 conda env

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


app = App()
app.mainloop()

Mozoloa avatar May 17 '23 10:05 Mozoloa

to fix this issue you can use .lift() like this

TopLevelOpj.after(100, TopLevelOpj.lift)

in the end of TopLevel displaying function

I just noticed that this was happening on my app, but only on Windows; it doesn't happen on Linux (I don't know about Mac - I'm not posh enough to own one ;o).

I had to add the lift() method, on the line after grab_set(), at the end of my dialog classes. If I added the lift() near the top of the class, it didn't work. This is still ugly though, because you find that the dialog, briefly appears, disappears and then re-appears again.

avalon60 avatar Jun 18 '23 13:06 avalon60

@window.after(100, window.lift) # Workaround for bug where main window takes focus this sure helps thank you so much, ig now we can close this issue

Pgsaisuhas avatar Jun 18 '23 14:06 Pgsaisuhas

I was having the same problem, and the .lift method worked at first, however I added a button on the secondary window and at the moment it's pressed, and executes a command, the main window comes back at being on top

DanRoal avatar Oct 05 '23 10:10 DanRoal

i have faced the same issue on windows and this can be solved by changing the attributes

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)
        
        self.attributes("-topmost",True)  #the code is changed here 


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


app = App()
app.mainloop()

hope this solves the issue have a great day ^^

otakuDev04 avatar Feb 16 '24 18:02 otakuDev04

self.attributes("-topmost",True) is the best solution for this bug

LeFouVolant13 avatar Mar 13 '24 10:03 LeFouVolant13