如何从 Tkinter 窗口小部件移除焦点?
要在 Tkinter 程序执行期间激活特定窗口小部件的焦点,我们可以使用focus_set()方法。添加焦点会在窗口小部件周围创建一条灰色线,并向用户展示该窗口小部件。
在某些情况下,我们需要从所需的窗口小部件中移除焦点。
这可以通过移除focus_set()属性或将焦点从一个窗口小部件切换到另一个窗口小部件来实现。
示例
#Import the required Libraries
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
#Create an instance of Tkinter frame
win = Tk()
#Set the geometry of Tkinter Frame
win.geometry("750x250")
#Define a function to show the message
def logged_in():
messagebox.showinfo("Message", "Successfully Logged In!")
win.destroy()
#Define a Label widget
Label(win, text= "Login to the System", font=('Aerial', 14, 'bold')).pack(pady=15)
#Create an entry widget
Label(win, text="Enter Username").pack()
username= Entry(win, width= 20)
username.pack()
Label(win, text= "Enter passowrd").pack()
password= Entry(win, show="*", width= 20)
password.pack()
password.focus_set()
#Add a Bottom widget
button=ttk.Button(win, text="Login", command= logged_in)
button.pack(pady=13)
#Create a Button widget
win.mainloop()输出
在上面的代码片段中,焦点当前设置为密码输入框,可以通过向另一个窗口小部件添加focus_set() 来取消设置或切换它。
广告
数据结构
网络技术
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP