Tkinter Scrollbar
2024. 2. 26. 18:19ㆍGUI/tkinter
요약 : 이 튜토리얼에서는 Tkinter Scrollbar 위젯과 이를 스크롤 가능한 위젯에 연결하는 방법에 대해 배웁니다.
Tkinter 스크롤바 위젯 소개
스크롤바를 사용하면 일반적으로 사용 가능한 공간보다 내용이 더 큰 다른 위젯의 모든 부분을 볼 수 있습니다.
Tkinter 스크롤바 위젯은 Text 및 Listbox 와 같은 다른 위젯의 일부가 아닙니다 . 대신 스크롤바는 독립적인 위젯입니다.
스크롤바 위젯을 사용하려면 다음을 수행해야 합니다.
- 먼저 스크롤바 위젯을 만듭니다.
- 둘째, 스크롤 막대를 스크롤 가능한 위젯과 연결합니다.
다음은 ttk.Scrollbar 생성자를 사용하여 스크롤바 위젯을 만드는 방법을 보여줍니다.
scrollbar = ttk.Scrollbar(
container,
orient='vertical',
command=widget.yview
)
이 구문에서는:
- 스크롤 막대 가 위치한 container창이나 프레임 입니다.
- orient 인수는 스크롤 막대를 가로 또는 세로로 스크롤해야 하는지 여부를 지정합니다.
- command 인수를 사용하면 스크롤 막대 위젯이 스크롤 가능한 위젯과 통신할 수 있습니다.
또한 스크롤 가능한 위젯은 현재 표시되는 전체 콘텐츠 영역의 비율에 대해 스크롤 막대에 다시 전달해야 합니다.
모든 스크롤 가능한 위젯에는 yscrollcommand 및/또는 xscrollcommand 옵션이 있습니다. 그리고 여기에 scrollbar.set 메서드를 할당할 수 있습니다.
widget['yscrollcommand'] = scrollbar.set
Tkinter 스크롤바 위젯 예`
Text 위젯은 여러 유형의 스크롤 가능한 위젯 중 하나입니다. 다음 프로그램은 Text와 Scrollbar 위젯으로 구성된 간단한 사용자 인터페이스를 보여줍니다.
In [2]:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.resizable(False, False)
root.title("Scrollbar Widget Example")
# apply the grid layout
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
# create the text widget
text = tk.Text(root, height=10)
text.grid(row=0, column=0, sticky=tk.EW)
# create a scrollbar widget and set its command to the text widget
scrollbar = ttk.Scrollbar(root, orient='vertical', command=text.yview)
scrollbar.grid(row=0, column=1, sticky=tk.NS)
# communicate back to the scrollbar
text['yscrollcommand'] = scrollbar.set
# add sample text to the text widget to show the screen
for i in range(1,50):
position = f'{i}.0'
text.insert(position,f'Line {i}\n');
root.mainloop()

요약
- ttk.Scrollbar(orient, command)을 사용하여 스크롤바를 만듭니다.
- 방향은 'vertical' 또는 'horizontal' 입니다.
- 명령은 스크롤 막대에 연결되는 스크롤 가능한 위젯의 속성 xview 이거나 yview 속성일 수 있습니다.
- 스크롤 막대에 연결되도록 스크롤 가능 위젯의 yscrollcommand 속성을 설정합니다.
'GUI > tkinter' 카테고리의 다른 글
Tkinter Separator (0) | 2024.02.28 |
---|---|
Tkinter ScrolledText (1) | 2024.02.27 |
Tkinter Text (0) | 2024.02.25 |
Tkinter Frame (1) | 2024.02.24 |
Tkinter Place (0) | 2024.02.23 |