Creating a native app and GUI with TKinter

They say necessity is the mother of invention. In my case, it was high power bills.

I have a bank of computer systems sitting on my desk that I use on occasion to host different web apps or to run experiments. (I mean, who doesn’t right?)

Although, they’re not particularly energy-guzzling individually, together, they can consume quite a bit of juice. Running them all together at full throttle would end up costing almost $2 a day in energy. That won’t do. I could have them go to sleep which would save some power, but that would mean I could no longer access them without hitting the power button. What if I’m not at home and need to run a script that calculates how many tickets you’d have to buy to win the lottery? Or what if inspiration hits at the coffee shop, and I need a server to host a last-minute Django project?

The offending energy Guzzlers

What is a lowly cash-strapped computer nerd to do? Turn to Python of course. Wouldn’t it be great if we could lean on Python’s vast library of third-party code to create some kind of wake-on-lan tool? And give that tool buttons to make it easy to wake these systems up no matter where we are in the world?

For this project we’ll need two modules:

wakeonlan

tkinter

Step 1 — Declare your systems

We will need to save the name of each system as well as the corresponding MAC address. In this case we’re going to save each piece of information in a dictionary. The mac addresses below are fake, you would replace these with your own equipment’s addresses:

Step 2 — Build out your interface

Tkinter is relatively simple GUI platform to learn and there’s a great tutorial here. In essence, you are going to create a “root” (root = Tk()) object which you can then attach your components to. A very simple beginning app would look something like this:

Executing this, we get:

From there, it’s just a matter of getting used the the Tkinter framework. We can add buttons, tables, labels, and much more. Here is our completed app:

When executed we get:

Step 3 — Waking up a system.

wakeonlans’s built in “send magic packet” makes it extremely easy to wake up a system. In the function “wake_up” we’re passing a system object along with it’s mac address. From there we can simply call “send_magic_packet” to send a targeted packet to that machine. Presto! We’ve woken it up.

Step 4 — Implementation

This is all great, but you still need an system that will run this application. That would mean leaving at least one computer on at all times that would let a user dial into it and run this program. Because python runs on all platforms, we don’t really need anything powerful to accomplish this. In fact, a raspberry pi would be perfect.

These little guys are useful for all kinds of projects, but most importantly, they are extremely low-voltage. In most cases, consuming less than a watt.

Here is our code executed on a Raspberry Pi: