Spiking Out a GUI Employee Time Clock
Using Tk Backed by Square’s Ruby SDK
As small companies get bigger, they sometimes decide to start using time clocks to track employee hours. Stakeholders often ask engineering for a solution that’s easy to use. As Rubyists, we usually create command line apps or web apps, but sometimes we get asked to make something for mobile or desktop. Below I’ll spike out the start of a desktop GUI employee time clock app with Ruby and the Tk graphical user interface toolkit backed by Square’s API to track the details.
First Install Ruby and a Few Gems
I’ll show these examples on macOS, so we’ll use use brew to install the most recent version of Ruby first. (If you already have Ruby installed, skip this step.)
brew install ruby
Tk previously shipped with Ruby, but it was cut into a gem and removed from the standard library with the release of Ruby 2.4 back in 2016. So first we’ll need to install both the Tk and Square SDK gems.
gem install tk && gem install square_connect
Next we can use the IRB REPL that ships with Ruby to check our current employees with the SDK, and make sure everything so far is working.
To be able to see our employees we’ll need an API token at this point. To get a token, sign up for a Square developer account. For the purposes of these examples, set your API token as an environment variable on the terminal that you’re going to use.
export SQUARE_CONNECT_TOKEN="YOUR ACCESS TOKEN HERE"
You can now use irb to check that there aren’t any employees yet. First type
irb in the command line then input the following code into the IRB terminal.
Next we can create an employee in your browser, and re-run
list_employees to see the newly created employee in the REPL.
Okay, the SDK works! Let’s move on to the GUI app.
Spiking out a Tk GUI App
I love command line apps. If all your employees are engineers, maybe you can get away with making a command line employee time card app. Today, let’s start a graphical interface app with Tk. The Ruby Tk gem wraps the toolkit and provides us a nice DSL in Ruby.
This is my first time using Tk, so let’s muddle through some examples as best we can. First, let’s create a little Hello World type app that shows us today’s menu. We’ll just need two Tk classes to get started,
That wasn’t so bad! Now we have a little window to work with. If we save this snippet in a file called
menu.rb, we can run it from the command line with
Let’s move on and try to integrate the GUI with our employee time sheet SDK.
First we’ll require Date and Pretty Print from the Ruby standard library and the Tk and Square SDK gems.
Then we’re ready to fetch our list of employees and pull out the parts we care about — the employee name and id.
Next we have some app-wide settings, like the title and fonts.
At this point we’re ready to define the labels and fields that we’ll be showing in our interface.
That’s it for the boilerplate. Now we’re on to the guts of our app — everything that happens when you press the “Submit” button.
The above code fetches the values from the relevant input fields and then creates a time card using Square’s API.
Finally, we need to pick a layout for our window. I chose a grid since that seemed the simplest to get started with.
Okay, that was a lot of Tk DSL… But it works! We can save the code as
time_card.rb and when we run it with
ruby time_card.rb the GUI app appears.
Now that we have a working GUI app, let’s add a shebang, make the file executable and remove the
.rb extension so it can be run directly. First, add the following shebang to the first line of the
Next, let’s rename the file to remove the
.rb extension and make it executable.
mv time_card.rb time_card
chmod +x time_card
Now we can run our app directly from the console with just.
We’re now ready to add an icon to our very basic GUI app! The steps for adding an icon are platform-dependent so I’ll leave that as an exercise for the reader.
This interface is far from polished but I’m happy with it as a partial day spike towards creating a GUI with Tk. My first impression using Tk is that it’s fairly easy to use for simple projects. I look forward to trying something more complex. Next it would be fun to add a better date picker and more features, like adding employees and managing time cards.
If you’re a Rubyist but that was too much boilerplate for your fancy, you might want to consider Shoes as an alternative to Tk. Shoes was created by _why as a super simple way to make GUIs in Ruby. These days there are a variety of types of Shoes in Ruby, but Shoes4 is one nice modern pair that works with JRuby.