CustomTkinter
CustomTkinter copied to clipboard
CTkTopLevel not being focused upon the main window
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.
Can you post example code where this appears?
to fix this issue you can use .lift() like this
TopLevelOpj.after(100, TopLevelOpj.lift)
in the end of TopLevel displaying function
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
Which platform are you using? On macOS its working fine.
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
i also facing same issues
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
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()
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.
@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
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
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 ^^
self.attributes("-topmost",True) is the best solution for this bug