Popular recipes tagged "idlelib"http://code.activestate.com/recipes/tags/idlelib/2017-04-03T13:37:34-07:00ActiveState Code RecipesFile browser for tkinter using idle GUI library (Python)
2017-04-03T13:37:34-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580772-file-browser-for-tkinter-using-idle-gui-library/
<p style="color: grey">
Python
recipe 580772
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/browser/">browser</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/idlelib/">idlelib</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
</p>
<p>Idle is installed by default on windows.</p>
<p>For Ubuntu, Linux Mint and Debian run:</p>
<pre class="prettyprint"><code> sudo apt-get install idle-python2.7
</code></pre>
<p>A tree structure is drawn on a Tkinter Canvas object. A tree item is an object with an icon and a text. The item maybe be expandable and/or editable. A tree item has two kind of icons: A normal icon and an icon when the item is selected. To create the tree structure, it's necessary to create a link between tree items, using a parent-child relationship. </p>
<p>The canvas is built using a <em>idlelib.TreeWidget.ScrolledCanvas</em> class. The <em>frame</em> attribute of this object contains a <em>Frame</em> Tkinter widget prepared for scrolling. This frame allocates a Tkinter <em>Canvas</em> and Tkinter <em>Scrollbars</em>. This is the signature:</p>
<pre class="prettyprint"><code> ScrolledCanvas(master, **options_for_the_tkinter_canvas)
</code></pre>
<p>It accepts exactly the same arguments than a <em>Canvas</em> widget.</p>
<p>A tree item should be a subclass of <em>idlelib.TreeWidget.TreeItem</em>.</p>
<p>The parent-child relationship between tree items is established using the <em>idlelib.TreeWidget.TreeNode</em> class.</p>
<p>This is the signature for <strong>TreeNode(canvas, parent, item)</strong>:</p>
<ul>
<li><em>canvas</em> should be a <em>ScrolledCanvas</em> instance. </li>
<li><em>parent</em> should be the parent item. Leave that to <em>None</em> to create a root node.</li>
<li><em>item</em> should be the child item</li>
</ul>
<p><em>FileTreeItem</em> is a type of <em>TreeItem</em>. The only argument of a file tree item is a path.</p>
<p>Here there is an example of a custom tree item:</p>
<p><a href="https://code.activestate.com/recipes/579077-bookmarks-browser-for-firefox/" rel="nofollow">https://code.activestate.com/recipes/579077-bookmarks-browser-for-firefox/</a></p>
<p>To create your own tree items, it's required to subclass <em>TreeItem</em>. These are the methods that should be overrided:</p>
<ul>
<li><em>GetText:</em> It should return the text string to display.</li>
<li><em>GetLabelText:</em> It should return label text string to display in front of text (Optional).</li>
<li><em>IsExpandable:</em> It should return a boolean indicating whether the istem is expandable</li>
<li><em>IsEditable:</em> It should return a boolean indicating whether the item's text may be edited.</li>
<li><em>SetText:</em> Get the text to change if the item is is editable</li>
<li><em>GetIconName:</em> Return name of icon to be displayed normally (Icons should be included in <em>ICONDIR</em> directory)</li>
<li><em>GetSelectedIconName:</em> Return name of icon to be displayed when selected (Icons should be included in <em>ICONDIR</em> directory).</li>
<li><em>GetSubList:</em> It should return a list of child items (Optional. If not defined, the element is not expandable)</li>
<li><em>OnDoubleClick:</em> Called on a double-click on the item. (Optional)</li>
</ul>
<p>Icons should be included in "Icons" subdirectory of path to idlelib library. If you want to use other path, just change <em>ICONDIR</em> variable to path to your icons:</p>
<pre class="prettyprint"><code> import idlelib
idlelib.ICONDIR = "Your path to your icons"
</code></pre>
<p>Run this to find the path to <em>idlelib</em> module:</p>
<pre class="prettyprint"><code> python -c "import idlelib; print(idlelib.__file__)"
</code></pre>