Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ความแตกต่างระหว่าง focus และ focus_set ใน Tkinter


Focus ใช้หมายถึง widget หรือหน้าต่างที่กำลังรับอินพุตอยู่ Widget สามารถใช้จำกัดการเคลื่อนไหวของเมาส์ grab focus และ keystrokes นอกขอบเขต อย่างไรก็ตาม หากต้องการ focus widget ให้เปิดใช้งานสำหรับอินพุต ให้ใช้เมธอด focus_set() focus() บางครั้งอ้างถึง focus_set()

focus_set() จะ focus widget เมื่อหน้าต่างหรือ widget ได้รับ focus

ตัวอย่าง

# 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 เมื่อคลิกปุ่ม จะตั้ง focus ให้ Entry Widget

ความแตกต่างระหว่าง focus และ focus_set ใน Tkinter