Tkinter Cursors
2024. 3. 12. 19:30ㆍGUI/tkinter
요약 : 이 튜토리얼에서는 Tkinter 애플리케이션에서 위젯에 대한 커서를 설정하는 방법을 배웁니다.
루트 창의 커서 변경
루트 창에는 커서가 두 개만 있습니다.
- Normal 커서
- Busy 커서
normal 커서의 값은 ""이고 사용 중인busy 커서의 값은 "watch" 입니다.
다음 프로그램은 루트 창의 커서를 정상에서 사용 중으로 변경하는 방법을 보여줍니다.
In [1]:
import tkinter as tk
root = tk.Tk()
root.geometry("300x300")
root.config(cursor="watch")
root.mainloop()
작동 방식.
- 먼저 루트 창의 너비와 높이를 300×300으로 설정합니다.
- 둘째, 커서 매개변수를 사용하여 커서를 사용 중으로 변경합니다.
루트 창 영역의 커서를 변경하려면 이벤트를 사용하고 커서의 x(및/또는 y) 좌표를 추적할 수 있습니다. 예를 들어:
In [2]:
import tkinter as tk
root = tk.Tk()
root.geometry("300x300")
def change_cursor(event):
if event.x in range(100, 300):
root.config(cursor="watch")
else:
root.config(cursor="")
root.bind("<Motion>", change_cursor)
root.mainloop()
이 예에서는 마우스가 x 좌표가 100에서 300까지에서 시작되면 커서가 사용 중으로 변경됩니다.
위젯 커서 변경
모든 ttk 위젯 에는 마우스가 위젯 위에 있을 때 커서를 변경할 수 있는 커서 매개변수가 있습니다.
예를 들어, 버튼의 커서를 변경하려면, 다음과 같이 커서 매개변수를 사용하여 커서 이름을 설정할 수 있습니다.
from tkinter import ttk
...
button = ttk.Button(cursor=cursor_name)
커서 이름은 다음 값 중 하나일 수 있습니다.
- arrow
- based_arrow_down
- based_arrow_up
- boat
- bogosity
- bottom_left_corner
- bottom_right_corner
- bottom_side
- bottom_tee
- box_spiral
- center_ptr
- circle
- clock
- coffee_mug
- cross
- cross_reverse
- crosshair
- diamond_cross
- dot
- dotbox
- double_arrow
- draft_large
- draft_small
- draped_box
- exchange
- fleur
- gobbler
- gumby
- hand1
- hand2
- heart
- icon
- iron_cross
- left_ptr
- left_side
- left_tee
- leftbutton
- ll_angle
- lr_angle
- man
- middlebutton
- mouse
- pencil
- pirate
- plus
- question_arrow
- right_ptr
- right_side
- right_tee
- rightbutton
- rtl_logo
- sailboat
- sb_down_arrow
- sb_h_double_arrow
- sb_left_arrow
- sb_right_arrow
- sb_up_arrow
- sb_v_double_arrow
- shuttle
- sizing
- spider
- spraycan
- star
- target
- tcross
- top_left_arrow
- top_left_corner
- top_right_corner
- top_side
- top_tee
- trek
- ul_angle
- umbrella
- ur_angle
- watch
- xterm
- X_cursor
루트 창의 커서를 변경하려면 다음을 수행할 수 있습니다.
- 먼저, 새로운 Frame을 생성하세요.
- 둘째, 루트 창에 놓고 100% 확장합니다.
- 셋째, 프레임에 커서를 놓습니다.
다음 프로그램을 사용하면 콤보 상자에서 커서를 선택하여 커서를 변경할 수 있습니다.
In [4]:
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
# config the root window
root.geometry('600x400')
root.resizable(False, False)
root.title('Tkinter Cursors')
frame = ttk.Frame(root)
# label
label = ttk.Label(frame, text="Cursor:")
label.pack(fill=tk.X, padx=5, pady=5)
# cursor list
selected_cursor = tk.StringVar()
cursor_list = ttk.Combobox(frame, textvariable=selected_cursor, cursor='arrow')
cursors = ['arrow', 'man', 'based_arrow_down', 'middlebutton', 'based_arrow_up', 'mouse', 'boat', 'pencil', 'bogosity', 'pirate', 'bottom_left_corner', 'plus', 'bottom_right_corner', 'question_arrow', 'bottom_side', 'right_ptr', 'bottom_tee', 'right_side', 'box_spiral', 'right_tee', 'center_ptr', 'rightbutton', 'circle', 'rtl_logo', 'clock', 'sailboat', 'coffee_mug', 'sb_down_arrow', 'cross', 'sb_h_double_arrow', 'cross_reverse', 'sb_left_arrow', 'crosshair', 'sb_right_arrow', 'diamond_cross',
'sb_up_arrow', 'dot', 'sb_v_double_arrow', 'dotbox', 'shuttle', 'double_arrow', 'sizing', 'draft_large', 'spider', 'draft_small', 'spraycan', 'draped_box', 'star', 'exchange', 'target', 'fleur', 'tcross', 'gobbler', 'top_left_arrow', 'gumby', 'top_left_corner', 'hand1', 'top_right_corner', 'hand2', 'top_side', 'heart', 'top_tee', 'icon', 'trek', 'iron_cross', 'ul_angle', 'left_ptr', 'umbrella', 'left_side', 'ur_angle', 'left_tee', 'watch', 'leftbutton', 'xterm', 'll_angle', 'X_cursor', 'lr_angle']
cursor_list['values'] = cursors
cursor_list['state'] = 'readonly'
cursor_list.pack(fill=tk.X, padx=5, pady=5)
frame.pack(expand=True, fill=tk.BOTH)
# bind the selected value changes
def cursor_changed(event):
frame.config(cursor=selected_cursor.get())
cursor_list.bind('<<ComboboxSelected>>', cursor_changed)
root.mainloop()
요약
- 루트 창에는 일반("") 및 사용 중("watch")이라는 두 개의 커서만 있습니다.
- 위젯에는 고정된 이름을 가진 많은 커서가 있습니다.
- cursor 매개변수를 사용하여 루트 창이나 위젯의 커서를 변경합니다.
'GUI > tkinter' 카테고리의 다른 글
Tkinter Object-Oriented Frames (0) | 2024.03.14 |
---|---|
Tkinter Object-Oriented Window (0) | 2024.03.13 |
Tkinter Canvas (0) | 2024.03.11 |
Tkinter Treeview (0) | 2024.03.10 |
Tkinter Notebook (0) | 2024.03.09 |