Python Logo

Drawing Shapes With The Tkinter Canvas Element In Python

24th October 2021 - 16 minutes read time

The Canvas element that comes with Tkinter is quite versatile. Out of the box you can draw simple basic shapes like squares and circles, but also lines and more complex shapes made up of points. You can even add text and images to the canvas.

This article will go through all of the different types of items you can draw using the Tkinter Canvas object.

Before getting into that, it's important to understand how coordinates are used on a canvas object. Drawing items on a canvas requires the use of an x and y coordinate to pinpoint where the item is to be drawn. All points are relative to the top left hand corner, so a coordinate of 0,0 would be right in the top left corner.

Drupal Logo

Drupal 9: Loading All Routes From A Module

17th October 2021 - 14 minutes read time

When creating Drupal modules I like to keep the hard coded components to a minimum. This helps when changing parts of the module in the future as hard coded links and other elements will require manual intervention and slow down maintenance. Sometimes, though, this isn't an option as you just need to have a few routes in your *.routing.yml file that point to controllers or forms within your module.

I had a situation today where I was looking to load all of the routes that are contained in a module. I could then construct a page of links that would handily point to different parts of the module or feed those links into a sitemap. This meant that I wouldn't need to hard code this list into a controller, I just needed to load all the routes and print that list out instead. Especially handy if I ever added or removed a route as that would mean that list would update without me having to do it manually.

Python Logo

Creating A Word Clock With Python And Tkinter

10th October 2021 - 17 minutes read time

I recently saw a design of a physical clock that inspired me to go about creating one using Python and Tkinter. The clock was essentially a wall of letters with lights behind that light up depending on what time it is. Without the lights the clock looks like a jumble of letters, it is only when the light is on that the relevant time is displayed.

The original clock was of a proprietary design so I set about creating one that was based more on an open source clock design that I found. That clock design I found didn't display the AM or PM of the time, so I ended up tweaking that design a little anyway.

Python Logo

Converting The Current Time Into A Sentence In Python

3rd October 2021 - 16 minutes read time

Changing time into different formats is quite a common thing to do in programming. I have seen examples that change times into Roman numerals and other formats, but I realised I hadn't seen any code that changed the current time into a sentence. This means converting a numeric time value into a sentence that can be read. For example, the time 9:05 can be read as "it is five past nine", or if the time is 9:00 then it would read "it is nine o'clock". At it turns out, there are only a few rules that govern doing this.

Python Logo

SOLID Principles In Python

26th September 2021 - 18 minutes read time

SOLID is a set of object oriented design principles aimed at making code more maintainable and flexible. They were coined by Robert "Uncle Bob" Martin in the year 2000 in his paper Design Principles and Design Patterns. The SOLID principles apply to any object oriented language, but I'm going to concentrate on what they mean in a Python application in this post.

I originally wrote about SOLID principles with PHP as the basis of the article, but as the lessons here can be easily applied to any object oriented language I thought that I would re-write it with Python in mind. If you are familiar with only PHP or Python then this will be a good learning resource on learning the other side.

Python Logo

The Basics Of Object Oriented Programming In Python

19th September 2021 - 25 minutes read time

I have been learning Python recently, and whilst I'm familiar with PHP and other C-like languages, Python has been a bit of a challenge to get used to due to the different syntax. This is especially the case with objects as although Python is object oriented there are a couple of gotchas when creating custom objects. This article sets out how to use classes and objects, but I will assume that you have a basic understanding of object oriented programming in other languages.

Python Logo

A Guide To Using Different Tkinter Widgets In Python

12th September 2021 - 14 minutes read time

The Python Tkinter module has a lot of widgets built in that provide different parts of the graphical user interface. Everything from labels to buttons are all widgets and they extend from the base Widget class.  Some Widgets are easy to put together, but others require a little bit of configuration to get working. This article will show each Tkinter widget available and how to add each one to an application. There are two sorts of Widgets available. Simple widgets exist in their own right, but some widgets are made to contain other widgets.

Python Logo

Slicing Arrays and Strings Using The Colon Operator In Python

5th September 2021 - 7 minutes read time

Strings and arrays can be manipulated in a number of ways in python. Splitting a string or an array into sections can be done using the section slicing that is built into the core python syntax and uses the colon (:) symbol.

Generating a simple array in python is straightforward, the following creates an array of 9 elements.

an_array = [1,2,3,4,5,6,7,8,9]

To grab a particular element of the array you can use the simplest section slicing syntax. The number in the square brackets represents the position of the item to be returned. Remember that the counting starts at 0, so using 1 will return the item from the "second" position.

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.

Drupal Logo

Drupal 9: Stubbing API Modules For Fun And Profit

22nd August 2021 - 21 minutes read time

If you've been building websites sites for a while you will realise that no site lives in isolation. Almost every site you build integrates with some form of API, and this is especially the case for the more enterprise sites where data is often synchronised back to a CRM system or similar. Drupal's hook and service architecture means that you can easily build integration points to that API to pull in data.

Pulling in data from an API into a Drupal site means installing an off the shelf module or creating a custom module to provide the integration. What route you go for depends on the integration, but for enterprise sites the API is quite often very custom to the business. I have even seen APIs being built at the same time as the site that it needs to integrate with, which is especially the case for startups and new businesses.