/Demo/tix/samples/OptMenu.py

http://unladen-swallow.googlecode.com/ · Python · 68 lines · 34 code · 13 blank · 21 comment · 2 complexity · b69393f205f6d273cb864a89773a739d MD5 · raw file

  1. # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: OptMenu.py 36560 2004-07-18 06:16:08Z tim_one $
  4. #
  5. # Tix Demostration Program
  6. #
  7. # This sample program is structured in such a way so that it can be
  8. # executed from the Tix demo program "tixwidgets.py": it must have a
  9. # procedure called "RunSample". It should also have the "if" statment
  10. # at the end of this file so that it can be run as a standalone
  11. # program.
  12. # This file demonstrates the use of the tixOptionMenu widget -- you can
  13. # use it for the user to choose from a fixed set of options
  14. #
  15. import Tix
  16. options = {'text':'Plain Text', 'post':'PostScript', 'html':'HTML',
  17. 'tex':'LaTeX', 'rtf':'Rich Text Format'}
  18. def RunSample(w):
  19. global demo_opt_from, demo_opt_to
  20. demo_opt_from = Tix.StringVar()
  21. demo_opt_to = Tix.StringVar()
  22. top = Tix.Frame(w, bd=1, relief=Tix.RAISED)
  23. from_file = Tix.OptionMenu(top, label="From File Format : ",
  24. variable=demo_opt_from,
  25. options = 'label.width 19 label.anchor e menubutton.width 15')
  26. to_file = Tix.OptionMenu(top, label="To File Format : ",
  27. variable=demo_opt_to,
  28. options='label.width 19 label.anchor e menubutton.width 15')
  29. # Add the available options to the two OptionMenu widgets
  30. #
  31. # [Hint] You have to add the options first before you set the
  32. # global variables "demo_opt_from" and "demo_opt_to". Otherwise
  33. # the OptionMenu widget will complain about "unknown options"!
  34. #
  35. for opt in options.keys():
  36. from_file.add_command(opt, label=options[opt])
  37. to_file.add_command(opt, label=options[opt])
  38. demo_opt_from.set('html')
  39. demo_opt_to.set('post')
  40. from_file.pack(side=Tix.TOP, anchor=Tix.W, pady=3, padx=6)
  41. to_file.pack(side=Tix.TOP, anchor=Tix.W, pady=3, padx=6)
  42. box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL)
  43. box.add('ok', text='Ok', underline=0, width=6,
  44. command=lambda w=w: ok_command(w))
  45. box.add('cancel', text='Cancel', underline=0, width=6,
  46. command=lambda w=w: w.destroy())
  47. box.pack(side=Tix.BOTTOM, fill=Tix.X)
  48. top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
  49. def ok_command(w):
  50. # tixDemo:Status "Convert file from %s to %s" % ( demo_opt_from.get(), demo_opt_to.get())
  51. w.destroy()
  52. if __name__ == '__main__':
  53. root = Tix.Tk()
  54. RunSample(root)
  55. root.mainloop()