Python

Posts about the Python scripting language.

Python Logo

Getting Started Creating Graphical User Interfaces With Python And Tkinter

29th August 2021 - 12 minutes read time

When starting out learning Python you will probably start by looking at the command line a lot. This is understandable as it's important to learn about variables, conditionals and loops and the command line is the best way to start out with the language.

I have always found it useful to quickly introduce graphical user interfaces when learning programming as it allows people learning programming to have something they can engage with. Being able to see the output of their program in real, tangible things that they can interact with.

There are a couple of different libraries in Python that can be used to create user interfaces, and perhaps the first one Python developers tend to learn is Tkinter or the "Tk interface" module. This is a Python binding to the Tcl/Tk toolkit to create graphical user interface.

Python Logo

Pretty Print JSON With Python

23rd December 2014 - 5 minutes read time

JSON is a very common data format, but reading it can be a little difficult, especially if the JSON contains very little white space. If you have Python 2.6 or above you have use the json.tool to format the JSON so that you can read it correctly. This is also a good way to validate JSON strings that you have had to hand edit before they cause errors upstream.

If you have a file called file.json, which contains a bunch of JSON output, you can use Python to format this into a readable structure in the following way.


python -m json.tool file.json

You can also write this output directly into a file, in this case called formatted.json.


python -m json.tool file.json > formatted.json

The most common use of this tool is to pipe JSON to it from the command line (either directly or via a script).

Python Logo

Using Python To Beat The 2012 Olympic Google Doodles

11th August 2012 - 6 minutes read time

The other day I was inspired by a story on Reddit about a guy who had created a Python script to automatically play the olympic hurdles Google Doodle. The Python script just passed the correct keyboard commands to the game so that the game was finished in 1.5 seconds, earning a gold medal. The problem was that his script was specifically for Windows, so I set about trying to create a Linux version that I could run on Kubuntu.

After a bit of research I found a decent plugin that sends keyboard commands through Python called uinput. To get this installed I had to add the following lines to /etc/apt/sources.list.

Python Logo

Multi-line Comments In Python

13th April 2012 - 3 minutes read time

Python doesn't officially support multi-line comments, but there is a way of implementing the same functionality using an existing language construct. Single line comments in Python are written like this:


# This is a single line comment.

Multi-line comments are not officially supported in Python. That said, you can create the same effect in Python by using a multi-line string. Unless it is part of a docstring at the start of a class, function or module then it parsed into a string variable, but nothing is done with it.


'''
This is
a multiline
comment
'''

Because this is a multi-line string it is therefore parsed by Python, so you will need to be careful what you put in it.

Python Logo

Stopping Code Execution In Python

13th April 2012 - 3 minutes read time

I am currently in the process of learning Python, so I thought I would start a series of mini blog posts detailing different things that I have found useful whilst learning how to use the language.

To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program running. It is the most reliable, cross-platform way of stopping code execution. Here is a simple example.