Category: Python

Byte Insight

Twitter

The original inspiration for this whole experiment came from a @Rob Bishop (OCR) Raspberry Pi Recipe Card  (ocr-recipe-card-twitter-led.pdf). The objective is to interact with Twitter using Python.

Python

Python is a highly readable language that is easy to use and learn hence it is gaining popularity as an education language.  However, this should not lead anyone to consider it a child’s language for it is not.  It is very powerful, flexible and platform independent.  It supports many styles of programming from scripting and web design to  GUI/Game development.

Python – Strings

A string is a list of characters and, in Python, they are easy to declare using speech marks.

myquote = “Talk is cheap. Show me the code.”

RPi Twitter Led

This program follows on from the standard Python Twitter experiments.

The original inspiration for this whole experiment came from a @Rob Bishop (OCR) Raspberry Pi Recipe Card.  The objective of the program is to light an LED every time a tweet containing a particular word is sent to a certain person.

TKinter Drawing

TKinter is the easiest way to get drawing in Python.

Tkinter Events

Tkinter employs a type of programming called Event-Driven which means that the program runs in a endless loop waiting for things (events) to happen.  In order for the program to respond to these events it needs to know what to listen for.  

Time

It is not uncommon to use time (or dates) or programs in some way or another.   If you are writing games you might want to record how long something takes or give the player a restricted amount of time.  Thankfully Python has some pretty good libraries (datetime, time) to help you.

Turtle

Python Turtle is based on the Logo Programming Language that has been used as a popular way of teaching children programming for many years.  

TKinter Images

You can attach images to the canvas with

welcomeGif = PhotoImage(file = './images/welcome.gif')
canvas.create_image(canvas_width/2,canvas_height/2, image=welcomeGif)

Where the example image is located in a image subfolder of the program with type gif. If you wanted to change the image to another type during the execution of the program you would need to use a slightly different initial call which creates a reference to the image.

Tkinter Text

You can add text to a TKinter canvas with the following code

titletext = canvas.create_text(10, 10, anchor="nw", fill="red")

This creates a canvas text object with the name title text in the colour of red.  The text is anchored by its North West corner so the top left corner of the text will be positioned at position x=10 and y=10.  You can read more about the options for this line at EffBot.

canvas.insert(titletext, 14, "This is the Dice game")

Then it is inserted into the canvas.  There is a good explanation of the different text methods at info host. If you wanted to specify the font used then you have to first import tkFont and then create a new font that can be used. You can read more about the font options here.

titleFont = tkFont.Font(family='Helvetica', size=24, weight='bold')