OOP tkinter:如何将焦点(并添加文本)设置为条目?
Posted
技术标签:
【中文标题】OOP tkinter:如何将焦点(并添加文本)设置为条目?【英文标题】:OOP tkinter: How to set focus (and add text) to entry? 【发布时间】:2015-10-11 23:01:46 【问题描述】:重构工作过程代码后,我无法集中精力工作。因为它在我可能会弄乱课程之前就起作用了。
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()
我的目标:点击“Page 1”按钮后,焦点应该在条目上(其中应该显示文本“a default value”)。
【问题讨论】:
【参考方案1】:问题是您在myApp
类中初始化了StartPage
和PageOne
。
因此,PageOne
的 __init__
方法在框架处于活动状态之前被初始化。
解决方案是在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()
【讨论】:
感谢您的解决方案,尤其是您的解释!以上是关于OOP tkinter:如何将焦点(并添加文本)设置为条目?的主要内容,如果未能解决你的问题,请参考以下文章
wpf 焦点定位到 文本框开头? 2:如何按Tab,让焦点不进入第3个文本框,意思是在前两个文本框中切换
Flex - 如何显示可滚动文本并捕获单击/焦点事件以允许添加新文本?