如何在 Tkinter 中设置 Entry 窗口小部件的焦点?


假设应用程序中存在一些窗口小部件,而我们必须重点关注某个特定窗口小部件。利用focus_set()方法,我们可以在任何窗口小部件上激活焦点,以便在执行应用程序时优先处理它们。

示例

#Import tkinter library
from tkinter import *

#Create an instance of tkinter frame
win = Tk()

#Set the geometry of tkinter window
win.geometry("750x250")

#Create an Entry Widget
entry= Entry(win, width= 25)
entry.insert(0,"ttk Entry widget")
entry.pack()

#Set the focus to Entry widget
entry.focus_set()
win.mainloop()

输出

当我们运行程序时,它会显示一个窗口,其中有一个激活的条目窗口小部件。

更新日期:16/04/2021

7K+ 查看

事业开启引擎

完成课程获取认证

开始
广告
© . All rights reserved.