# XXX TO DO: # - popup menu # - support partial or total redisplay # - key bindings (instead of quick-n-dirty bindings on Canvas): # - up/down arrow keys to move focus around # - ditto for page up/down, home/end # - left/right arrows to expand/collapse & move out/in # - more doc strings # - add icons for "file", "module", "class", "method"; better "python" icon # - callback for selection??? # - multiple-item selection # - tooltips # - redo geometry without magic numbers # - keep track of object ids to allow more careful cleaning # - optimize tree redraw after expand of subnode import os from Tkinter import * import imp from idlelib import ZoomHeight from idlelib.configHandler import idleConf ICONDIR = "Icons" # Look for Icons subdirectory in the same directory as this module try: _icondir = os.path.join(os.path.dirname(__file__), ICONDIR) except NameError: _icondir = ICONDIR if os.path.isdir(_icondir): ICONDIR = _icondir elif not os.path.isdir(ICONDIR): raise RuntimeError, "can't find icon directory (%r)" % (ICONDIR,) def listicons(icondir=ICONDIR): """Utility to display the available icons.""" root = Tk() import glob list = glob.glob(os.path.join(icondir, "*.gif")) list.sort() images = [] row = column = 0 for file in list: name = os.path.splitext(os.path.basename(file))[0] image = PhotoImage(file=file, master=root) images.append(image) label = Label(root, image=image, bd=1, relief="raised") label.grid(row=row, column=column) label = Label(root, text=name) label.grid(row=row+1, column=column) column = column + 1 if column >= 10: row = row+2 column = 0 root.images = images class TreeNode: def __init__(self, canvas, parent, item): self.canvas = canvas self.parent = parent self.item = item self.state = 'collapsed' self.selected = False self.children = [] self.x = self.y = None self.iconimages = {} # cache of PhotoImage instances for icons def destroy(self): for c in self.children[:]: self.children.remove(c) c.destroy() self.parent = None def geticonimage(self, name): try: return self.iconimages[name] except KeyError: pass file, ext = os.path.splitext(name) ext = ext or ".gif" fullname = os.path.join(ICONDIR, file + ext) image = PhotoImage(master=self.canvas, file=fullname) self.iconimages[name] = image return image def select(self, event=None): if self.selected: return self.deselectall() self.selected = True self.canvas.delete(self.image_id) self.drawicon() self.drawtext() def deselect(self, event=None): if not self.selected: return self.selected = False self.canvas.delete(self.image_id) self.drawicon() self.drawtext() def deselectall(self): if self.parent: self.parent.deselectall() else: self.deselecttree() def deselecttree(self): if self.selected: self.deselect() for child in self.children: child.deselecttree() def flip(self, event=None): if self.state == 'expanded': self.collapse() else: self.expand() self.item.OnDoubleClick() return "break" def expand(self, event=None): if not self.item._IsExpandable(): return if self.state != 'expanded': self.state = 'expanded' self.update() self.view() def collapse(self, event=None): if self.state != 'collapsed': self.state = 'collapsed' self.update() def view(self): top = self.y - 2 bottom = self.lastvisiblechild().y + 17 height = bottom - top visible_top = self.canvas.canvasy(0) visible_height = self.canvas.winfo_height() visible_bottom = self.canvas.canvasy(visible_height) if visible_top <= top and bottom <= visible_bottom: return x0, y0, x1, y1 = self.canvas._getints(self.canvas['scrollregion']) if top >= visible_top and height <= visible_height: fraction = top + height - visible_height else: fraction = top fraction = float(fraction) / y1 self.canvas.yview_moveto(fraction) def lastvisiblechild(self): if self.children and self.state == 'expanded': return self.children[-1].lastvisiblechild() else: return self def update(self): if self.parent: self.parent.update() else: oldcursor = self.canvas['cursor'] self.canvas['cursor'] = "watch" self.canvas.update() self.canvas.delete(ALL) # XXX could be more subtle self.draw(7, 2) x0, y0, x1, y1 = self.canvas.bbox(ALL) self.canvas.configure(scrollregion=(0, 0, x1, y1)) self.canvas['cursor'] = oldcursor def draw(self, x, y): # XXX This hard-codes too many geometry constants! dy = 20 self.x, self.y = x, y self.drawicon() self.drawtext() if self.state != 'expanded': return y + dy # draw children if not self.children: sublist = self.item._GetSubList() if not sublist: # _IsExpandable() was mistaken; that's allowed return y+17 for item in sublist: child = self.__class__(self.canvas, self, item) self.children.append(child) cx = x+20 cy = y + dy cylast = 0 for child in self.children: cylast = cy self.canvas.create_line(x+9, cy+7, cx, cy+7, fill="gray50") cy = child.draw(cx, cy) if child.item._IsExpandable(): if child.state == 'expanded': iconname = "minusnode" callback = child.collapse else: iconname = "plusnode" callback = child.expand image = self.geticonimage(iconname) id = self.canvas.create_image(x+9, cylast+7, image=image) # XXX This leaks bindings until canvas is deleted: self.canvas.tag_bind(id, "<1>", callback) self.canvas.tag_bind(id, "<Double-1>", lambda x: None) id = self.canvas.create_line(x+9, y+10, x+9, cylast+7, ##stipple="gray50", # XXX Seems broken in Tk 8.0.x fill="gray50") self.canvas.tag_lower(id) # XXX .lower(id) before Python 1.5.2 return cy def drawicon(self): if self.selected: imagename = (self.item.GetSelectedIconName() or self.item.GetIconName() or "openfolder") else: imagename = self.item.GetIconName() or "folder" image = self.geticonimage(imagename) id = self.canvas.create_image(self.x, self.y, anchor="nw", image=image) self.image_id = id self.canvas.tag_bind(id, "<1>", self.select) self.canvas.tag_bind(id, "<Double-1>", self.flip) def drawtext(self): textx = self.x+20-1 texty = self.y-4 labeltext = self.item.GetLabelText() if labeltext: id = self.canvas.create_text(textx, texty, anchor="nw", text=labeltext) self.canvas.tag_bind(id, "<1>", self.select) self.canvas.tag_bind(id, "<Double-1>", self.flip) x0, y0, x1, y1 = self.canvas.bbox(id) textx = max(x1, 200) + 10 text = self.item.GetText() or "<no text>" try: self.entry except AttributeError: pass else: self.edit_finish() try: self.label except AttributeError: # padding carefully selected (on Windows) to match Entry widget: self.label = Label(self.canvas, text=text, bd=0, padx=2, pady=2) theme = idleConf.CurrentTheme() if self.selected: self.label.configure(idleConf.GetHighlight(theme, 'hilite')) else: self.label.configure(idleConf.GetHighlight(theme, 'normal')) id = self.canvas.create_window(textx, texty, anchor="nw", window=self.label) self.label.bind("<1>", self.select_or_edit) self.label.bind("<Double-1>", self.flip) self.text_id = id def select_or_edit(self, event=None): if self.selected and self.item.IsEditable(): self.edit(event) else: self.select(event) def edit(self, event=None): self.entry = Entry(self.label, bd=0, highlightthickness=1, width=0) self.entry.insert(0, self.label['text']) self.entry.selection_range(0, END) self.entry.pack(ipadx=5) self.entry.focus_set() self.entry.bind("<Return>", self.edit_finish) self.entry.bind("<Escape>", self.edit_cancel) def edit_finish(self, event=None): try: entry = self.entry del self.entry except AttributeError: return text = entry.get() entry.destroy() if text and text != self.item.GetText(): self.item.SetText(text) text = self.item.GetText() self.label['text'] = text self.drawtext() self.canvas.focus_set() def edit_cancel(self, event=None): try: entry = self.entry del self.entry except AttributeError: return entry.destroy() self.drawtext() self.canvas.focus_set() class TreeItem: """Abstract class representing tree items. Methods should typically be overridden, otherwise a default action is used. """ def __init__(self): """Constructor. Do whatever you need to do.""" def GetText(self): """Return text string to display.""" def GetLabelText(self): """Return label text string to display in front of text (if any).""" expandable = None def _IsExpandable(self): """Do not override! Called by TreeNode.""" if self.expandable is None: self.expandable = self.IsExpandable() return self.expandable def IsExpandable(self): """Return whether there are subitems.""" return 1 def _GetSubList(self): """Do not override! Called by TreeNode.""" if not self.IsExpandable(): return [] sublist = self.GetSubList() if not sublist: self.expandable = 0 return sublist def IsEditable(self): """Return whether the item's text may be edited.""" def SetText(self, text): """Change the item's text (if it is editable).""" def GetIconName(self): """Return name of icon to be displayed normally.""" def GetSelectedIconName(self): """Return name of icon to be displayed when selected.""" def GetSubList(self): """Return list of items forming sublist.""" def OnDoubleClick(self): """Called on a double-click on the item.""" # Example application class FileTreeItem(TreeItem): """Example TreeItem subclass -- browse the file system.""" def __init__(self, path): self.path = path def GetText(self): return os.path.basename(self.path) or self.path def IsEditable(self): return os.path.basename(self.path) != "" def SetText(self, text): newpath = os.path.dirname(self.path) newpath = os.path.join(newpath, text) if os.path.dirname(newpath) != os.path.dirname(self.path): return try: os.rename(self.path, newpath) self.path = newpath except os.error: pass def GetIconName(self): if not self.IsExpandable(): return "python" # XXX wish there was a "file" icon def IsExpandable(self): return os.path.isdir(self.path) def GetSubList(self): try: names = os.listdir(self.path) except os.error: return [] names.sort(key = os.path.normcase) sublist = [] for name in names: item = FileTreeItem(os.path.join(self.path, name)) sublist.append(item) return sublist # A canvas widget with scroll bars and some useful bindings class ScrolledCanvas: def __init__(self, master, **opts): if 'yscrollincrement' not in opts: opts['yscrollincrement'] = 17 self.master = master self.frame = Frame(master) self.frame.rowconfigure(0, weight=1) self.frame.columnconfigure(0, weight=1) self.canvas = Canvas(self.frame, **opts) self.canvas.grid(row=0, column=0, sticky="nsew") self.vbar = Scrollbar(self.frame, name="vbar") self.vbar.grid(row=0, column=1, sticky="nse") self.hbar = Scrollbar(self.frame, name="hbar", orient="horizontal") self.hbar.grid(row=1, column=0, sticky="ews") self.canvas['yscrollcommand'] = self.vbar.set self.vbar['command'] = self.canvas.yview self.canvas['xscrollcommand'] = self.hbar.set self.hbar['command'] = self.canvas.xview self.canvas.bind("<Key-Prior>", self.page_up) self.canvas.bind("<Key-Next>", self.page_down) self.canvas.bind("<Key-Up>", self.unit_up) self.canvas.bind("<Key-Down>", self.unit_down) #if isinstance(master, Toplevel) or isinstance(master, Tk): self.canvas.bind("<Alt-Key-2>", self.zoom_height) self.canvas.focus_set() def page_up(self, event): self.canvas.yview_scroll(-1, "page") return "break" def page_down(self, event): self.canvas.yview_scroll(1, "page") return "break" def unit_up(self, event): self.canvas.yview_scroll(-1, "unit") return "break" def unit_down(self, event): self.canvas.yview_scroll(1, "unit") return "break" def zoom_height(self, event): ZoomHeight.zoom_height(self.master) return "break" def _tree_widget(parent): root = Tk() root.title("Test TreeWidget") width, height, x, y = list(map(int, re.split('[x+]', parent.geometry()))) root.geometry("+%d+%d"%(x, y + 150)) sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1) sc.frame.pack(expand=1, fill="both", side=LEFT) item = FileTreeItem(os.getcwd()) node = TreeNode(sc.canvas, None, item) node.expand() root.mainloop() if __name__ == '__main__': from idlelib.idle_test.htest import run run(_tree_widget)
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
Icons | Folder | 0755 |
|
|
idle_test | Folder | 0755 |
|
|
AutoComplete.py | File | 8.75 KB | 0644 |
|
AutoComplete.pyc | File | 7.82 KB | 0644 |
|
AutoComplete.pyo | File | 7.82 KB | 0644 |
|
AutoCompleteWindow.py | File | 16.91 KB | 0644 |
|
AutoCompleteWindow.pyc | File | 12.19 KB | 0644 |
|
AutoCompleteWindow.pyo | File | 12.13 KB | 0644 |
|
AutoExpand.py | File | 3.32 KB | 0644 |
|
AutoExpand.pyc | File | 3.42 KB | 0644 |
|
AutoExpand.pyo | File | 3.42 KB | 0644 |
|
Bindings.py | File | 2.91 KB | 0644 |
|
Bindings.pyc | File | 4.58 KB | 0644 |
|
Bindings.pyo | File | 4.58 KB | 0644 |
|
CREDITS.txt | File | 1.82 KB | 0644 |
|
CallTipWindow.py | File | 5.92 KB | 0644 |
|
CallTipWindow.pyc | File | 5.99 KB | 0644 |
|
CallTipWindow.pyo | File | 5.99 KB | 0644 |
|
CallTips.py | File | 7.56 KB | 0644 |
|
CallTips.pyc | File | 7.94 KB | 0644 |
|
CallTips.pyo | File | 7.94 KB | 0644 |
|
ChangeLog | File | 55.07 KB | 0644 |
|
ClassBrowser.py | File | 6.83 KB | 0644 |
|
ClassBrowser.pyc | File | 9.28 KB | 0644 |
|
ClassBrowser.pyo | File | 9.28 KB | 0644 |
|
CodeContext.py | File | 8.15 KB | 0644 |
|
CodeContext.pyc | File | 6.5 KB | 0644 |
|
CodeContext.pyo | File | 6.46 KB | 0644 |
|
ColorDelegator.py | File | 9.53 KB | 0644 |
|
ColorDelegator.pyc | File | 8.69 KB | 0644 |
|
ColorDelegator.pyo | File | 8.69 KB | 0644 |
|
Debugger.py | File | 17.81 KB | 0644 |
|
Debugger.pyc | File | 17.13 KB | 0644 |
|
Debugger.pyo | File | 17.13 KB | 0644 |
|
Delegator.py | File | 665 B | 0644 |
|
Delegator.pyc | File | 1.24 KB | 0644 |
|
Delegator.pyo | File | 1.24 KB | 0644 |
|
EditorWindow.py | File | 63.96 KB | 0644 |
|
EditorWindow.pyc | File | 55.53 KB | 0644 |
|
EditorWindow.pyo | File | 55.43 KB | 0644 |
|
FileList.py | File | 3.63 KB | 0644 |
|
FileList.pyc | File | 3.93 KB | 0644 |
|
FileList.pyo | File | 3.9 KB | 0644 |
|
FormatParagraph.py | File | 7.12 KB | 0644 |
|
FormatParagraph.pyc | File | 6.97 KB | 0644 |
|
FormatParagraph.pyo | File | 6.97 KB | 0644 |
|
GrepDialog.py | File | 5.02 KB | 0644 |
|
GrepDialog.pyc | File | 6.27 KB | 0644 |
|
GrepDialog.pyo | File | 6.27 KB | 0644 |
|
HISTORY.txt | File | 10.08 KB | 0644 |
|
HyperParser.py | File | 10.25 KB | 0644 |
|
HyperParser.pyc | File | 6.52 KB | 0644 |
|
HyperParser.pyo | File | 6.52 KB | 0644 |
|
IOBinding.py | File | 21.4 KB | 0644 |
|
IOBinding.pyc | File | 18.1 KB | 0644 |
|
IOBinding.pyo | File | 18.1 KB | 0644 |
|
IdleHistory.py | File | 3.96 KB | 0644 |
|
IdleHistory.pyc | File | 3.96 KB | 0644 |
|
IdleHistory.pyo | File | 3.96 KB | 0644 |
|
MultiCall.py | File | 17.29 KB | 0644 |
|
MultiCall.pyc | File | 15.97 KB | 0644 |
|
MultiCall.pyo | File | 15.9 KB | 0644 |
|
MultiStatusBar.py | File | 1.32 KB | 0644 |
|
MultiStatusBar.pyc | File | 2.23 KB | 0644 |
|
MultiStatusBar.pyo | File | 2.23 KB | 0644 |
|
NEWS.txt | File | 46.14 KB | 0644 |
|
ObjectBrowser.py | File | 4.27 KB | 0644 |
|
ObjectBrowser.pyc | File | 6.9 KB | 0644 |
|
ObjectBrowser.pyo | File | 6.9 KB | 0644 |
|
OutputWindow.py | File | 4.47 KB | 0644 |
|
OutputWindow.pyc | File | 5.11 KB | 0644 |
|
OutputWindow.pyo | File | 5.11 KB | 0644 |
|
ParenMatch.py | File | 6.56 KB | 0644 |
|
ParenMatch.pyc | File | 6.96 KB | 0644 |
|
ParenMatch.pyo | File | 6.96 KB | 0644 |
|
PathBrowser.py | File | 2.94 KB | 0644 |
|
PathBrowser.pyc | File | 4.38 KB | 0644 |
|
PathBrowser.pyo | File | 4.38 KB | 0644 |
|
Percolator.py | File | 3.15 KB | 0644 |
|
Percolator.pyc | File | 4.5 KB | 0644 |
|
Percolator.pyo | File | 4.32 KB | 0644 |
|
PyParse.py | File | 19.05 KB | 0644 |
|
PyParse.pyc | File | 9.77 KB | 0644 |
|
PyParse.pyo | File | 9.34 KB | 0644 |
|
PyShell.py | File | 57.48 KB | 0755 |
|
PyShell.pyc | File | 51.59 KB | 0644 |
|
PyShell.pyo | File | 51.49 KB | 0644 |
|
README.txt | File | 7.71 KB | 0644 |
|
RemoteDebugger.py | File | 11.36 KB | 0644 |
|
RemoteDebugger.pyc | File | 15.94 KB | 0644 |
|
RemoteDebugger.pyo | File | 15.79 KB | 0644 |
|
RemoteObjectBrowser.py | File | 942 B | 0644 |
|
RemoteObjectBrowser.pyc | File | 2.1 KB | 0644 |
|
RemoteObjectBrowser.pyo | File | 2.1 KB | 0644 |
|
ReplaceDialog.py | File | 6.48 KB | 0644 |
|
ReplaceDialog.pyc | File | 7.57 KB | 0644 |
|
ReplaceDialog.pyo | File | 7.57 KB | 0644 |
|
RstripExtension.py | File | 1.03 KB | 0644 |
|
RstripExtension.pyc | File | 1.58 KB | 0644 |
|
RstripExtension.pyo | File | 1.58 KB | 0644 |
|
ScriptBinding.py | File | 8.26 KB | 0644 |
|
ScriptBinding.pyc | File | 8.01 KB | 0644 |
|
ScriptBinding.pyo | File | 8.01 KB | 0644 |
|
ScrolledList.py | File | 4.27 KB | 0644 |
|
ScrolledList.pyc | File | 6.33 KB | 0644 |
|
ScrolledList.pyo | File | 6.33 KB | 0644 |
|
SearchDialog.py | File | 2.57 KB | 0644 |
|
SearchDialog.pyc | File | 3.89 KB | 0644 |
|
SearchDialog.pyo | File | 3.89 KB | 0644 |
|
SearchDialogBase.py | File | 6.93 KB | 0644 |
|
SearchDialogBase.pyc | File | 8.26 KB | 0644 |
|
SearchDialogBase.pyo | File | 8.26 KB | 0644 |
|
SearchEngine.py | File | 7.29 KB | 0644 |
|
SearchEngine.pyc | File | 8.11 KB | 0644 |
|
SearchEngine.pyo | File | 8.11 KB | 0644 |
|
StackViewer.py | File | 4.33 KB | 0644 |
|
StackViewer.pyc | File | 6.25 KB | 0644 |
|
StackViewer.pyo | File | 6.25 KB | 0644 |
|
TODO.txt | File | 8.28 KB | 0644 |
|
ToolTip.py | File | 3.1 KB | 0644 |
|
ToolTip.pyc | File | 4.56 KB | 0644 |
|
ToolTip.pyo | File | 4.56 KB | 0644 |
|
TreeWidget.py | File | 14.68 KB | 0644 |
|
TreeWidget.pyc | File | 17.28 KB | 0644 |
|
TreeWidget.pyo | File | 17.28 KB | 0644 |
|
UndoDelegator.py | File | 10.53 KB | 0644 |
|
UndoDelegator.pyc | File | 13.24 KB | 0644 |
|
UndoDelegator.pyo | File | 13.24 KB | 0644 |
|
WidgetRedirector.py | File | 6.74 KB | 0644 |
|
WidgetRedirector.pyc | File | 7.59 KB | 0644 |
|
WidgetRedirector.pyo | File | 7.59 KB | 0644 |
|
WindowList.py | File | 2.42 KB | 0644 |
|
WindowList.pyc | File | 3.55 KB | 0644 |
|
WindowList.pyo | File | 3.55 KB | 0644 |
|
ZoomHeight.py | File | 1.27 KB | 0644 |
|
ZoomHeight.pyc | File | 1.61 KB | 0644 |
|
ZoomHeight.pyo | File | 1.61 KB | 0644 |
|
__init__.py | File | 288 B | 0644 |
|
__init__.pyc | File | 431 B | 0644 |
|
__init__.pyo | File | 431 B | 0644 |
|
aboutDialog.py | File | 6.85 KB | 0644 |
|
aboutDialog.pyc | File | 6.69 KB | 0644 |
|
aboutDialog.pyo | File | 6.69 KB | 0644 |
|
config-extensions.def | File | 2.9 KB | 0644 |
|
config-highlight.def | File | 2.46 KB | 0644 |
|
config-keys.def | File | 7.59 KB | 0644 |
|
config-main.def | File | 2.5 KB | 0644 |
|
configDialog.py | File | 64.41 KB | 0644 |
|
configDialog.pyc | File | 52.04 KB | 0644 |
|
configDialog.pyo | File | 52.04 KB | 0644 |
|
configHandler.py | File | 31.72 KB | 0644 |
|
configHandler.pyc | File | 28.67 KB | 0644 |
|
configHandler.pyo | File | 28.67 KB | 0644 |
|
configHelpSourceEdit.py | File | 6.53 KB | 0644 |
|
configHelpSourceEdit.pyc | File | 6.44 KB | 0644 |
|
configHelpSourceEdit.pyo | File | 6.44 KB | 0644 |
|
configSectionNameDialog.py | File | 3.95 KB | 0644 |
|
configSectionNameDialog.pyc | File | 4.32 KB | 0644 |
|
configSectionNameDialog.pyo | File | 4.32 KB | 0644 |
|
dynOptionMenuWidget.py | File | 1.94 KB | 0644 |
|
dynOptionMenuWidget.pyc | File | 2.72 KB | 0644 |
|
dynOptionMenuWidget.pyo | File | 2.72 KB | 0644 |
|
extend.txt | File | 3.56 KB | 0644 |
|
help.html | File | 41.42 KB | 0644 |
|
help.py | File | 10.78 KB | 0644 |
|
help.pyc | File | 11.98 KB | 0644 |
|
help.pyo | File | 11.98 KB | 0644 |
|
help.txt | File | 11.86 KB | 0644 |
|
idle.py | File | 453 B | 0644 |
|
idle.pyc | File | 410 B | 0644 |
|
idle.pyo | File | 410 B | 0644 |
|
idle.pyw | File | 563 B | 0644 |
|
idlever.py | File | 415 B | 0644 |
|
idlever.pyc | File | 578 B | 0644 |
|
idlever.pyo | File | 578 B | 0644 |
|
keybindingDialog.py | File | 12.18 KB | 0644 |
|
keybindingDialog.pyc | File | 11.89 KB | 0644 |
|
keybindingDialog.pyo | File | 11.89 KB | 0644 |
|
macosxSupport.py | File | 8.24 KB | 0644 |
|
macosxSupport.pyc | File | 8.16 KB | 0644 |
|
macosxSupport.pyo | File | 8.02 KB | 0644 |
|
rpc.py | File | 19.68 KB | 0644 |
|
rpc.pyc | File | 21.22 KB | 0644 |
|
rpc.pyo | File | 21.12 KB | 0644 |
|
run.py | File | 12.61 KB | 0644 |
|
run.pyc | File | 13.1 KB | 0644 |
|
run.pyo | File | 13.05 KB | 0644 |
|
tabbedpages.py | File | 18.01 KB | 0644 |
|
tabbedpages.pyc | File | 18.13 KB | 0644 |
|
tabbedpages.pyo | File | 18.13 KB | 0644 |
|
textView.py | File | 3.44 KB | 0644 |
|
textView.pyc | File | 3.93 KB | 0644 |
|
textView.pyo | File | 3.93 KB | 0644 |
|