Saturday, June 22, 2013

Text edit box with scroll bars in Tkinter

Tkinter supplies us a widget (Text) that is very powerful. However, it lacks scroll bars. While you could bundle a scroll bar and hook up events yourself, you could also save yourself some time and use ScrolledText. In Python3 this is available as part of Tkinter, for earlier versions you have to import ScrolledText. (From the discussion here) Thus:
import Tkinter as tki
from ScrolledText import ScrolledText #ScrolledText = tki.scrolledtext for Python 3

class App(object):

  def __init__(self):
    self.root = tki.Tk()

    # create a Text widget with a Scrollbar attached
    #http://stackoverflow.com/questions/13832720/python-tkinter-scrollbar-and-text-field
    #Courtesy Honest Abe (http://stackoverflow.com/users/1217270/honest-abe)
    self.query_win = ScrolledText(self.root, undo=True, width=50, height=5)
    self.query_win.pack(side='top', expand=True, fill='both')

app = App()
app.root.mainloop()

No comments:

Post a Comment