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

การเปลี่ยน Mouse Cursor ใน Tkinter

Tkinter เป็นไลบรารี Python แบบ GUI-Based ใช้พัฒนาแอปพลิเคชัน Functional และ GUI ต่างๆ มีฟังก์ชันและเมธอดมากมายที่ให้ Extensibility และฟีเจอร์ต่างๆ

ในบทความนี้ เราจะดูวิธีเปลี่ยน Mouse Cursor ขณะ Hover บนปุ่มใน Tkinter Frame โดยใช้ Property Cursor มี Cursor Maps มากมายในไลบรารีปุ่มของ Tkinter ที่ให้ Visual ต่างๆ สำหรับ End-user บางตัวในไลบรารี ได้แก่:

"arrow", "circle", "clock", "cross", "dotbox", "exchange", "fleur", "heart", "man", "mouse", "pirate", "plus", "shuttle", "sizing", "spider", "spraycan", "star", "target", "tcross", "trek", "watch"

ลองสร้างปุ่มบางปุ่มแล้ว Apply Cursor บางตัวบน Mouse Pointer

ตัวอย่าง

from tkinter import *

# Create an instance of window or frame
win = Tk()

# Set the geometry
win.geometry("700x600")
win.resizable(0, 0)
win.config(cursor="fleur")

# Let us create a text label
Label(win, text="Hover on each of these buttons", font=('Poppins', 20)).pack(pady=20)

# Create some buttons with cursor property
b1 = Button(win, text="Star", cursor="star")
b1.pack(pady=20)

b2 = Button(win, text="Arrow", cursor="arrow")
b2.pack(pady=20)

b3 = Button(win, text="Circle", cursor="circle")
b3.pack(pady=20)

b4 = Button(win, text="Clock", cursor="clock")
b4.pack(pady=20)

b5 = Button(win, text="Heart", cursor="heart")
b5.pack(pady=20)

b6 = Button(win, text="Man", cursor="man")
b6.pack(pady=20)

b7 = Button(win, text="Mouse", cursor="mouse")
b7.pack(pady=20)

# Keep Running the window
win.mainloop()

ผลลัพธ์

การรันโค้ดข้างต้นจะสร้างปุ่มต่างๆ พร้อมรูปทรง Mouse Pointer ที่ต่างกัน

การเปลี่ยน Mouse Cursor ใน Tkinter