Video 0

09/08/2012

Simple one on for loops, while, if, else.
Plus a factorial program.
Made from the Pi (via VNC)

Bubble Sort

08/08/2012

Watch “Python Tutorial 8” on YouTube

07/08/2012

Tutorial Video 7: a template for Pygame programs

07/08/2012

Tutorial Video 6: A GUI program that does “Magic 8 Ball”

06/08/2012

Fifth Tutorial Video – a simple Hangman game.

04/08/2012

Watch “Antiloquax Python Tutorial 4” on YouTube

03/08/2012

Latest Python Tutorial

02/08/2012

Python Tutorial Video

22/07/2012

Simple tutorial on Fibonacci numbers.

This is my first ever video tutorial. It’s fairly simple (no, I didn’t use a recursive algorithm in this example).

I hope you enjoy it,

 

Update

Here’s a version that uses recursion and had a GUI.

GUI Fibonacci Tutorial

antiloquax

A GUI Python Calculator

21/07/2012

Click on image to download the code.

Python Calculator

Here’s a little calculator I wrote.

I’ve recently had a look at this and fixed the divide by zero bug.

The links now point to updated code on github.

It you want to look at the code and/try it out, just click on the picture. Or click here.

If you find any bugs, let me know.

Read on if you want to know how the code works.

The program begins by importing tkinter, to handle the GUI. Straight after that we set up a “class” called “Calc()”. In some of the programs we’ve looked at so far, we have been using pre-defined “classes” and, therefore, the Object Orientated approach to programming. What’s different this time is that we are creating a class from scratch.

The first “method” that we create for this class of objects is a “constructor”. This section of code is run automatically whenever and instance of “Calc() is create. The object has some variables associated with it. Total is a running total of the sums we have done so far – so we set it to zero initially. “self.current” is a string in which we hold the numbers that are being entered via the GUI. Then we have three “flags”. I’m not sure if this is the most effective way to do this, but it seems to work okay. The “new_num” flag is set to true if the calculator is expecting a new number (eg after the user has pressed “+” etc.) There’s also a flag for “op_pending” which tells if there is still a calculation to do. The flag “self.eq_flag” keeps a record of whether equals has just been pressed. I needed these flags to make the calculator work as expected. For instance, you can press “equals” when there is no operation pending, and the number will stay the same. You can press an operator after “equals” and that action will be done to the current total. Or you can start a new number after pressing equals and the previous target is discarded.

Each instance of the class I am creating will have these variables, and also some “methods”.  Methods look just like functions. This one controls how the calculator responds to a number being pressed. First it takes the text from the box. If the new_num flag is set to “True”, it ignores what was in the text box and starts a new number with the button-press. Of course it then sets the “new_num” flag to false, so that we can add more digits to this number.

If we are not looking for a new number, it will continue adding the digits we have pressed onto the “self_current” string (at this point we are treating whatever is input as a String). The method also checks that the used hasn’t tried to have 2 decimal point symbols. Only the first one will actually be accepted.

The method ends by deleting everything in the text-box and printing the “self_current” string out again.

Now we deal with the method that gets called when we press equals. This is a very simple method! If there is an operation pending (ie. a sum to do), then we do the sum. Otherwise, pressing equals has no effect

Next comes the “do_sum” method. This starts off by converting the string in “self.current” to a floating point number. It then does the operation that is stored in “self.op”, performing it on “self.total” and “self.current”. Then it displays the result in the text box and sets the new_num flag to True.

This method defines what happens if one of the “operation” buttons on the calculator is pressed. If we have an operation “pending”, the we work that out first. Otherwise, we check that the total variable holds the correct total and set the op_pending and new_num flags to true, so that we are ready to find out which value the user wants this operation applied to.

The next three methods sort out some simple matters. The cancel, all_cancel and “+/-” buttons. As you can see, all_cancel is the same as cancel, but with the additional zeroing of self.total. And the plus / minus button just inverts the sign of whatever is in the text-box at that moment.

And that’s it for the workings of the calculator. The rest of the program just sets up the GUI. I am not going to spend too long on this, except to show you how I  made some of my buttons with a loop.

 

By the way, if you haven’t seen “lambda:” before, this is a really useful thing. Usually, when you specify a function in the command part of a button’s creation, you leave the brackets off. If you leave them on, the program will behave as if the button has been pressed, even when it has not! Lambda lets you get around this, which is of course essential if you want to pass some parameter to that function.

Here’s the loop that makes the buttons 1-9.

button

 

 

 

So first I created a little string with my numbers in the order they appear on the buttons on a number pad. Then I use j and k to iterate over all the positions in my grid in which I want a button. I use my version of the Button class which will allow me to use my built-in command-setting method.

I’m not going to write any more than that. You can download the code if you want to see the whole thing.

Please let me know if you spot any mistakes or ways to improve this.

antiloquax