菜单

菜单示例

输出:

  1. 100 371 91 29
  2. 100 342 91 29
  3. 100 313 91 29
  4. 100 284 91 29
  5. 100 255 91 29
  1. import numpy as np
  2. import matplotlib.colors as colors
  3. import matplotlib.patches as patches
  4. import matplotlib.mathtext as mathtext
  5. import matplotlib.pyplot as plt
  6. import matplotlib.artist as artist
  7. import matplotlib.image as image
  8. class ItemProperties(object):
  9. def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
  10. alpha=1.0):
  11. self.fontsize = fontsize
  12. self.labelcolor = labelcolor
  13. self.bgcolor = bgcolor
  14. self.alpha = alpha
  15. self.labelcolor_rgb = colors.to_rgba(labelcolor)[:3]
  16. self.bgcolor_rgb = colors.to_rgba(bgcolor)[:3]
  17. class MenuItem(artist.Artist):
  18. parser = mathtext.MathTextParser("Bitmap")
  19. padx = 5
  20. pady = 5
  21. def __init__(self, fig, labelstr, props=None, hoverprops=None,
  22. on_select=None):
  23. artist.Artist.__init__(self)
  24. self.set_figure(fig)
  25. self.labelstr = labelstr
  26. if props is None:
  27. props = ItemProperties()
  28. if hoverprops is None:
  29. hoverprops = ItemProperties()
  30. self.props = props
  31. self.hoverprops = hoverprops
  32. self.on_select = on_select
  33. x, self.depth = self.parser.to_mask(
  34. labelstr, fontsize=props.fontsize, dpi=fig.dpi)
  35. if props.fontsize != hoverprops.fontsize:
  36. raise NotImplementedError(
  37. 'support for different font sizes not implemented')
  38. self.labelwidth = x.shape[1]
  39. self.labelheight = x.shape[0]
  40. self.labelArray = np.zeros((x.shape[0], x.shape[1], 4))
  41. self.labelArray[:, :, -1] = x/255.
  42. self.label = image.FigureImage(fig, origin='upper')
  43. self.label.set_array(self.labelArray)
  44. # we'll update these later
  45. self.rect = patches.Rectangle((0, 0), 1, 1)
  46. self.set_hover_props(False)
  47. fig.canvas.mpl_connect('button_release_event', self.check_select)
  48. def check_select(self, event):
  49. over, junk = self.rect.contains(event)
  50. if not over:
  51. return
  52. if self.on_select is not None:
  53. self.on_select(self)
  54. def set_extent(self, x, y, w, h):
  55. print(x, y, w, h)
  56. self.rect.set_x(x)
  57. self.rect.set_y(y)
  58. self.rect.set_width(w)
  59. self.rect.set_height(h)
  60. self.label.ox = x + self.padx
  61. self.label.oy = y - self.depth + self.pady/2.
  62. self.hover = False
  63. def draw(self, renderer):
  64. self.rect.draw(renderer)
  65. self.label.draw(renderer)
  66. def set_hover_props(self, b):
  67. if b:
  68. props = self.hoverprops
  69. else:
  70. props = self.props
  71. r, g, b = props.labelcolor_rgb
  72. self.labelArray[:, :, 0] = r
  73. self.labelArray[:, :, 1] = g
  74. self.labelArray[:, :, 2] = b
  75. self.label.set_array(self.labelArray)
  76. self.rect.set(facecolor=props.bgcolor, alpha=props.alpha)
  77. def set_hover(self, event):
  78. 'check the hover status of event and return true if status is changed'
  79. b, junk = self.rect.contains(event)
  80. changed = (b != self.hover)
  81. if changed:
  82. self.set_hover_props(b)
  83. self.hover = b
  84. return changed
  85. class Menu(object):
  86. def __init__(self, fig, menuitems):
  87. self.figure = fig
  88. fig.suppressComposite = True
  89. self.menuitems = menuitems
  90. self.numitems = len(menuitems)
  91. maxw = max(item.labelwidth for item in menuitems)
  92. maxh = max(item.labelheight for item in menuitems)
  93. totalh = self.numitems*maxh + (self.numitems + 1)*2*MenuItem.pady
  94. x0 = 100
  95. y0 = 400
  96. width = maxw + 2*MenuItem.padx
  97. height = maxh + MenuItem.pady
  98. for item in menuitems:
  99. left = x0
  100. bottom = y0 - maxh - MenuItem.pady
  101. item.set_extent(left, bottom, width, height)
  102. fig.artists.append(item)
  103. y0 -= maxh + MenuItem.pady
  104. fig.canvas.mpl_connect('motion_notify_event', self.on_move)
  105. def on_move(self, event):
  106. draw = False
  107. for item in self.menuitems:
  108. draw = item.set_hover(event)
  109. if draw:
  110. self.figure.canvas.draw()
  111. break
  112. fig = plt.figure()
  113. fig.subplots_adjust(left=0.3)
  114. props = ItemProperties(labelcolor='black', bgcolor='yellow',
  115. fontsize=15, alpha=0.2)
  116. hoverprops = ItemProperties(labelcolor='white', bgcolor='blue',
  117. fontsize=15, alpha=0.2)
  118. menuitems = []
  119. for label in ('open', 'close', 'save', 'save as', 'quit'):
  120. def on_select(item):
  121. print('you selected %s' % item.labelstr)
  122. item = MenuItem(fig, label, props=props, hoverprops=hoverprops,
  123. on_select=on_select)
  124. menuitems.append(item)
  125. menu = Menu(fig, menuitems)
  126. plt.show()

下载这个示例