TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
10.8k stars 1.03k forks source link

focus_set() does not work #2379

Closed sim2511 closed 2 months ago

sim2511 commented 2 months ago

Hello,

I don't understand why focus_set() method does not work here :

import customtkinter

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.default_search=""
        self.login_frame = customtkinter.CTkFrame(master=self, width=400, height=300)
        self.login_frame.grid(row=0, column=0, padx=20, pady=20)

        self.filter_entry = customtkinter.CTkEntry(master=self.login_frame,width=300)
        self.filter_entry.grid(row=3, column=1, pady=5, padx=5, sticky="ew")
        self.filter_entry.insert(0, self.default_search)
        self.filter_entry.focus_set()

if __name__ == "__main__":
    app = App()
    app.mainloop()
sim2511 commented 2 months ago

Here a solution which works, hope it can helps :

import customtkinter

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.default_search=""
        self.login_frame = customtkinter.CTkFrame(master=self, width=400, height=300)
        self.login_frame.grid(row=0, column=0, padx=20, pady=20)

        self.filter_entry = customtkinter.CTkEntry(master=self.login_frame,width=300)
        self.filter_entry.grid(row=3, column=1, pady=5, padx=5, sticky="ew")
        self.filter_entry.insert(0, self.default_search)

        self.after(100, lambda: self.filter_entry.focus_set())

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