Sunday, July 29, 2012

Tkinter Entry with scrollbar Demo

This code is based on the Tcl entry2.tcl demo. It displays 3 basic, single line entry fields, each with their own horizontal scrollbar.



# File: entrywithscroll.py
# References:
#    http://infohost.nmt.edu/tcc/help/pubs/tkinter//entry.html
#    http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_entry.htm

from tkinter import *
from tkinter import ttk

from demopanels import MsgPanel, SeeDismissPanel

class EntryNoScrollDemo(ttk.Frame):
    
    def __init__(self, isapp=True, name='entrywithscrolldemo'):
        ttk.Frame.__init__(self, name=name)
        self.pack(expand=Y, fill=BOTH)
        self.master.title('Entry with scroll Demo')
        self.isapp = isapp
        self._create_widgets()
        
    def _create_widgets(self):
        if self.isapp:
            MsgPanel(self, 
                     ["Three different entries are displayed below.  You can add characters by pointing, ",
                      "clicking and typing.  The normal Motif editing characters are supported, along ",
                      "with many Emacs bindings.\n\n",
                      "For example, Backspace and Control-h delete the character to the left of the ",
                      "insertion cursor and Delete and Control-d delete the chararacter to the right ",
                      "of the insertion cursor\n\n", 
                      "For entries that are too large to fit in the window all at once, you can scan ",
                      "through the entries by dragging with the left mouse button pressed or ",
                      "use the entry's scrollbar."])
            
            SeeDismissPanel(self)
        
        self._create_demo_panel()
        
    def _create_demo_panel(self):
        demoPanel = ttk.Frame(self)
        demoPanel.pack(side=TOP, fill=BOTH, expand=Y)
                
        longTxt = ["This entry contains a long value, ",
                   "much to long to fit into the window ",
                   "at one time; so long in fact, that ",
                   "you'll have to scan and scroll to ",
                   "get to the end."]
                          
        # entry fields
        e1 = ttk.Entry(demoPanel)
        e2 = ttk.Entry(demoPanel)
        e3 = ttk.Entry(demoPanel)
        
        # scrollbars
        sb1 = ttk.Scrollbar(demoPanel, orient=HORIZONTAL, command=e1.xview)
        sb2 = ttk.Scrollbar(demoPanel, orient=HORIZONTAL, command=e2.xview)
        sb3 = ttk.Scrollbar(demoPanel, orient=HORIZONTAL, command=e3.xview)
        
        # join entry fields and scrollbars
        e1['xscroll'] = sb1.set
        e2['xscroll'] = sb2.set
        e3['xscroll'] = sb3.set
        

        # add some text to the fields
        e1.insert(END, 'Initial value')
        e2.insert(END, ''.join(longTxt))
        
        # pack and show
        e1.pack(side=TOP, pady=5, padx=10, fill=X)
        sb1.pack(side=TOP, pady=5, padx=10, fill=X)
        
        e2.pack(side=TOP, pady=5, padx=10, fill=X)
        sb2.pack(side=TOP, pady=5, padx=10, fill=X)
        
        e3.pack(side=TOP, pady=5, padx=10, fill=X)
        sb3.pack(side=TOP, pady=5, padx=10, fill=X)
        

if __name__ == '__main__':
    EntryNoScrollDemo().mainloop()