WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 482 reviews for our classroom and online training
Playing Wordle in Python
A modest Python program to mimic the popular online Wordle game, using a Tkinter form and some basic classes.

Posted by Andy Brown on 31 January 2022

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Playing Wordle in Python

Not to be outdone by my colleagues' impressive Wordle games in Excel VBA and Power BI, I thought I'd get Wordle working in Python.

I think the game works, but I haven't tested it exhaustively.  The opportunity to share your results will be in version 2 of the software!

Here's what a typical screen looks like:

Wordle

The word was DEMUR on this occasion, if you're wondering.

Installing the game

To make the game easy to run, I haven't used any external modules - just these:

Modules imported

The only modules used by this Wordle program.

I've also included all of the code in a single file, which you can download here.  This also includes these two text files:

File Contents
words.txt A list of all the 12,973 valid five-letter words that you can guess in Wordle.
targets.txt A list of the 2,315 possible words you might have to guess.

You'll need to set the path to where these files are on your computer at the start of the program wordle.py:

File path

Make sure you change this file path.

Running the game

You can run the program as normal in Python:

Keyboard

Click on the letters that you want to try. Use BACK to change a letter, and ENTER when you have a valid five-letter word.

Each time that you run the program it will pick a new random word from the appropriate text file.

Some notes about the programming

The program uses TKinter for its graphical interface, a Python module I neither know particularly well nor love (this probably shows with the slightly clunky way I draw the buttons).  I've used two classes:

Class Used to
Button Hold each of the letter buttons in the keyboard
Letter Used to hold each of the letters in the 6 rows at the top.

I've also added a fair few additional properties/attributes to the TKinter.Tk fom class.

I had fun creating this, although suspect I haven't quite ironed out all of the bugs.  Let me know your thoughts! 

This blog has 0 threads Add post