Popular recipes by Miguel Martínez López http://code.activestate.com/recipes/users/4189907/2017-06-19T18:22:01-07:00ActiveState Code RecipesFrame with border color for Tkinter (Python) 2017-04-17T11:25:02-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580735-frame-with-border-color-for-tkinter/ <p style="color: grey"> Python recipe 580735 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/border/">border</a>, <a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/frame/">frame</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). </p> <p>This trick show how to add a border color to frame. These are the important configurations:</p> <pre class="prettyprint"><code> highlightbackground="your border color here" highlightcolor="your border color here" highlightthickness="the border width" bd= 0 </code></pre> Tkinter table with scrollbars (Python) 2017-05-06T19:06:05-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580793-tkinter-table-with-scrollbars/ <p style="color: grey"> Python recipe 580793 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/scrollbars/">scrollbars</a>, <a href="/recipes/tags/table/">table</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 13. </p> <p>I created here a tkinter table with scrollbar support. I use one of my other recipes for the mousewheel support and scrolling:</p> <p><a href="https://code.activestate.com/recipes/580640-scrolling-frame-with-mouse-wheel-support" rel="nofollow">https://code.activestate.com/recipes/580640-scrolling-frame-with-mouse-wheel-support</a></p> Simple multicolumn listbox for tkinter (Python) 2017-05-02T22:27:15-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580794-simple-multicolumn-listbox-for-tkinter/ <p style="color: grey"> Python recipe 580794 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/listbox/">listbox</a>, <a href="/recipes/tags/multicolumn/">multicolumn</a>, <a href="/recipes/tags/table/">table</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). </p> <p>This recipe makes easy to work a treeview widget like a table.</p> <p>It has several options for styling:</p> <ul> <li>heading_anchor</li> <li>heading_font</li> <li>heading_background</li> <li>heading_foreground</li> <li>cell_anchor</li> <li>cell_background</li> <li>cell_foreground</li> <li>cell_font</li> <li>cell_pady</li> <li>height</li> <li>padding</li> <li>adjust_heading_to_content</li> <li>stripped_rows</li> <li>headers</li> <li>selection_background</li> <li>selection_foreground</li> <li>field_background</li> </ul> <p>The "command" parameter is a callback and its called each time a row is selected.</p> Tkinter frame with gradient (Python) 2017-03-11T03:43:56-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580761-tkinter-frame-with-gradient/ <p style="color: grey"> Python recipe 580761 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/canvas/">canvas</a>, <a href="/recipes/tags/frame/">frame</a>, <a href="/recipes/tags/gradient/">gradient</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 4. </p> <p>Frame with linear gradient using PIL. </p> <p>It's also possible to make the same trick using tkinter PhotoImage.</p> <p><a href="http://stackoverflow.com/questions/10417524/why-is-photoimage-put-slow" rel="nofollow">http://stackoverflow.com/questions/10417524/why-is-photoimage-put-slow</a></p> <p>But PIL is more efficient:</p> <p><a href="https://groups.google.com/forum/#%21topic/comp.lang.python/nQ6YO-dTz10" rel="nofollow">https://groups.google.com/forum/#!topic/comp.lang.python/nQ6YO-dTz10</a></p> <p>Possible values for <strong>orient</strong> are: <em>VERTICAL</em>, <em>HORIZONTAL</em>. If <strong>orient</strong> is "vertical", then width is mandatory. If <strong>orient</strong> is "horizontal", then height is mandatory. If <strong>steps</strong> is <em>None</em>, then the gradient is composed of adjacent lines.</p> <p>One possible practical application of gradient frames are tool bars. Gradient guives a visual clue of when an area starts and when an area finish. </p> Tkinter search box (Python) 2017-04-08T12:27:36-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580773-tkinter-search-box/ <p style="color: grey"> Python recipe 580773 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/entry/">entry</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/searchbox/">searchbox</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 9. </p> <p>Instead of using two colors for active background and normal background, I use only one color and opacity parameter.</p> <p>I trigger the feeling of a button using different colors when the mouse is and isn't over. Many modern HTML search boxes uses the same approach.</p> <p>Command function receives text of entry box when button is pressed.</p> Tkinter Link or Hyperlink Button (Python) 2017-04-07T11:51:26-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580774-tkinter-link-or-hyperlink-button/ <p style="color: grey"> Python recipe 580774 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/button/">button</a>, <a href="/recipes/tags/hyperlink/">hyperlink</a>, <a href="/recipes/tags/link/">link</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/webbrowser/">webbrowser</a>). Revision 6. </p> <p>If a background is not provided, the widget gets the background color from its container. The link button has two states: normal and visited. When the link is clicked, the state change to visited. When the link is clicked the action command is called. By default the text of the link has an underline. There is several options for customization.</p> <p>I also added this megawidget to my "Metro UI Tkinter" recipe:</p> <p><a href="https://code.activestate.com/recipes/580729-metro-ui-tkinter" rel="nofollow">https://code.activestate.com/recipes/580729-metro-ui-tkinter</a></p> Tkinter frame with different border sizes (Python) 2017-05-06T18:45:00-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580798-tkinter-frame-with-different-border-sizes/ <p style="color: grey"> Python recipe 580798 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/border/">border</a>, <a href="/recipes/tags/size/">size</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). </p> <p>This trick shows how to create a bordered frame with different border size in each side.</p> Combobox Autocomplete (Python) 2017-04-03T15:17:26-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580770-combobox-autocomplete/ <p style="color: grey"> Python recipe 580770 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/autocomplete/">autocomplete</a>, <a href="/recipes/tags/combobox/">combobox</a>, <a href="/recipes/tags/entry/">entry</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 11. </p> <p>This is my own version of combobox autocomplete. The user can customize the matching function setting whether he wants to ignore case or whether the matching function should only match from the beginning of the string. The other possibility is to provide your own "auto complete function". The complete function returns all the found strings matching the text in the entry box.</p> <p>It's also possible to customize the height of listbox and whether to use horizontal or vertical scrollbars.</p> <p>Use arrows or Contro+n, Control+p to move selection on listbox.</p> <p>This is a practical application of the combobox widget:</p> <p><a href="https://code.activestate.com/recipes/580771-tkinter-file-autocomplete-entry/" rel="nofollow">https://code.activestate.com/recipes/580771-tkinter-file-autocomplete-entry/</a></p> Tkinter entry with placeholder (Python) 2017-04-06T14:21:03-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580768-tkinter-entry-with-placeholder/ <p style="color: grey"> Python recipe 580768 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/entry/">entry</a>, <a href="/recipes/tags/placeholder/">placeholder</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 8. </p> <p>The function <em>add_placeholder_to</em> adds a placeholder to a tkinter entry.</p> <p>Parameters:</p> <ul> <li><em>entry:</em> Tkinter widget to add placeholder</li> <li><em>placeholder:</em> Text of placeholder</li> <li><em>color:</em> Color of placeholder (optional)</li> <li><em>font</em>: Font of placeholder (optional)</li> </ul> <p>A placeholder state object is attached to attribute "placeholder_state" of entry widget for future reference. It makes more easier to configure state of placeholder, or change the preferences of the user for color and font for the placeholder.</p> <p>This widget is added also to my Metro Tkinter recipe:</p> <p><a href="https://code.activestate.com/recipes/580729-metro-ui-tkinter/" rel="nofollow">https://code.activestate.com/recipes/580729-metro-ui-tkinter/</a></p> Tkinter Desktop Notifications or Popups (Python) 2017-04-01T19:27:59-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580769-tkinter-desktop-notifications-or-popups/ <p style="color: grey"> Python recipe 580769 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/alert/">alert</a>, <a href="/recipes/tags/easing/">easing</a>, <a href="/recipes/tags/notification/">notification</a>, <a href="/recipes/tags/popup/">popup</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 2. </p> <p>This trick requires <em>pytweening</em>:</p> <p><a href="https://pypi.python.org/pypi/PyTweening" rel="nofollow">https://pypi.python.org/pypi/PyTweening</a></p> <p>Install writing:</p> <pre class="prettyprint"><code> pip install pytweening </code></pre> <p>It shows a notification on one corner of the screen,and gradually the notification disappears using an easing function. By default, it uses a linear easing function.</p> <p>The class <em>Notification_Manager</em> has the method <em>create_notification</em>, but it also has some convenient methods to create easily some kind of notifications: success, alert, info, warning.</p> Tkinter Datepicker (like the jQuery UI datepicker) (Python) 2017-06-19T18:22:01-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580725-tkinter-datepicker-like-the-jquery-ui-datepicker/ <p style="color: grey"> Python recipe 580725 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/calendar/">calendar</a>, <a href="/recipes/tags/datepicker/">datepicker</a>, <a href="/recipes/tags/megawidget/">megawidget</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 18. </p> <p>It's based on ttkcalendar.py. But the internals are totally changed.I don't use for example the Treeview widget. I added more features also:</p> <ul> <li>On mouse over, the day label changes the background</li> <li>The selected day has an outstanding style</li> <li>Added support for many hotkeys</li> </ul> <p>These are the default bindings:</p> <ul> <li><p><em>Click button 1 on entry:</em> Show calendar</p></li> <li><p><em>Click button 1 outside entry and calendar:</em> Hide calendar</p></li> <li><p><em>Escape:</em> Hide calendar</p></li> <li><p><em>CTRL + PAGE UP:</em> Move to the previous month.</p></li> <li><p><em>CTRL + PAGE DOWN:</em> Move to the next month.</p></li> <li><p><em>CTRL + SHIFT + PAGE UP:</em> Move to the previous year.</p></li> <li><p><em>CTRL + SHIFT + PAGE DOWN:</em> Move to the next year.</p></li> <li><p><em>CTRL + LEFT:</em> Move to the previous day.</p></li> <li><p><em>CTRL + RIGHT:</em> Move to the next day.</p></li> <li><p><em>CTRL + UP:</em> Move to the previous week.</p></li> <li><p><em>CTRL + DOWN:</em> Move to the next week.</p></li> <li><p><em>CTRL + END:</em> Close the datepicker and erase the date.</p></li> <li><p><em>CTRL + HOME:</em> Move to the current month.</p></li> <li><p><em>CTRL + SPACE:</em> Show date on calendar</p></li> <li><p><em>CTRL + Return:</em> Set current selection to entry</p></li> </ul> XML viewer for Tkinter or XML treeview (Python) 2017-02-22T23:00:21-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580752-xml-viewer-for-tkinter-or-xml-treeview/ <p style="color: grey"> Python recipe 580752 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/browser/">browser</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/treeview/">treeview</a>, <a href="/recipes/tags/viewer/">viewer</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 3. </p> <p>Tk widget to display XML for Python. I use the treeview widget. There is no external dependency.I also added support for autoscrollbars.</p> Tkinter table (Python) 2017-05-02T21:19:51-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580786-tkinter-table/ <p style="color: grey"> Python recipe 580786 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/table/">table</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 24. </p> <p>Table of data for tkinter.</p> <p>Here there is an improved vesion with vertical scrollbar support:</p> <p><a href="https://code.activestate.com/recipes/580793-tkinter-table-with-scrollbars" rel="nofollow">https://code.activestate.com/recipes/580793-tkinter-table-with-scrollbars</a></p> tkinter custom fonts (Python) 2017-04-10T01:07:11-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580778-tkinter-custom-fonts/ <p style="color: grey"> Python recipe 580778 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/font/">font</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 2. </p> <p>One Windows the best solution is to use the trick explained here:</p> <p><a href="http://stackoverflow.com/a/30631309" rel="nofollow">http://stackoverflow.com/a/30631309</a></p> <p>Another possibility is to use PIL. creating an image with the text and a specific font.</p> <p>I provide 2 classes: <em>CustomFont_Label</em> and <em>CustomFont_Message</em>.</p> <p><em>CustomFont_Message</em> displays multilines but requires the <em>width</em> parameter.</p> Tkinter treeview like a table or multicolumn listbox (Python) 2017-05-02T22:27:48-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580746-tkinter-treeview-like-a-table-or-multicolumn-listb/ <p style="color: grey"> Python recipe 580746 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/editable/">editable</a>, <a href="/recipes/tags/listbox/">listbox</a>, <a href="/recipes/tags/megawidget/">megawidget</a>, <a href="/recipes/tags/multicolumn/">multicolumn</a>, <a href="/recipes/tags/table/">table</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/treeview/">treeview</a>). Revision 33. </p> <p>This trick provides use my other recipe:</p> <p><a href="https://code.activestate.com/recipes/580794-simple-multicolumn-listbox-for-tkinter/" rel="nofollow">https://code.activestate.com/recipes/580794-simple-multicolumn-listbox-for-tkinter/</a></p> <p>This recipes defines a <em>Tk_Table:</em> A table that extends the multicolumn listbox adding row numbers, making the cells editable and adding autoscrollbars.</p> <p><em>Tk_Table</em></p> <p>It has the same options than <em>Multicolumn_Listbox</em>, and some extra parameters to configure the new functionality.</p> <p>Setting the editable keyword to True, makes the widget editable.</p> <p>If you want stripped rows, pass the stripped_rows with a tuple (or list) of two colors.</p> <p>If you want row numbers, then pass to <em>row_numbers</em> parameter a True value.</p> <p>These are the extra options for this class:</p> <ul> <li>entry_background</li> <li>entry_foreground</li> <li>entry_validatecommand</li> <li>entry_selectbackground</li> <li>entry_selectborderwidth</li> <li>entry_selectforeground</li> <li>scrollbar_background</li> <li>scrollbar_troughcolor</li> <li>rowlabel_anchor</li> <li>rowlabel_minwidth</li> <li>rowlabel_hoverbackground</li> <li>frame_relief</li> <li>frame_borderwidth</li> <li>frame_background</li> <li>row_numbers</li> </ul> Scrolled Frame V2 (Python) 2017-05-06T18:54:47-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580797-scrolled-frame-v2/ <p style="color: grey"> Python recipe 580797 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/frame/">frame</a>, <a href="/recipes/tags/scrolling/">scrolling</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 4. </p> <p>This is another version of scrolled frame. It doesn't use Canvas. Instead it does the trick using place geometry manager.</p> <p>Based on these codes:</p> <ul> <li><p>https://github.com/alejandroautalan/pygubu/blob/master/pygubu/widgets/tkscrolledframe.py</p></li> <li><p>http://wiki.tcl.tk/9223 </p></li> </ul> <p>This is my other version of scrolled frame:</p> <p><a href="https://code.activestate.com/recipes/580640-scrolling-frame-with-mouse-wheel-support/" rel="nofollow">https://code.activestate.com/recipes/580640-scrolling-frame-with-mouse-wheel-support/</a></p> File 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> bind all tkinter "bug" (Python) 2017-05-05T20:33:31-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580795-bind-all-tkinter-bug/ <p style="color: grey"> Python recipe 580795 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/all/">all</a>, <a href="/recipes/tags/bind/">bind</a>, <a href="/recipes/tags/binding/">binding</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 3. </p> <p>This recipes tries to solve the problem of bind_all and unbind_all for tkinter.</p> <p>When a callback is registered using bind_all method and later it's unregistered using unbind_all, all the callbacks are deleted for the "all" tag event. This makes difficult to register and unregister only one callback at a time. This recipes tries to solve this problem.</p> <p>Observe the difference between the code below and the recipe. With the code below, when the user clicks nothing happens. But with my recipe it's possible to bind and unbind specific callbacks.</p> <pre class="prettyprint"><code>try: from Tkinter import Tk, Frame except ImportError: from tkinter import Tk, Frame root = Tk() f = Frame(root, width= 300, height=300) f.pack() def callback1(event): print("callback1") def callback2(event): print("callback2") def callback3(event): print("callback3") root.bind_all("&lt;1&gt;", callback1, add="+") f.bind_all("&lt;1&gt;", callback2, add="+") f.bind_all("&lt;1&gt;", callback3, add="+") f.unbind_all("&lt;1&gt;") root.mainloop() </code></pre> Metro Spinner for Tkinter (Python) 2017-04-10T21:14:36-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580777-metro-spinner-for-tkinter/ <p style="color: grey"> Python recipe 580777 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/canvas/">canvas</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/rotation/">rotation</a>, <a href="/recipes/tags/spinner/">spinner</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). </p> <p>I create a rotating image class <em>RotatingIcon</em> inspired and based on this code:</p> <p><a href="http://stackoverflow.com/questions/15736530/python-tkinter-rotate-image-animation" rel="nofollow">http://stackoverflow.com/questions/15736530/python-tkinter-rotate-image-animation</a></p> <p>Features:</p> <ul> <li>Methods to stop and start the animation</li> <li>The animation automically stops when the window is not mapped, and the animation continues when the window is mapped again</li> <li>Time setting to control the speed of the animation</li> <li>All the formats accepted for PIL could be used. XBM format is automatically converted to Tk Bitmap. The advantage of Bitmats is the possibility to change the color of the foreground.</li> </ul> <p>I added 6 different styles of spinners with different sizes.</p> <p>I used fontawesome.io for the icon generation.</p> <p>For more metro widgets see here:</p> <p><a href="https://code.activestate.com/recipes/580729-metro-ui-tkinter/" rel="nofollow">https://code.activestate.com/recipes/580729-metro-ui-tkinter/</a></p> Long processing computation in Tkinter or long-running task (Python) 2017-02-06T18:30:18-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580754-long-processing-computation-in-tkinter-or-long-run/ <p style="color: grey"> Python recipe 580754 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/computation/">computation</a>, <a href="/recipes/tags/intensive/">intensive</a>, <a href="/recipes/tags/long/">long</a>, <a href="/recipes/tags/task/">task</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 12. </p> <p>I provide in this module the function "tk_call_async": It executes a function asynchronously.</p> <pre class="prettyprint"><code>tk_call_async(window, computation, args=(), kwargs={}, callback=None, polling=100, method=MULTIPROCESSING) </code></pre> <p>"tk_call_async" executes the function "computation" asynchronously with the provided "args" and "kwargs" without blocking the tkinter event loop.</p> <p>If "callback" is provided, it will be called with the result when the computation is finished.</p> <p>If an exception happens doing the computation, the errback function is called with the exception if this is provided.</p> <p>"Polling" will be the frequency to poll to check for results.</p> <p>There is two methods to execute the task: using multiprocessing or using threads.</p>