📜  禁用 tkinter 小部件的焦点 – Python

📅  最后修改于: 2022-05-13 01:54:58.734000             🧑  作者: Mango

禁用 tkinter 小部件的焦点 – Python

先决条件: Python-Tkinter

在本文中,我们将讨论如何从 Tkinter 框架中的小部件禁用焦点。为了禁用焦点,我们在小部件内使用takefocus选项并将其值指定为 0。

分步实施:

Step1:导入Tkinter并初始化tkinter窗口

Python3
# import tkinter 
import tkinter as tk
  
# initialize a tkinter window
root = tk.Tk()
  
# Creating the geometry with width and height of 300px
root.geometry("300x300")
  
# creating the body loop
root.mainloop()


Python3
import tkinter as tk
   
# initialize a tkinter window
root = tk.Tk()
  
# Creating the geometry with width and height of 300px
root.geometry("300x300")
  
# create a button
btn = tk.Button(root, text ="I am button")
btn.pack()
  
# text widget
txt = tk.Entry(root, width=10)
txt.pack()
  
# create a radio button
rb = tk.Radiobutton(root, text ="I am radio button") 
rb.pack() 
  
# create a check button
cb = tk.Checkbutton(root, text = "I am check button")
cb.pack()
  
  
root.mainloop()


Python3
import tkinter as tk
   
root = tk.Tk()
root.geometry("300x300")
  
# creating button
btn = tk.Button(root, text ="I am button")
btn.pack()
  
# takefocus is set to 0 for disabling the TAB key
# focus in widget
btn_no_focus = tk.Button(root, text ="I am not focused",
                         takefocus = 0,foreground = "red")
btn_no_focus.pack()
  
# created an entry widget with width 10
txt = tk.Entry(root, width = 10)
txt.pack()
  
# creating a entry widget with width 10 and focus is
# 0 hence disabled 
txt = tk.Entry(root, width = 10, takefocus = 0)
txt.pack()
  
# Creating radiobutton
rb = tk.Radiobutton(root, text ="I am radio button") 
rb.pack() 
  
# putting an takefocus=0 for disabling focus
rb_unfocus = tk.Radiobutton(root, text ="I am unfocused radio button", 
                            takefocus = 0,foreground = "red") 
rb_unfocus.pack()
  
# creating the checkbutton
cb = tk.Checkbutton(root, text = "I am check button")
cb.pack()
  
  
cb_unfocused = tk.Checkbutton(root, text = "I am unfocused check button", 
                              takefocus = 0, foreground = "red")
cb_unfocused.pack()
  
root.mainloop()


第 2 步:向应用程序添加一些小部件



代码:

蟒蛇3

import tkinter as tk
   
# initialize a tkinter window
root = tk.Tk()
  
# Creating the geometry with width and height of 300px
root.geometry("300x300")
  
# create a button
btn = tk.Button(root, text ="I am button")
btn.pack()
  
# text widget
txt = tk.Entry(root, width=10)
txt.pack()
  
# create a radio button
rb = tk.Radiobutton(root, text ="I am radio button") 
rb.pack() 
  
# create a check button
cb = tk.Checkbutton(root, text = "I am check button")
cb.pack()
  
  
root.mainloop()

输出:

在这里,我们注意到按下 TAB 键后,我们将注意力集中在每个小部件上。



第 3 步:向我们的程序添加禁用焦点功能。

我们使用takefocus参数来禁用焦点

Syntax: takefocus = 0

# for button
btn = tk.Button(root, text="Button", takefocus=0)

该程序的方法是使用 help takefocus参数禁用小部件的焦点。为此,我们将其值设为 0。其他小部件启用了它们的焦点。

代码:

蟒蛇3

import tkinter as tk
   
root = tk.Tk()
root.geometry("300x300")
  
# creating button
btn = tk.Button(root, text ="I am button")
btn.pack()
  
# takefocus is set to 0 for disabling the TAB key
# focus in widget
btn_no_focus = tk.Button(root, text ="I am not focused",
                         takefocus = 0,foreground = "red")
btn_no_focus.pack()
  
# created an entry widget with width 10
txt = tk.Entry(root, width = 10)
txt.pack()
  
# creating a entry widget with width 10 and focus is
# 0 hence disabled 
txt = tk.Entry(root, width = 10, takefocus = 0)
txt.pack()
  
# Creating radiobutton
rb = tk.Radiobutton(root, text ="I am radio button") 
rb.pack() 
  
# putting an takefocus=0 for disabling focus
rb_unfocus = tk.Radiobutton(root, text ="I am unfocused radio button", 
                            takefocus = 0,foreground = "red") 
rb_unfocus.pack()
  
# creating the checkbutton
cb = tk.Checkbutton(root, text = "I am check button")
cb.pack()
  
  
cb_unfocused = tk.Checkbutton(root, text = "I am unfocused check button", 
                              takefocus = 0, foreground = "red")
cb_unfocused.pack()
  
root.mainloop()

输出:

红色小部件具有takefocus=0 ,其他部件没有此参数。红色仅用于清晰起见,您也可以将其删除。