I am probably over-thinking this, but for some reason I can't seem to figure this out. I am trying to attach a scrollbar to my Text field and have been unable to do so. Here is the segment of code:
self.scroller = Scrollbar(self.root)
self.outputArea = Text(self.root, height=26, width=100)
self.outputArea.config(state=DISABLED, yscrollcommand = self.scroller.set)
You can use the grid manager's row and column options
to position the Scrollbar next to the Text widget.
Set the Scrollbar widget's command option to the Text's yview method.
scrollb = tki.Scrollbar(..., command=txt.yview)
Set the Text widget's yscrollcommand option to the Scrollbar's set method.
txt['yscrollcommand'] = scrollb.set
Here's a working example:
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()
Something that will be helpful for you to learn right now is that different Tkinter widgets can use different geometry managers within the same program as long as they do not share the same parent.
It's also worth mentioning that there is the ScrolledText module (renamed tkinter.scrolledtext in Python3). It contains a class that is also called ScrolledText which is a compound widget (Text & Scrollbar).
# 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()