Tkinter 세 형상 관리자 있습니다.
일반적으로 포장과 그리드를 권장합니다.
당신은 텍스트 위젯 옆에 스크롤을 배치합니다 grid manager's행 및 열 옵션
을 사용할 수 있습니다.
는 텍스트의
yview 방법에
스크롤 위젯의
명령 옵션을 설정합니다.
scrollb = tki.Scrollbar(..., command=txt.yview)
는 스크롤 막대의
가 방법을 설정에
텍스트 위젯의
yscrollcommand 옵션을 설정합니다.
import Tkinter as tki # Tkinter -> tkinter in Python3
class App(object):
def __init__(self):
self.root = tki.Tk()
# create a Frame for the Text and Scrollbar
txt_frm = tki.Frame(self.root, width=600, height=600)
txt_frm.pack(fill="both", expand=True)
# ensure a consistent GUI size
txt_frm.grid_propagate(False)
# implement stretchability
txt_frm.grid_rowconfigure(0, weight=1)
txt_frm.grid_columnconfigure(0, weight=1)
# create a Text widget
self.txt = tki.Text(txt_frm, borderwidth=3, relief="sunken")
self.txt.config(font=("consolas", 12), undo=True, wrap='word')
self.txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)
# create a Scrollbar and associate it with txt
scrollb = tki.Scrollbar(txt_frm, command=self.txt.yview)
scrollb.grid(row=0, column=1, sticky='nsew')
self.txt['yscrollcommand'] = scrollb.set
app = App()
app.root.mainloop()
부분, 작은되는 것은 sticky='nsew'
당신이 약 → here 또는 here을 읽을 수
당신의 스크롤를 해결합니다
txt['yscrollcommand'] = scrollb.set
다음은 작업 예입니다. 당신이 지금 배울 도움이 될 것입니다
뭔가가 같은 부모를 공유하지 않는 한 다른 Tkinter를 위젯만큼 동일한 프로그램 내의 다른 형상 관리자를 사용할 수 있다는 것입니다.
그것은 또한 ScrolledText 모듈 (Python3에 tkinter.scrolledtext 개명)이 있다는 것을 언급 할 가치가있다. 여기에는 복합 위젯 인 ScrolledText (텍스트 & 스크롤 막대)이라는 클래스가 포함되어 있습니다.
# Python 2.7
import Tkinter as tki
from ScrolledText import ScrolledText
class App(object):
def __init__(self):
self.root = tki.Tk()
# create a Text widget with a Scrollbar attached
self.txt = ScrolledText(self.root, undo=True)
self.txt['font'] = ('consolas', '12')
self.txt.pack(expand=True, fill='both')
app = App()
app.root.mainloop()
감사합니다. 덕분에 많은 도움이되었습니다. 정말 감사. – jwebster
"대개 장소 추천"을 "* 항상 * 장소 추천"으로 변경합니다. 장소는 유용하지만 드문 경우에만 해당됩니다. –
@honestabe 어떻게 답변을 여러 섹션으로 나눌 수 있습니까? – xxmbabanexx