vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You can put an image and text on a Tkinter button widget. This small code shows you how. The GIF image file you want to use for the button should be in the working directory, or you have to give it the full path. Incorporate the Python Image Library (PIL) for other image formats.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

For those of you who think that an interpreted language like Python has to be slow, here is a small surpise. This snippet will draw a fair sized Mandelbrot set in a relatively short time. I cheated just a little, drawing the set in only black and white. Hey, nothing much for a guy that comes from a poor neighborhood, where even the rainbow was in black and white.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Just a relatively simple calculator using the Tkinter GUI. It has a few nice features such as correcting division by an integer (for older Python versions), error trapping, to and from memory buttons, and an editable display. The editable display allows you to backspace mistakes, and also to enter things not on the key pad, like hexnumbers. For instance if you enter 0xFF and press equals, it will give you the decimal (denary) equivalent of 255

The program also has a guard against the bad guys that like to abuse the underlying eval() function to wipe out files.

I wrote the code in detail, so you can hopefully figure it out. You can fancy up the calculator quite a bit, just experiment with the code.

See also the updated version at ...
http://www.daniweb.com/software-development/python/code/467452/updated-tiny-tkinter-calculator-python

sneekula commented: nice code +12
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Sometimes when you get a large check from your employer, the value is written out in words. I get those all the time of course. So I wrote this little Python code to convert an integer value to english words. Numbers as high as 999 vigintillion can be used. In case you don't know, a vigintillion is 10 to the power 60. That is what our national deficit will be soon.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

If you press any key on your keyboard, this small Tkinter GUI program will tell you which key and what type of key has been pressed. Great for applications where a simple key stroke is required.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A demo of the Tkinter listbox widget that shows you how to create the listbox, make it scrollable, position it within a grid, load it with data from a file, select a line of data with the mouse and display it. Also, sort the lines, add a line, delete a line, edit a line and save the modified listbox data.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This shows you how to draw a colorful bar graph of a data set using the Tkinter canvas and rectangles. An attempt has been made to use most of the canvas area for the graph. The bars are spaced and labeled with the corresponding value. The x-axis is simply the index of the data set.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Another famous fractal called the Sierpinski Triangle named after Polish mathematician Waclaw Sierpinski. He seemed to have played around with it the most. To state it simple, you start with an equilateral triangle and then form smaller triangles by connecting the midpoints of each of the sides. Some folks shade alternate triangles to pretty up the pattern. We are using the canvas of the Python Image Library (PIL) to draw the triangle and save the final image to a file.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

There are situations in Python code when a function is used as an object rather then a function call. Handling arguments becomes a mild problem, that can easily be handled by wrapping the function object and its arguments. One common way is the use of a curry function or class. This snippet shows the use of a curry function in a short Tkinter GUI dialog demo. Here the function objects appear in a dictionary of dialogs and the button command associated with a specific dialog.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Some computer languages have a goto statement to break out of deeply nested loops. Python has chosen not to implement the much abused goto. There are other, really more elegant, ways to accomplish the same outcome. Here are three examples.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Because of their demand only nature, generators are ideal for finding the combinations of larger sequences. This code applies a Python generator to find the combinations of the sequence. Since permutations are a special case of combinations for all the items in a sequence, I have expanded the result to give the permutations of a string.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This short Python code gets the local time from the PC as a formatted string using time.strftime('%H:%M:%S'). The time string is displayed in a label using a larger font. A recursive function checks the time five times per second, and updates the time string, if it has changed. Five times per second may sound like overkill, but keeps the display from acting spasmodic.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Yet Another Calculator using wxPython, smaller, but more advanced than the previous one. Wrote this one to test the Curry class, but it didn't work well. So there is no curry there. I found out that one has to be careful with the eval() function. If you write 012 instead of 12, it looks at 012 as an octal number with a denary value of 10. A few safeguards are included.

