OOP tkinter: How to set focus (and add text) to entry?
The problem is that you initialize the StartPage
and the PageOne
at the myApp
class.
Therefore, the __init__
method of PageOne
is initialized before the frame is active.
The solution is to create a new method at the PageOne
that will set the focus and the textvariable, and only when you click the PageOne
button you will call the set method.
This is the code:
import tkinter as tkfrom tkinter import ttkLARGE_FONT = ("Verdana", 14)ANSWER_FONT = ("Verdana", 20, "bold")class myApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) container = ttk.Frame(self) container.pack(side=tk.TOP, fill=tk.BOTH, expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in [StartPage, PageOne]: frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(StartPage) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise()class StartPage(ttk.Frame): def __init__(self, parent, controller): ttk.Frame.__init__(self, parent) self.controller = controller label = ttk.Label(self, text="StartPage", font=LARGE_FONT) label.pack(padx=10, pady=10) but_1 = ttk.Button(self, text="Page 1", command=self.page_one_command) but_1.pack(padx=10, pady=10) def page_one_command(self): self.controller.show_frame(PageOne) self.controller.frames[PageOne].set_unanswer() # Only after calling the show_frame you call the set method that will set the focus and the textvariable.class PageOne(ttk.Frame): def __init__(self, parent, controller): ttk.Frame.__init__(self, parent) label = ttk.Label(self, text="Page One", font=LARGE_FONT) label.pack(pady=10, padx=10) frame_answ = ttk.Frame(self) frame_answ.pack(side=tk.TOP, fill=tk.X) self.useranswer = tk.StringVar() self.entry_answ = ttk.Entry(self, textvariable=self.useranswer, font=ANSWER_FONT, justify=tk.CENTER) self.entry_answ.pack(fill=tk.X) frame_button = ttk.Frame(self) frame_button.pack(padx=10, pady=10) but_next = ttk.Button(frame_button, text="back", state=tk.NORMAL, command=lambda: controller.show_frame(StartPage)) but_next.pack(side=tk.LEFT) def set_unanswer(self): self.useranswer.set("a default value") self.entry_answ.focus_set() app = myApp()app.geometry("300x150+300+300")app.mainloop()