Popular recipes tagged "meta:loc=82"http://code.activestate.com/recipes/tags/meta:loc=82/2017-04-06T14:21:03-07:00ActiveState Code RecipesTkinter 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> Testing Tkinter or Selenium for Tkinter (Python) 2017-01-24T20:34:52-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580751-testing-tkinter-or-selenium-for-tkinter/ <p style="color: grey"> Python recipe 580751 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/rpyc/">rpyc</a>, <a href="/recipes/tags/selenium/">selenium</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 3. </p> <p>This code is a little variation of my other trick:</p> <p><a href="https://code.activestate.com/recipes/580721-tkinter-remote-debugging" rel="nofollow">https://code.activestate.com/recipes/580721-tkinter-remote-debugging</a></p> <p>It makes more easy to create tests for Tkinter.</p> <p>Install rpyc:</p> <blockquote> <p>pip install rpyc</p> </blockquote> <p>Save the code below to a file named for example tkinter_selenium.py.</p> <p>This is the usage:</p> <blockquote> <p>python tkinter_selenium.py [-h] [-p PORT] filename</p> </blockquote> <p>where filename is the path to main file of Tkinter application, and port is an optional port number for the remote interpreter. Otherwise it uses default port.</p> <p>Then in another python interpreter you can interact with the application. For example, write:</p> <pre class="prettyprint"><code>import rpyc c = rpyc.classic.connect("localhost") c.execute(""" from Tkinter import Button, Toplevel import tkMessageBox responsive_button = Button(Toplevel(), text="It's responsive", command = lambda:tkMessageBox.showinfo("alert window", "It's responsive!")) responsive_button.pack() """) responsive_button = c.eval("responsive_button") responsive_button.invoke() </code></pre> <p>(This example only works for Python 2. For python 3 use "tkinter" instead of "Tkinter" and so on)</p> <p>Use port keyword argument to <em>"repyc.classic.connect"</em> if you want a different port number than default port. For example:</p> <pre class="prettyprint"><code>import rpyc c = rpyc.classic.connect("localhost", port=8000) </code></pre> <p>For the selection of tkinter widgets, I have this other trick:</p> <p><a href="https://code.activestate.com/recipes/580738-tkinter-selectors" rel="nofollow">https://code.activestate.com/recipes/580738-tkinter-selectors</a></p> <p>Using this remote debugging utility and selectors makes easy to test tkinter applications similar to selenium.</p> <p>This utility could be used not only for Tkinter applications. It could be used also for wxpython, pygtk and pyqt applications.</p> <p>NOTE: Interact with remote application using python of same version. If the application is running using a Python 2 interpreter, use a python 2 interpreter for remote interaction. Similarly use a python 3 interpreter for remote interaction with a python 3 application.</p> Encode multipart form data for uploading files via POST (Python) 2013-09-22T21:27:24-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578668-encode-multipart-form-data-for-uploading-files-via/ <p style="color: grey"> Python recipe 578668 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/form/">form</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/multipart/">multipart</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>This function lets you encode form fields <em>and</em> files in multipart/form-data format for uploading files via HTTP POST.</p> Type checking using Python 3.x annotations (Python) 2013-05-23T22:46:19-07:00David Mertzhttp://code.activestate.com/recipes/users/4173018/http://code.activestate.com/recipes/578528-type-checking-using-python-3x-annotations/ <p style="color: grey"> Python recipe 578528 by <a href="/recipes/users/4173018/">David Mertz</a> (<a href="/recipes/tags/annotations/">annotations</a>, <a href="/recipes/tags/check/">check</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/type/">type</a>, <a href="/recipes/tags/typecheck/">typecheck</a>). Revision 5. </p> <p>Some other recipes have been suggested to allow type checking by various means. Some of these require the use of type specification in a decorator itself. Others try to be much more elaborate in processing a large variety of annotations (but hence require much more and more convoluted code).</p> <p>The recipe provided below is very short, and simply provides actual <strong>type</strong> checking of arguments and return values. It utilizes an unadorned decorator, rather than manufacture one that is parameterized by types or other arguments.</p> Two Versions Of Bash One Liner INKEY$ Functions... (Bash) 2013-03-28T17:51:32-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578502-two-versions-of-bash-one-liner-inkey-functions/ <p style="color: grey"> Bash recipe 578502 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/inkey/">inkey</a>, <a href="/recipes/tags/shell/">shell</a>). Revision 2. </p> <p>This is probably common knowledge to the professionals but not so much for amateurs like myself.</p> <p>This is a code snippet for the equivalent of BASIC's...</p> <p>LET char$=INKEY$</p> <p>As the timeout parameter cannot be less than 1 second then this is the only limitation...</p> <p>It is a single line function which has a variable "char"...</p> <p>Read the code for more information...</p> <p>There are now two versions, edit out and choose which is best for you...</p> fmap(): a kind of inverse of the built-in Python map() function (Python) 2012-10-06T22:15:17-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578281-fmap-a-kind-of-inverse-of-the-built-in-python-map-/ <p style="color: grey"> Python recipe 578281 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/composition/">composition</a>, <a href="/recipes/tags/fmap/">fmap</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/map/">map</a>). </p> <p>The Python map() function returns a list of the results of applying the function to the items of the argument sequence(s).</p> <p>The fmap() function does the inverse, in a sense. It returns the result of applying a list of functions to a given argument.</p> Text Compressor 3.1 (Python) 2010-10-20T00:50:15-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577433-text-compressor-31/ <p style="color: grey"> Python recipe 577433 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/compression/">compression</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/encryption/">encryption</a>). </p> <p>Compression, encryption, and data codecs are all related fields that most programmers will use ready-made solutions for. This recipe is a shallow adventure into the writing of original code and algorithms that explores a combination of those topics. Based on the work of <a href="http://code.activestate.com/recipes/502202/">recipe 502202</a>, the code here is compliant with Python 3.1 and will run a test of itself when executed. From the program's report, one can gather that the novel procedures compress the source and accurately decompress it again. For those who wish to experiment further with the concept, note that fewer unique characters will yield higher compression ratios.</p> CPU Usage Graph Applet (Java) 2010-03-26T01:12:44-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577145-cpu-usage-graph-applet/ <p style="color: grey"> Java recipe 577145 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/applet/">applet</a>, <a href="/recipes/tags/cpu_usage/">cpu_usage</a>, <a href="/recipes/tags/graphics/">graphics</a>). </p> <p>It calculates CPU speed w/o using any system calls etc. Instead it simply uses a timed counter.</p> Application lock (Python) 2009-08-22T13:00:35-07:00Max Polkhttp://code.activestate.com/recipes/users/4171523/http://code.activestate.com/recipes/576891-application-lock/ <p style="color: grey"> Python recipe 576891 by <a href="/recipes/users/4171523/">Max Polk</a> (<a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/locking/">locking</a>). </p> <p>Ensures application runs only once.</p> Shutdown your PC by using ctypes (Win32 Platform) (Python) 2009-08-03T10:05:38-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576865-shutdown-your-pc-by-using-ctypes-win32-platform/ <p style="color: grey"> Python recipe 576865 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/reboot/">reboot</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/system_programming/">system_programming</a>, <a href="/recipes/tags/win32/">win32</a>). </p> <p>It is true that by using win32 extension python modules, such as win32api, win32con, and win32security, we can easily shutdown the computer with a few steps. However, sometimes your python's runtime environment does not provide win32com module (because it is not a build-in module), we may have to shutdown the pc on our own.</p> <p>By using ctypes, we are still able to shutdown or reboot the PC easily.</p> Hash text simply (PHP) 2012-04-30T21:43:00-07:00Xavier L.http://code.activestate.com/recipes/users/4171602/http://code.activestate.com/recipes/576893-hash-text-simply/ <p style="color: grey"> PHP recipe 576893 by <a href="/recipes/users/4171602/">Xavier L.</a> (<a href="/recipes/tags/crypt/">crypt</a>, <a href="/recipes/tags/extensible/">extensible</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/secure/">secure</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/simple/">simple</a>). Revision 5. </p> <p>This is a simply and extensible script that can be used to rapidly has any amount of text using PHP's hash() built-in function.</p> <p>It recognizes when the page is loaded for the first time and displays a form with options. Afterward, it display the selected hash(es), with a link at the bottom to start again.</p> Serve static web content from within a gzipped tarball to save space using CherryPy (Python) 2009-03-31T18:24:06-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/576706-serve-static-web-content-from-within-a-gzipped-tar/ <p style="color: grey"> Python recipe 576706 by <a href="/recipes/users/4169722/">Dan McDougall</a> (<a href="/recipes/tags/cherrypy/">cherrypy</a>, <a href="/recipes/tags/compression/">compression</a>, <a href="/recipes/tags/embedded/">embedded</a>, <a href="/recipes/tags/gzip/">gzip</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/routes/">routes</a>, <a href="/recipes/tags/web/">web</a>, <a href="/recipes/tags/web_server/">web_server</a>). </p> <p>This code lets you store all of your static website content inside a gzipped tarball while transparently serving it to HTTP clients on-demand. Perfect for embedded systems where space is limited.</p> pyglet or cocos2d rich text label (Python) 2008-07-31T12:32:43-07:00Thomas Woelzhttp://code.activestate.com/recipes/users/4165953/http://code.activestate.com/recipes/576399-pyglet-or-cocos2d-rich-text-label/ <p style="color: grey"> Python recipe 576399 by <a href="/recipes/users/4165953/">Thomas Woelz</a> (<a href="/recipes/tags/cocos2d/">cocos2d</a>, <a href="/recipes/tags/pyglet/">pyglet</a>). Revision 7. </p> <p>Small changes to the Label classes in pyglet and in cocos2d, so the label (in cocos2d or pyglet) accepts pyglet's attributed text as well as plain text, and changing style of part of the document is also possible after it is created.</p> Simple calls to Python functions from command line or shortcut (Python) 2008-07-22T02:17:06-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528891-simple-calls-to-python-functions-from-command-line/ <p style="color: grey"> Python recipe 528891 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>This module allows one to create a command to call a Python function from the command line, and to call that function with arguments from the command line without using getopt or optparse</p> Blackjack (Python) 2007-06-25T12:23:47-07:00Mike McGowanhttp://code.activestate.com/recipes/users/4064733/http://code.activestate.com/recipes/522992-blackjack/ <p style="color: grey"> Python recipe 522992 by <a href="/recipes/users/4064733/">Mike McGowan</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>Text based command line blackjack. Hit and Stand are the only available options. Clean, commented code. Comment out any "clear()" statements you see if you want to run it in Idle.</p> Implementing an Immutable Dictionary (Python) 2007-06-13T09:23:09-07:00Aristotelis Mikropouloshttp://code.activestate.com/recipes/users/2881737/http://code.activestate.com/recipes/498072-implementing-an-immutable-dictionary/ <p style="color: grey"> Python recipe 498072 by <a href="/recipes/users/2881737/">Aristotelis Mikropoulos</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 7. </p> <p>The implementation of a dictionary, whose items cannot be reset or deleted, nor new can be added.</p> Windows shortcuts import hook (Python) 2006-05-10T02:57:05-07:00Brian Quinlanhttp://code.activestate.com/recipes/users/118989/http://code.activestate.com/recipes/496692-windows-shortcuts-import-hook/ <p style="color: grey"> Python recipe 496692 by <a href="/recipes/users/118989/">Brian Quinlan</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>This simple import hook can resolve Windows shortcuts refering to directories. This allows Windows users to simulate UNIX directory structures which contain symlinks.</p> How to use Cyrillic fonts in ReportLab PDF library (Python) 2005-08-06T10:02:14-07:00Yuriy Tkachenkohttp://code.activestate.com/recipes/users/2011468/http://code.activestate.com/recipes/438817-how-to-use-cyrillic-fonts-in-reportlab-pdf-library/ <p style="color: grey"> Python recipe 438817 by <a href="/recipes/users/2011468/">Yuriy Tkachenko</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>By default ReportLab PDF library <a <a href="http://href=%22http://www.reportlab.com%22%3Ehttp://www.reportlab.com" rel="nofollow">href="http://www.reportlab.com">http://www.reportlab.com</a></a> doesn't allow easy using Cyrillic fonts for generating PDF documents. The following example explains how to use any font in the Adobe AFM ('Adobe Font Metrics') and PFB ('Printer Font Binary') format (aka Type 1) which supports Unicode Cyrillic characters (glyphs). It assumes that you have font files named 'a010013l.afm' and 'a010013l.pfb' in the same directory with this example. The font files can be found in /usr/share/fonts/default/Type1 directory in many Linux distributions.</p> Broken Test decorator (Python) 2006-01-12T09:07:50-08:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/466288-broken-test-decorator/ <p style="color: grey"> Python recipe 466288 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>broken_test_XXX(reason) is a decorator for "inverting" the sense of the following unit test. Such tests will succeed where they would have failed (or failed because of a raised exception), and fail if the decorated test succeeds.</p> Builtin i18n _() function in a multi-threaded environment. (Python) 2006-01-02T10:57:10-08:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/465756-builtin-i18n-_-function-in-a-multi-threaded-enviro/ <p style="color: grey"> Python recipe 465756 by <a href="/recipes/users/1643324/">Martin Blais</a> (<a href="/recipes/tags/web/">web</a>). </p> <p>Injecting _() in the __builtin__ module in order to inject global functions _() and N_() is common in applications which need i18n. This is a variation on the theme that does this in a multi-threaded environment, using threading.local from Python-2.4.</p>