Page List

Friday, August 3, 2012

Tkinter Entry Form Demo

This code is based on the Tcl form.tcl demo. An example of laying out a simple, rolodex type form.



# File: entryform.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 EntryFormDemo(ttk.Frame):
    
    def __init__(self, isapp=True, name='entryformdemo'):
        ttk.Frame.__init__(self, name=name)
        self.pack(expand=Y, fill=BOTH)
        self.master.title('Entry Form Demo')
        self.isapp = isapp
        self._create_widgets()
        
    def _create_widgets(self):
        if self.isapp:
            MsgPanel(self, 
                     ["This window contains a simple form where you ",
                      "can type in the various entries and use tabs ",
                      "to move circularly between the entries."])
            
            SeeDismissPanel(self)
        
        self._create_demo_panel()
        
    def _create_demo_panel(self):
        demoPanel = Frame(self)
        demoPanel.pack(side=TOP, fill=X)
        
        self.flds = []  # entry fields
        lbls = ['Name:', 'Address:', '', '', "Phone:"]
    
        for i in range(5):
            f = ttk.Frame()
            f.pack(in_=demoPanel, side=TOP, fill=X, padx=10)
            
            lbl = ttk.Label(text=lbls[i], width=10)
            e = ttk.Entry(width=20)
            self.flds.append(e) # save reference to field
            
            # on resize, expand entry but not label
            lbl.pack(in_=f, side=LEFT)
            e.pack(in_=f, side=LEFT, expand=Y, fill=X)
            
        self.flds[0].focus_set()

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