On 6/24/05, Adam Cripps <[EMAIL PROTECTED]> wrote:
On 6/24/05, Michael P. Reilly <[EMAIL PROTECTED]> wrote:
> On 6/24/05, Adam Cripps <[EMAIL PROTECTED]> wrote:
> > I hadn't thought about a scrollbar - that would be very useful,
> > although doesn't add to the management side of the statement (i.e.
> > organising them according to subjects).
> >
> > The user can load a text file and adapt that - so they don't have to
> > enter them by hand if they have them in a .txt file. Typing them in by
> > hand once is better than typing them in by hand for every child that
> > you have in your class, which is the whole aim of the software.
> >
>
>  I could try to whip up an example a little later if you like.  I probably
> wouldn't be large, but it could demonstrate tabbed frames with scrolling.
>
>   -Arcege
> --

Michael,

That would be much appreciated.

Thanks
Adam

Here you go, Adam.  It's not full and complete, but it's working and you can customize it how you like.
  -Michael
--
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
from Tkinter import *

class TabbedFrame(Frame):
    """Create a frame with two areas, one is a small bar at the top where tabs
will be placed, like a menu.  The second area will be one container panel.
When a tab is pressed, the panel will display the contents of a selected
frame (created with the new() method).  Each tab has a unique given name that
identifies it.

These are the public methods:
    new(tab_name)       -> frame
      create a new tab and panel frame, return that frame
    remove(tab_name)
      destroy the tab and associated panel frame
    has_tab(tab_name)   -> boolean
      does a tab exist with this name?
    select(tab_name)
      switch to selected tab
"""
    def __init__(self, master):
        Frame.__init__(self, master)

        self.tabs = {}
        self.current_tab = None
        self._create_widgets()

    def _create_widgets(self):
        self.tabframe = Frame(self, height=25, relief=SUNKEN, bg='grey25', bd=2)
        self.tabframe.pack(side=TOP, fill=X, expand=YES)

        self.baseframe = Frame(self, bg='white', bd=2)
        self.baseframe.pack(side=BOTTOM, fill=BOTH, expand=YES)

    def _add_tab(self, name, button, frame):
        self.tabs[name] = (button, frame)

    def new(self, name):
        if self.has_tab(name):
            raise ValueError(name)

        button = Button(self.tabframe, relief=FLAT, text=str(name),
                        bg='grey50',
                        command=lambda n=name, s=self: s.select(n))
        button.pack(side=LEFT, pady=1, padx=1)

        frame = Frame(self.baseframe)

        self._add_tab(name, button, frame)
        self.select(name)
        return frame

    def remove(self, name):
        try:
            button, frame = self.tabs[name]
        except:
            raise ValueError(name)
        if self.current_tag == name:
            frame.forget()
            button.forget()
            self.current_tag = None
        frame.destroy()
        button.destroy()
        del self.tabs[name]

    def has_tab(self, name):
        return self.tabs.has_key(name)
    
    def select(self, name):
        if self.current_tab:
            button, frame = self.tabs[self.current_tab]
            button['bg'] = 'grey50'
            frame.forget()
        button, frame = self.tabs[name]
        button['bg'] = 'grey75'
        frame.pack(fill=BOTH, expand=YES)
        self.current_tab = name

def Adam_Report(tabbedframe):
    # Reproduce something like Adam's teacher report
    frame = tabbedframe.new('subject')
    topframe = Frame(frame)
    nameframe = Frame(topframe)
    firstnameframe = Frame(nameframe)
    Label(firstnameframe, text="First name: ").pack(side=LEFT)
    Entry(firstnameframe).pack(side=LEFT)
    firstnameframe.pack(side=TOP)
    lastnameframe = Frame(nameframe)
    Label(lastnameframe, text="Last name: ").pack(side=LEFT)
    Entry(lastnameframe).pack(side=LEFT)
    lastnameframe.pack(side=TOP)
    nameframe.pack(side=LEFT, fill=BOTH, expand=YES)

    sexframe = Frame(topframe)
    Label(sexframe, text="Sex: ").pack(side=LEFT)
    sexm_r = Radiobutton(sexframe, value="male", text="male")
    sexf_r = Radiobutton(sexframe, value="female", text="female")
    sexm_r.pack(side=LEFT)
    sexf_r.pack(side=LEFT)
    sexframe.pack(side=RIGHT, fill=BOTH, expand=YES)

    topframe.pack(side=TOP, fill=X, expand=YES)

    splitbottom = Frame(frame)

    NoStatements = 16
    leftsplitframe = Frame(splitbottom) # has statementframe and Scrollbar
    sb = Scrollbar(leftsplitframe, orient=VERTICAL)
    canv = Canvas(leftsplitframe, yscrollcommand=sb, height=200, width=150)
    sb.config(command=canv.yview)
    statementframe = Frame(leftsplitframe)
    canv.create_window(75, 0, window=statementframe)
    for i in range(NoStatements):
        label = Label(statementframe, text="Statement %s" % i)
        label.grid(column=0, row=i)

        entry = Entry(statementframe)
        entry.grid(column=1, row=i)

        button = Button(statementframe, text=">>> ")
        button.grid(column=2, row=i)
    sb.pack(side=RIGHT, fill=Y, expand=YES)
    canv.pack(side=LEFT, fill=BOTH, expand=YES)
    leftsplitframe.pack(side=LEFT, fill=BOTH, expand=YES)

    textframe = Frame(splitbottom)
    Label(textframe, text="Text: ").pack(side=TOP)
    text_t = Text(textframe, width=60, height=30)
    text_t.pack(padx=7, pady=7, fill=BOTH, expand=YES)
    textframe.pack(side=RIGHT, fill=Y, expand=YES)
    
    splitbottom.pack(fill=BOTH, expand=YES)

    return frame

def test():
    root = Tk()
    #root.geometry('800x400')
    tf = TabbedFrame(root)
    tf.pack(fill=BOTH, expand=YES)

    f1 = tf.new('test')
    b = Button(f1, text='Continue', command=root.quit)
    b.pack()
    b = Button(f1, text='Quit')
    b.pack()
    Label(f1, text='Name').pack(side=LEFT)
    Entry(f1).pack(side=LEFT)

    f2 = tf.new('Try')
    sb = Scrollbar(f2, orient=VERTICAL)
    lb = Listbox(f2, yscrollcommand=sb.set)
    for i in range(100):
        lb.insert(END, str(i))
    sb.config(command=lb.yview)
    sb.pack(side=RIGHT, fill=Y, expand=YES)
    lb.pack(fill=BOTH, expand=YES)

    Adam_Report(tf)
    
    root.mainloop()

if __name__ == '__main__':
    test()
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to