As you look at the code, you will see that most of it is simple copy and paste with minor modifications. I have added a power button, so you can do square roots like 25**0.5, which should give you a 5.0. Well, enjoy what's there, and improve on it!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This code sample shows how to represent employee data as a list of dictionaries. This list can be sorted and grouped by dictionary keys. In this particular example, I demonstrate how to calculate each department's average age from the data after sorting and grouping. The code is heavily commented to aid in understanding.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Another exercise in applied geometry. This time we use the Tkinter GUI canvas and its create_line() function to draw a triangle, or a series of connected triangles to create something that looks like fancy art work. You might be able to impress grandmama with that one!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Just a small Python program to calculate monthly payments and other costs. I have compared it with some of the calculators available from many of the online mortgage companies, and results seem to match.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Py2Exe takes your Python code file and packages it into an executable file that you can distribute to a client that does not have Python installed. The executable file contains your byte code, all the required modules and the Python interpreter. It is not a small file.

The snippet is a template to run Py2Exe. For convenience, all the needed modification are done to the last line of the snippet. It is best to have this snippet, modified to accept your code file, and your code file in the same directory. Just follow the instructions in the remark section.

Note that Py2Exe only works with the Windows OS, and is freely available from:
http://www.py2exe.org/

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

An example showing how to use the wx.media.MediaCtrl() widget to play MIDI, MP3, WAV, AU, AVI and MPG audio and video files. I have only tested it with Windows. If you are very ambitious, you could use a random graphics display with the sound, or tie the slider position to a slide presentation.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Here we experiment with a gauge or progress bar made from a label displaying a variable number of spaces in color. In this example, we use from 0 to 100 spaces. This label based gauge allows you to choose many different colors, and you can even change the color as the gauge reaches critical levels. The custom gauge is bound to a wx.Slider to test it.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A permutation is the arrangement of a set of items in different order. One interesting application is the rearrangement of characters in a word to create other words. If all the n characters are unique, you should get n! unique permutations. You can make a list of words unique by converting it to a set.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A list comprehension derives a new list from an existing list, and was adopted by Python from the Haskell language. This snippet tries to explain the development of list comprehension and shows a number of uses, including nested list comprehension. The benefits are simplicity (after an initial learning bump) and in most cases speed improvements.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Early versions of Python used a hybrid of samplesort (a variant of quicksort with large sample size) and binary insertion sort as the built-in sorting algorithm. This proved to be somewhat unstable, and was replaced in version 2.3 with an adaptive mergesort algorithm. I am comparing several rudimentary sorting routines, translated to Python from Narue's C code by my friend Micko, with the Python built-in sorting routine. The new Python24 @ function decorator is used to implement the timing.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This code snippet allows you to list the files in a folder sorted by "last modified date". The tuple returned by the os.stat() function contains amongst other things the last modified date, which is then converted by the time.localtime() function to a tuple that is ideal for sorting. You can select the folder and the type of file you want to display.

Lardmeister commented: practical code +10
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Version 2.6 of wxPython has support for playing animated gif image files. The example sets up a panel on a frame and then uses wx.animate.GIFAnimationCtrl() to do the hard work. Actually pretty simple code.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The lowly soundcard in your PC has fascinating capabilities. All you need is a module that can handle the Musical Instrument Digital Interface (MIDI). Tell the soundcard which instrument to play, specify the musical note, the duration, the beat and other things. The sound chip makes 127 instruments available and a whole set of drums and things. You can play a number of different instruments at the same time. You will be surprised about the soundeffects you can create! The Python module is called PMIDI, written by the great sound expert Peter Parente (UNC Assistive Technology), a free download from sourceforge.net

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Boo is new, well at least relatively new. If you are familiar with Python and C#, you can feel right at home. Most of Boo is written in C# by a Python devotee. Boo has static typing, but also mimics dynamic typing by inference. This way you are not in a complete straight-jacket. It runs in the NET or MONO environment, can be interpreted or compiled and a Python-like shell is available too. Boo is open source, developed enough to allow for serious experimentation. For those of you who have used the SharpDevelop IDE for C# there is a Boo plug-in. This code snippet will compile to a 10k executable file and can be distributed along with a 64k Boo.Lang.dll (runtime DLL) to other .NET or Mono computers. A utility to convert Boo to C#, or C# to Boo has been published.

