Latest recipes tagged "tkinter"http://code.activestate.com/recipes/tags/tkinter/new/2017-05-06T19:06:05-07:00ActiveState Code RecipesTkinter 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> 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> 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> 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 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> 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> Metro Listbox (Python) 2017-04-16T16:48:22-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580785-metro-listbox/ <p style="color: grey"> Python recipe 580785 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/listbox/">listbox</a>, <a href="/recipes/tags/metro/">metro</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 2. </p> <p>I provide an alternative listbox for tkinter.</p> <p>Using this trick you can add horizontal and vertical padding to every item and also a width.</p> Stacked frame for Tkinter (Python) 2017-04-13T18:20:26-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580784-stacked-frame-for-tkinter/ <p style="color: grey"> Python recipe 580784 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/animation/">animation</a>, <a href="/recipes/tags/frame/">frame</a>, <a href="/recipes/tags/stacked/">stacked</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 2. </p> <p>I provide here a stacked frame widget with possibility to use animation for the transition.</p> Metro Checkbuttons and Radiobuttons (Python) 2017-04-12T23:25:49-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580783-metro-checkbuttons-and-radiobuttons/ <p style="color: grey"> Python recipe 580783 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/checkbutton/">checkbutton</a>, <a href="/recipes/tags/radiobutton/">radiobutton</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 2. </p> <p>This recipe shows how to create custom checkbuttons and radiobuttons</p> Image background for tkinter (Python) 2017-04-12T19:37:06-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580782-image-background-for-tkinter/ <p style="color: grey"> Python recipe 580782 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/background/">background</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 3. </p> <p>This is only a proof of concept.</p> <p>In my first example, PIL is required. I use PIL also to draw the text over the background. I use the place geometry manager to position the entry.</p> <p>In my second example, I use a canvas widget to draw text over image. I also use the canvas to create other widgets over the background.</p> Metro Accordion for Tkinter (Python) 2017-04-11T15:52:23-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580781-metro-accordion-for-tkinter/ <p style="color: grey"> Python recipe 580781 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/accordion/">accordion</a>, <a href="/recipes/tags/metro/">metro</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/widget/">widget</a>). Revision 5. </p> <p>I provide an animation abstract object to make easy the animation of the accordion.</p> Metro Dialog for Tkinter (Python) 2017-04-11T11:23:46-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580780-metro-dialog-for-tkinter/ <p style="color: grey"> Python recipe 580780 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/dialog/">dialog</a>, <a href="/recipes/tags/metro/">metro</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 5. </p> <p>I provide here a metro style dialog.</p> <p>It's possible to use a different style of colors subclassing the class <em>Metro_Dialog</em> or providing the corresponding arguments during class initialization. </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> 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> 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> Tkinter buddies or shorcuts (Python) 2017-04-07T11:45:19-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580775-tkinter-buddies-or-shorcuts/ <p style="color: grey"> Python recipe 580775 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/accelerator/">accelerator</a>, <a href="/recipes/tags/buddies/">buddies</a>, <a href="/recipes/tags/buddy/">buddy</a>, <a href="/recipes/tags/shorcut/">shorcut</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 2. </p> <p>I provide two convenience functions to create shorcuts. <em>create_buddy</em> creates a buddy for the provided label.A buddy establish a connection between a label and a related widget. It provides a quick keyboard shorcut to focus its partner widget. (Buddy is a terminology used in PyQt).</p> <p><em>create_shortcut_to_button</em> creates a shorcut to invoke a button.</p> <p>I bind to toplevel containing the widget. This way, when the dialog is closed all the bindings disappear.</p> <p>All shorcuts are of this form: Alt + letter</p> <p>Buddies and shorcuts enriches the user experience providing new ways to navigate and interact quickly with the application.</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 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> 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> Tkinter file autocomplete entry (Python) 2017-04-03T15:18:36-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580771-tkinter-file-autocomplete-entry/ <p style="color: grey"> Python recipe 580771 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/autocomplete/">autocomplete</a>, <a href="/recipes/tags/entry/">entry</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 9. </p> <p>I define a "File_Entry" widget to make more easier to the final user to write and search for a path. I am using my own version of combobox widget:</p> <p><a href="https://code.activestate.com/recipes/580770-combobox-autocomplete/" rel="nofollow">https://code.activestate.com/recipes/580770-combobox-autocomplete/</a></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>