重构工作程序代码后,我无法将重点放在工作上。因为它可能之前我可能搞乱了类。
import tkinter as tk
from tkinter import ttk
LARGE_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)
label = ttk.Label(self, text="StartPage", font=LARGE_FONT)
label.pack(padx=10, pady=10)
but_1 = ttk.Button(self, text="Page 1",
command=lambda: controller.show_frame(PageOne))
but_1.pack(padx=10, pady=10)
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)
useranswer = tk.StringVar()
entry_answ = ttk.Entry(self, textvariable=useranswer, font=ANSWER_FONT, justify=tk.CENTER)
entry_answ.pack(fill=tk.X)
useranswer.set("a default value") ## These two lines
entry_answ.focus_set() ## just won\'t work
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)
app = myApp()
app.geometry("300x150+300+300")
app.mainloop()
我的目标:点击“第1页”按钮后,焦点应该在条目上(并且文本中应显示“默认值”)。
答案 0 :(得分:5)
问题是您在StartPage
班级初始化PageOne
和myApp
。
因此,__init__
PageOne
方法会在帧处于活动状态之前初始化。
解决方法是在PageOne
创建一个新方法,用于设置焦点和文本变量,只有当您单击PageOne
按钮时,才会调用set方法。
这是代码:
import tkinter as tk
from tkinter import ttk
LARGE_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()