让我们假设应用程序中存在一些小部件,因此我们必须专注于特定的小部件。通过使用该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()输出结果
当我们运行我们的程序时,它会显示一个带有活动条目小部件的窗口。