简体   繁体   中英

Python Tkinter: Canvas scrolling with MouseWheel

I have created a tree inside a Canvas, and I have also allowed MouseWheel to scroll up and down.

However, how do I prevent scrolling if tree content has not exceed canvas size? (Tree content can possibly exceed canvas size by expanding)

Please run the following code:

from Tkinter import Tk, Frame, BOTH, Canvas
from xml.dom.minidom import parseString
from idlelib.TreeWidget import TreeItem, TreeNode

class DomTreeItem(TreeItem):
   def __init__(self, node):
      self.node = node
   def GetText(self):
      node = self.node
      if node.nodeType == node.ELEMENT_NODE:
         return node.nodeName
      elif node.nodeType == node.TEXT_NODE:
         return node.nodeValue
   def IsExpandable(self):
      node = self.node
      return node.hasChildNodes()
   def GetSubList(self):
      parent = self.node
      children = parent.childNodes
      prelist = [DomTreeItem(node) for node in children]
      itemlist = [item for item in prelist if item.GetText().strip()]
      return itemlist

data = '''
<top>
 <b>
  <c>d</c>
  <c>e</c>
 </b>
 <b>
  <c><c><c><c><c>f</c></c></c></c></c>
  <c><c><c><c><c>f</c></c></c></c></c>
  <c><c><c><c><c>f</c></c></c></c></c>
 </b>
</top>
'''

class Application(Frame):
   def __init__(self, parent):
      Frame.__init__(self, parent, background = "white")
      parent.configure(bg = "black")
      self.pack(fill = BOTH, expand = True, padx = 20, pady = 20)      
      self.parent = parent

      self.parent.geometry('%dx%d+%d+%d' % (700, 700, 0, 0))

      self.canvas = Canvas(self, bg = "white", bd = 10, highlightbackground = "black")
      self.canvas.grid(column = 0, row = 0, rowspan = 2)
      dom = parseString(data)
      item = DomTreeItem(dom.documentElement)
      node = TreeNode(self.canvas, None, item)
      node.update()
      node.expand()

      self.parent.bind("<MouseWheel>", self.mouse_wheel) # Windows mouse wheel event
      self.parent.bind("<Button-4>", self.mouse_wheel) # Linux mouse wheel event (Up)
      self.parent.bind("<Button-5>", self.mouse_wheel) # Linux mouse wheel event (Down)

   def mouse_wheel(self, event):
      """ Mouse wheel as scroll bar """
      direction = 0
      # respond to Linux or Windows wheel event
      if event.num == 5 or event.delta == -120:
         direction = 1
      if event.num == 4 or event.delta == 120:
         direction = -1
      self.canvas.yview_scroll(direction, "units")

def main():
   root = Tk()
   Application(root)
   root.mainloop()

if __name__ == '__main__':
   main()  

You can use the bbox method of canvas to retrieve the actual height of drawn items on canvas. bbox return a tuple defining a rectangle. You can compare it with the height of your canvas widget.

  height = self.canvas.winfo_height()
  _,_,_,items_height = self.canvas.bbox(Tkinter.ALL)
  if (items_height < height):
     direction = 0

I'm pretty unfamiliar with Trees, but it looks like it's just a bunch of Labels. You can measure their heights and compare them with the height of the Canvas to determine if you need to scroll. This is a little sloppy, but it worked for me:

if self.canvas.winfo_reqheight() < len(self.canvas.winfo_children()) * self.canvas.winfo_children()[0].winfo_reqheight():
    self.canvas.yview_scroll(direction, "units")
else:
    pass

EDIT: in case that's too messy, here's the pseudo code:

if CANVAS_HEIGHT < NUMBER_OF_LABELS * LABEL_HEIGHT:
    scroll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM