15,185 Topics
| |
Using a wxPython GUI program to find the position of the mouse when clicked within the frame/window. Also sets the color of the frame and changes the cursor to a pencil. The title bar of the frame displays the coordinates of the mouse position. | |
Suppose you are a medical researcher studying diabetes. Your boss has given you a big chart of data from diabetes patients. Each row of the chart has information for one patient. Each column of the chart is a health-related statistic, such as height, weight, age, blood pressure, cholesterol level, etc. … | |
Create a dictionary with char:count pairs, change the dictionary to a list of (char, count) tuples, sort the list by character and display the result. That's all there is to counting and displaying the characters in a given string. Just a few lines of Python code. | |
This short code lets you create a base64 encoded image string, that you can copy and paste into your Python program code to display relatively small embedded images. For a typical application see: [url]http://www.daniweb.com/code/snippet393.html[/url] | |
Small image files can be embedded in Python code using the base64 encoded string of the image. If you have to present a number of small fixed images in your program, embedding might be easier than including a set of image files. Attached files can be lost, and than your … | |
Here is a generator function using find() to do a search for all the occurances of a substring in a text. It will give you all the positions/indexes within the text where the substring is located. The sample text here can be replaced by text read in from a file. | |
This snippet takes a look at Python file handling. Different ways to write and read text files, zipped files and memory streams. Investigates how to access only part of a file. Also explores the "read" of a binary image file and performs a hex-dump of the data. | |
For loops in Python are quite versatile. The little keyword "in" allows you to loop through the elements of all kinds of sequences, may the elements be numbers, characters, strings, objects like tuples and more. Here is an example comparing range(), xrange() and a generator similar to xrange(). | |
Years ago I wrote a little screen-saver in Delphi that randomly put colorful circles all over the screen. A hit with the secretaries in the office. I modified a simple Tkinter based snippet to put the same colorful circles all over a window form at random locations, random radii and … | |
VPython's fame is with 3D animated modeling, but it's plotting abilities, while not flashy, are easy to understand and very useful. This program uses VPython to plot math functions y = sin(x) and y = cos(x) and y = sin(x)*cos(x). | |
Here's a cute little encipher/decipher program with a Tkinter GUI I wrote a while back. It's an implementation of a derivative of the Vigenere algorithm; the algorithm is taken from Laurence Smith's Cryptography: The Science of Secret Writing, Amazon link here. It's a dated book (and the technique itself is … | |
Python has a module specifically made for timing built-in or owner coded functions. This code snippet explains how to use this feature. | |
The psyco module has been around for a while and has been used to speed up the Python interpreter. For those of you who think speed is all important, take a look at a typical example. Psyco is similar to Java's just in time compiler. How does psyco do it? … | |
Python24 introduces the function decorator that lends itself nicely to the timing of a function. As a sample function we are using the ever popular and rather stodgy prime number generator. The prime number generator seems to exist only to fluster students and to make niggling comparisons of the speed … | |
Just a colorful ten second countdown to New Year. Hope you can learn some code from it. | |
The Python module zlib allows you to compress a typical text string to about one half its original size. A handy feature when you have to transmit or save a large amount of text or data. It saves you time both writing and later reading back the compressed file. The … | |
Python uses C syntax to perform bitwise operations. Take a look at code that introduces you to bitwise operations at the binary level. | |
If you have a Windows computer you can play musical beeps through the internal speaker. In this case it will sound like a very tiny Big Ben, brought to you by the module winsound and its method Beep(). | |
Did you ever want to know how how many square inches are in a square meter? This short Python code allows you to get the answer. It uses a dictionary to simplify the conversions. Could be the start of a nice GUI program using radio buttons. | |
The Python list container can be used for many practical things. In this somewhat light-hearted introspection of modern day politics we are looking at two lists and attempt to clear out elements that shouldn't be in both lists. No offense to anyone is intended, the outcome with the sets was … | |
A while ago I created a code snippet in C for the same question. Solving this question with Python is a lot simpler, and on top of that Python takes care of impossible dates with the appropriate error message. | |
Jumping through a few extra hoops allows you to display the common image format jpeg on a panel of the wxPython GUI window. All you need to do is to read in the image file as a binary, convert to a byte stream image and then to a bitmap. Now … | |
The other day I was looking at my family tree. There are my parents Antonio and Lucy Vegaseat, then my grandparents Alfonso and Ludmilla Vegaseat on my father's side and Roland and Helga Gruenspan on my mother's side. Then come my great grandparents, by now there are eight of those. … | |
While the calculation of the total resistance of two resistors in parallel is simple, this code shows you how to trap a number of potential errors that come with the entry of the required data. All is wrapped easily into a colorful GUI program, due to the simplicity of the … | |
The wxNotebook method from the wxPython module allows you to show a lot of data in a limited window space. It brings up tabbed pages you can click on to open each page. Enough said, run the code and experience the possibilities this humble method gives you. | |
A small script which converts Microsoft Office Documents (Word, Excel, Powerpoint) to Posscript. You can build your own PDF Converter with this script. More Python/Pywin32 examples can be found at [url]http://www.win32com.de[/url] and Python based PDF Converter (for server and client usage) can be found at [url]http://www.goermezer.de[/url] . | |
This is another GUI program using the Tkinter module. It shows you how to change the background and foreground color of a component/widget. In this case it is a button. The button itself is used to toggle the color selection via a color dialog box. Enough comments have been added … | |
Here are five simple snippets, to be used as an introduction to the use of unicode. This is, of course, an on-going thing, and hopefully additional snippets and comments will be forthcoming from the members. | |
I was looking for a somewhat meaningful and interesting example to explain the Python class to beginners. A room full of multilingual students makes this phrase translator a good choice. You can experiment with it and add more languages and phrases. |
The End.