home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Demo / tkinter / guido / switch.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  1.5 KB  |  56 lines

  1. # Show how to do switchable panels.
  2.  
  3. from Tkinter import *
  4.  
  5. class App:
  6.  
  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
  20.  
  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)
  30.  
  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")
  37.  
  38. class LabelPanel:
  39.     def __init__(self, frame):
  40.         self.label = Label(frame, text="Hello world")
  41.         self.label.pack()
  42.  
  43. class ButtonPanel:
  44.     def __init__(self, frame):
  45.         self.button = Button(frame, text="Press me")
  46.         self.button.pack()
  47.  
  48. def main():
  49.     app = App()
  50.     app.addpanel("label", LabelPanel)
  51.     app.addpanel("button", ButtonPanel)
  52.     app.top.mainloop()
  53.  
  54. if __name__ == '__main__':
  55.     main()
  56.