Balance Board Project

For the Balance Board Project, we’ve been given the task of coding a timer for a balance board by the HKIS Upper Primary Banana Balance Board Presidents. The objective is to use what we’ve learned about Makey Makeys and coding to make 2 workable timers for the board: one in Scratch, the other in Python.


Let me start with Scratch, since Arjun and I began block coding before attempting the timer in Python.

As you can see in this video, we created a block code in Scratch for the timer before trying anything in Python. I would like to make a slight amendment to the code: I didn’t use “add timer to Scores”, which does add your time to the leaderboard but fails to stop adding if one end of the board remains on the aluminium. Instead, I used “insert timer at last of Scores”. This adds your time to the bottom of the leaderboard once and only once, even if you keep the board down.

Python and Tkinter

More recently in this lesson, we learned about Tkinter, the standard Graphic Interface Unit that Python uses, which is what we used to code the timer (originally, we were going to use Pygame, but Ms. Mok said it was laggy). While I’m still not completely sure about the code used for Tkinter, I have found a working code for one off the internet, and have been trying to take it apart to understand it.

So far, I know that the first thing you have to do to use Tkinter is to import it in Python (I’ve also learned that all imports should be at the start of the code). Importing Tkinter for Python 2 requires a capital “t” for the import to work, while Python 3 requires a lowercase “t”. Since I was making a timer, I also imported time.

In this picture, you can see how the basic functions of the timer works. The function is named StopWatch(frame), and is not the main function. We’ll get to that later.

I’m still trying to figure out how the first 3 functions work, but I understand the concept behind the function “_setTime(self, elap)”. This function sets the time string to minutes : seconds : hundredths of a second. The reason why the integers for the variables (minute, seconds, hseconds) are set that way is because of how the elapsed time divided by 60 is the number of minutes that has passed, and there are 60 seconds in a minute, 100 hundredths of a second in a second.

Above, you can see the 3 functions that will later be called back for the buttons. Start(self), Stop(self) and Reset(self) are pretty straightforward. “Start” starts itself (the timer) if it’s not running already, “Stop” stops itself if it is running, while “Reset” sets it as 00:00:00 once more.

Last but not least, we have the main function that utilizes functions shown previously. First, there’s the syntax “root = Tk()”. A root widget is a little window with a title bar and whatever you decide to add/decorate the window, as well as that there can only be one root widget (it must be created before any other widgets).

I’ve also figured out how to code for buttons in Tkinter. The general syntax should be like this:

Button(root, text, fg, bg, command).pack(self)

Button, root, text and command are straightforward and shown in the picture above. “fg” is the font colour, “bg” is the highlight colour, and pack.(self) indicates where you want the button to go. For example, the use of “.pack(self = TOP)” would put the button at the top of the window, while “.pack(self = RIGHT)” would put it on the right, etc. One important thing to remember when using “.pack(self)” is to capitalize where you want the button to go. Otherwise, it won’t work.

And here’s the final product:

It’s a whole lot of code for a measly window with four buttons.

There are still a couple of things that I need to work out, such as how to connect this whole thing to a Makey Makey and the balance board, but so far so good. I’ll keep updating as I make progress. Below is a short video of running the code.

Links and References:

GUI Labels – Python Course

GUI Buttons – Python Course

Stopwatch code

Google Doc with Client Requirements and (brief) planning

Leave a Reply