โฟกัสใช้เพื่ออ้างถึงวิดเจ็ตหรือหน้าต่างซึ่งกำลังรับอินพุต วิดเจ็ตสามารถใช้เพื่อจำกัดการใช้การเคลื่อนไหวของเมาส์ จับโฟกัส และการกดแป้นพิมพ์นอกขอบเขต อย่างไรก็ตาม หากเราต้องการเน้นวิดเจ็ตเพื่อให้เปิดใช้งานสำหรับการป้อนข้อมูล เราก็สามารถใช้ focus.set() กระบวนการ. โฟกัส() บางครั้งเรียกว่า focus_set() .
focus_set() โฟกัสที่วิดเจ็ตเมื่อหน้าต่างหรือวิดเจ็ตได้รับการโฟกัส
ตัวอย่าง
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")
# Define a function to set the focus
def set_focus():
entry.focus_set()
# Create an Entry widget
entry=Entry(win, width=35)
entry.pack()
# Create a Button to get the focus on any widget
ttk.Button(win, text="Set Focus", command=set_focus).pack()
win.mainloop() ผลลัพธ์
การรันโค้ดด้านบนจะแสดงหน้าต่างที่มีปุ่มและวิดเจ็ตรายการ เมื่อเราคลิกปุ่ม มันจะกำหนดโฟกัสไปที่ Entry Widget
