File: switch.py

package info (click to toggle)
python2.4 2.4.6-1%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 44,888 kB
  • ctags: 86,995
  • sloc: ansic: 306,391; python: 271,931; sh: 10,210; makefile: 4,248; perl: 3,736; lisp: 3,678; xml: 894; objc: 756; cpp: 7; sed: 2
file content (55 lines) | stat: -rw-r--r-- 1,559 bytes parent folder | download | duplicates (27)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Show how to do switchable panels.

from Tkinter import *

class App:

    def __init__(self, top=None, master=None):
        if top is None:
            if master is None:
                top = Tk()
            else:
                top = Toplevel(master)
        self.top = top
        self.buttonframe = Frame(top)
        self.buttonframe.pack()
        self.panelframe = Frame(top,  borderwidth=2, relief=GROOVE)
        self.panelframe.pack(expand=1, fill=BOTH)
        self.panels = {}
        self.curpanel = None

    def addpanel(self, name, klass):
        button = Button(self.buttonframe, text=name,
                        command=lambda self=self, name=name: self.show(name))
        button.pack(side=LEFT)
        frame = Frame(self.panelframe)
        instance = klass(frame)
        self.panels[name] = (button, frame, instance)
        if self.curpanel is None:
            self.show(name)

    def show(self, name):
        (button, frame, instance) = self.panels[name]
        if self.curpanel:
            self.curpanel.pack_forget()
        self.curpanel = frame
        frame.pack(expand=1, fill="both")

class LabelPanel:
    def __init__(self, frame):
        self.label = Label(frame, text="Hello world")
        self.label.pack()

class ButtonPanel:
    def __init__(self, frame):
        self.button = Button(frame, text="Press me")
        self.button.pack()

def main():
    app = App()
    app.addpanel("label", LabelPanel)
    app.addpanel("button", ButtonPanel)
    app.top.mainloop()

if __name__ == '__main__':
    main()