ddanbe commented: Thanks for sharing! +14
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You need the Python tkSnack module to play notes of a given frequency and duration on your external sound system (attached to the PC's sound card). The code snippet gives just a tiny hint about the capabilities of tkSnack. It works with Tkinter, normally part of the Python installation. The module comes to you courtesy of the researchers at KTH and the University of Stockholm in Sweden. More information is at their website http://www.speech.kth.se/snack/

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The wx.ComboBox is a combination of an editbox and a (dropdown) listbox. It let's the user slect an item from the listbox and puts it in the editbox. In this snippet we use two comboboxes, each used to select an area unit of measurement. Once selected, an area value can be converted from a unit like acres to another unit, for instance squaremiles. Additional wxPython widgets like wx.Frame, wx.Panel, wx.ToolTip, wx.StaticText, wx.TextCtrl, and wx.Button are part of this demonstration. The layout is done entirely with upper left corner position (x, y) tuples and size (width, height) tuples. This demo is supposed to illustrate the layout too, so I used plenty of key-words like pos=wx.Point(x, y) and size=wx.Size(width, height).

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The Fibonacci series of numbers was used by Leonardo of Pisa, a.k.a. Fibonacci (around the year 1200), to describe the growth of a rabbit population. The series has been noted to appear in biological settings, such as the branching patterns of leaves in grasses and flowers. We take a look at two different ways Python can get you these numbers. The generator function will add the next number to the series each time it is called. The recursive function gives the Fabonacci value for the parameter given.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The printer seems to be a much ignored computer peripheral when it comes to programming. This little code snippet explores one way to send text to the default printer. It actually draws the text into an imaginary box on the paper. You can specify the upper left and lower right corners of this box.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This snippet explores the change of a hexadecimal (base 16) string to a denary (base 10) integer and the reverse. Interestingly Python allows the representation of the hexadecimal string in two different forms. For instance 255 can be changed to 'FF' (also 'ff') or '0xff'. Any of these hexadecimal strings return the correct value of 255 again.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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:
http://www.daniweb.com/code/snippet393.html

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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 program won't work. The snippet uses the ever popular jpeg image and wxPython to display the image. I have used a panel here, but you could also use a button or a label.

For your sanity, you do not type the image string, you let a small Python program create it, then copy and paste it into your code. For an example how to do this see:
http://www.daniweb.com/code/snippet394.html

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Crypting with xor allows one to write one function that can encrypt and decrypt a file. In this example a text file is used, but it could be an image file as well. Once you have created the encrypted file you simply send it through the same function again to decrypt it. The password is looped against the file, but you can get tricky and spell forward then backward, odd/even, or every odd character twice and every even character once. This will make it harder for grandma to decipher your secret files.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

ee_programmer, I posted the answer in the Python forum so the format is proper.
Thread title: A List of Class Objects

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A list of class objects mimics a C array of structures. The snippet explores how to setup the list, and sort the list according to a selected attribute. Then we use a format string to display the sorted list. Take a look at how to search the list. All in all an easy way to handle structured data.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This code shows you how to build a multiline story string that should display similarly to way it shows in the code. It should be relatively easy to maintain.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Python has some powerful functions, like the math expression evaluator eval(). You can embed this function and others in your Development C++ code. All you need to do, is to download and install the free Python Development Pak and feed the Python code in the form of a string to the interpreter. Your executable file will work as long as there is access to the Python24.dll (part of the pak). Looks to me like you could use a Python program to create this C++ program embedding a Python code file. A Python to C++ translator is born!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Do I have to spell it out for you? This short C code will do just that. It will spell out an integer number in English words. The banks do that on large checks, and it would be nice to get one of those every now and then.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I have joined the thousands who have done it before, and have compared a number of sorting routines. The sorting is done on the same random-integer arrays. No surprises, quicksort wins this simple comparison hands down. There are clever combinations of sorting routines that are faster, like the snippet by Narue shown in:
http://www.daniweb.com/code/snippet61.html
There is also an expert treatise on sorting at:
http://www.eternallyconfuzzled.com/tuts/sorting.html

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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).

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This program takes a text string and creates a list of words. Any attached punctuation marks are removed and the words are converted to lower case for sorting. Now you can generate a dictionary of the words and their frequencies. The result is displayed using a sorted list of dictionary keys. A fair amount of comments have been added to aid in the understanding of the program code.

tyrenwick commented: i doubt you see this, but your code was extremely helpful even 10yrs later and on a newer version of python. thank you very much!! +0
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Python has a module specifically made for timing built-in or owner coded functions. This code snippet explains how to use this feature.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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 of computer languages.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Python uses C syntax to perform bitwise operations. Take a look at code that introduces you to bitwise operations at the binary level.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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().

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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 surprise to me.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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.