Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Demo / tkinter / guido / switch.py
blob3b58f7ce476f39e39c3178b604d4b81513a03646
1 # Show how to do switchable panels.
3 from Tkinter import *
5 class App:
7 def __init__(self, top=None, master=None):
8 if top is None:
9 if master is None:
10 top = Tk()
11 else:
12 top = Toplevel(master)
13 self.top = top
14 self.buttonframe = Frame(top)
15 self.buttonframe.pack()
16 self.panelframe = Frame(top, borderwidth=2, relief=GROOVE)
17 self.panelframe.pack(expand=1, fill=BOTH)
18 self.panels = {}
19 self.curpanel = None
21 def addpanel(self, name, klass):
22 button = Button(self.buttonframe, text=name,
23 command=lambda self=self, name=name: self.show(name))
24 button.pack(side=LEFT)
25 frame = Frame(self.panelframe)
26 instance = klass(frame)
27 self.panels[name] = (button, frame, instance)
28 if self.curpanel is None:
29 self.show(name)
31 def show(self, name):
32 (button, frame, instance) = self.panels[name]
33 if self.curpanel:
34 self.curpanel.pack_forget()
35 self.curpanel = frame
36 frame.pack(expand=1, fill="both")
38 class LabelPanel:
39 def __init__(self, frame):
40 self.label = Label(frame, text="Hello world")
41 self.label.pack()
43 class ButtonPanel:
44 def __init__(self, frame):
45 self.button = Button(frame, text="Press me")
46 self.button.pack()
48 def main():
49 app = App()
50 app.addpanel("label", LabelPanel)
51 app.addpanel("button", ButtonPanel)
52 app.top.mainloop()
54 if __name__ == '__main__':
55 main()