PageRenderTime 88ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/gui/tour/menuDemo.py

https://bitbucket.org/haploc/pythonscripts
Python | 110 lines | 94 code | 5 blank | 11 comment | 0 complexity | 5b8dbd9babc78b665d280aace34b091c MD5 | raw file
  1. '''
  2. example 9-08 menuDemo.py
  3. tk8.0 style main window menus
  4. menu/tool bars packed before middle, fill=X (pack first=clip last);
  5. adds photo menu entries; see also: add_checkbutton, add_radiobutton
  6. '''
  7. from tkinter import * # get widget classes
  8. from tkinter.messagebox import * # get standard dialogs
  9. class NewMenuDemo(Frame): # an extended frame
  10. def __init__(self, parent=None): # attach to top-level?
  11. Frame.__init__(self, parent) # do superclass init
  12. self.pack(expand=YES, fill=BOTH)
  13. self.createWidgets() # attach frames/widgets
  14. self.master.title("Toolbars and Menus") # set window-manager info
  15. self.master.iconname('tkpython') # label when iconified
  16. def createWidgets(self):
  17. self.makeMenuBar()
  18. self.makeToolBar()
  19. L = Label(self, text='Menu and Toolbar Demo')
  20. L.config(relief=SUNKEN, width=40, height=10, bg='white')
  21. L.pack(expand=YES, fill=BOTH)
  22. def makeToolBar(self):
  23. toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
  24. toolbar.pack(side=BOTTOM, fill=X)
  25. Button(toolbar, text='Quit', command=self.quit).pack(side=RIGHT)
  26. Button(toolbar, text='Hello', command=self.greeting).pack(side=LEFT)
  27. '''
  28. # resize toolbar images on the fly with PIL
  29. def makeToolBar(self, size=(40, 40)):
  30. from PIL.ImageTk import PhotoImage, Image # if jpegs or make new thumbs
  31. imgdir = r'../PIL/images/'
  32. toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
  33. toolbar.pack(side=BOTTOM, fill=X)
  34. photos = 'ora-lp4e-big.jpg', 'PythonPoweredAnim.gif', 'python_conf_ora.gif'
  35. self.toolPhotoObjs = []
  36. for file in photos:
  37. imgobj = Image.open(imgdir + file) # make new thumb
  38. imgobj.thumbnail(size, Image.ANTIALIAS) # best downsize filter
  39. img = PhotoImage(imgobj)
  40. btn = Button(toolbar, image=img, command=self.greeting)
  41. btn.config(relief=RAISED, bd=2)
  42. btn.config(width=size[0], height=size[1])
  43. btn.pack(side=LEFT)
  44. self.toolPhotoObjs.append((img, imgobj)) # keep a reference
  45. Button(toolbar, text='Quit', command=self.quit).pack(side=RIGHT, fill=Y)
  46. # use unresized gifs with standard tkinter
  47. def makeToolBar(self, size=(30, 30)):
  48. imgdir = r'../gifs/'
  49. toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
  50. toolbar.pack(side=BOTTOM, fill=X)
  51. photos = 'ora-lp4e.gif', 'pythonPowered.gif', 'python_conf_ora.gif'
  52. self.toolPhotoObjs = []
  53. for file in photos:
  54. img = PhotoImage(file=imgdir + file)
  55. btn = Button(toolbar, image=img, command=self.greeting)
  56. btn.config(bd=5, relief=RIDGE)
  57. btn.config(width=size[0], height=size[1])
  58. btn.pack(side=LEFT)
  59. self.toolPhotoObjs.append(img) # keep a reference
  60. Button(toolbar, text='Quit', command=self.quit).pack(side=RIGHT, fill=Y)
  61. '''
  62. def makeMenuBar(self):
  63. self.menubar = Menu(self.master)
  64. self.master.config(menu=self.menubar) # master=top-level window
  65. self.fileMenu()
  66. self.editMenu()
  67. self.imageMenu()
  68. def fileMenu(self):
  69. pulldown = Menu(self.menubar)
  70. pulldown.add_command(label='Open...', command=self.notdone)
  71. pulldown.add_command(label='Quit', command=self.quit)
  72. self.menubar.add_cascade(label='File', underline=0, menu=pulldown)
  73. def editMenu(self):
  74. pulldown = Menu(self.menubar)
  75. pulldown.add_command(label='Paste', command=self.notdone)
  76. pulldown.add_command(label='Spam', command=self.greeting)
  77. pulldown.add_separator()
  78. pulldown.add_command(label='Delete', command=self.greeting)
  79. pulldown.entryconfig(4, state=DISABLED)
  80. self.menubar.add_cascade(label='Edit', underline=0, menu=pulldown)
  81. def imageMenu(self):
  82. photoFiles = ('ora-lp4e.gif', 'pythonPowered.gif', 'python_conf_ora.gif')
  83. pulldown = Menu(self.menubar)
  84. self.photoObjs = []
  85. for file in photoFiles:
  86. img = PhotoImage(file='../gifs/' + file)
  87. pulldown.add_command(image=img, command=self.notdone)
  88. self.photoObjs.append(img) # keep a reference
  89. self.menubar.add_cascade(label='Image', underline=0, menu=pulldown)
  90. def greeting(self):
  91. showinfo('greeting', 'Greetings')
  92. def notdone(self):
  93. showerror('Not implemented', 'Not yet available')
  94. def quit(self):
  95. if askyesno('Verify quit', 'Are you sure you want to quit?'):
  96. Frame.quit(self)
  97. if __name__ == '__main__': NewMenuDemo().mainloop() # if I'm run as a script