Built a Linux GUI App with GTK4 to Run an MNIST Model

Togo Kida
graphtogo
Published in
3 min readJun 2, 2024

It’s been almost three years since my last entry. After finishing graduate school, I’ve changed jobs twice and have been overwhelmed by work, housework, and childcare, with time flying by in the blink of an eye. My daughter, who was six months old when I studied abroad in graduate school, is now six years old and started elementary school this year. I’ll be 40 next year. The flow of time is cruel. Is this what life is all about…?

As I approach my 40s, I’m constantly filled with confusion, worries, and regrets. However, recently, I’ve found solace in simply using my hands to create something, even if it’s just a small thing. It’s about not worrying about what others think and just doing something for my own enjoyment… Besides coding, I’ve also enjoyed tinkering with Blender recently.

Generative AI and UI

Compared to four years ago when I was actively involved in my graduate school project, the world has seen the rise of Transformers and ChatGPT, leading to a global trend in generative AI. If I could tell my past self from three years ago, I would say, “Buy. NVIDIA. Period!”

Putting the NVIDIA matter aside for now, I’ve continued to have a personal interest in generative AI and the interfaces for utilizing it, following my graduate school research. We tend to imagine chat-based interfaces for generative AI, but “conversation” is, so to speak, the “oldest” interface for obtaining information. If we don’t get fixated on conversational forms, there’s potential for entirely new computing experiences.

I’ve been deeply feeling the bittersweetness of getting older, and recently, I felt the urge to declutter and switch from my 20-year-old Mac environment to an Ubuntu machine for personal use. It’s quite deep and I’m really enjoying using it.

With that, my desire to create my own GUI has grown stronger. So, with my personal interest in the interface of the burgeoning generative AI and the goal of becoming more proficient and enjoying Linux, I decided to create a GUI for Ubuntu and implement a simple machine learning model within it.

Enter GTK4!

When you search for how to create GUI apps on Linux, various methods come up, but among them, GTK is one of the most prominent. It’s a tool kit originally developed for implementing GIMP, hence the acronym GTK. I liked the fact that it’s a library created for developing creative tools, so I decided to use GTK. It has a relatively new version called GTK4, and it’s great that there’s an active community formed around it as an open-source project. I hope to contribute to it someday.

GUI App Structure

The GUI structure is simple, as shown in the image. On the right, there is a drawing area for writing numbers, and on the left, there are buttons for predicting the number and clearing the drawing area. When the model makes a prediction, the estimated number is output to the terminal.

Source Code

The source code, which is the result of following GTK tutorials, online articles, and asking Gemini countless questions, is as follows:

For now, I’m satisfied that it works! This gives me the confidence that I can create other GUI apps with machine learning models in the future.

Helpful Resources

While resources for GTK4 are somewhat limited, the following sites were particularly helpful. I’m grateful for them.

Getting Started with GTK
PyGObject Getting Started
Widget Gallery
Taiko’s GTK4 Python tutorial
GTK4 (Python) Tips | Controllers
Simple example of saving and loading a Keras-trained MNIST model

Final Thoughts

However, to be honest… Why does it seem like the model’s accuracy gradually decreases as I continue using the app…? I feel like there might be a bug somewhere…

--

--

graphtogo
graphtogo

Published in graphtogo

Currently a marketer in Tokyo. Formerly a graduate student at Harvard University studying Design Engineering as a Fulbright Scholar.

Togo Kida
Togo Kida

Written by Togo Kida

Creative. Marketer. Strategist. Technologist. Formerly at UCLA, Harvard, Dentsu, and Uniqlo. 100 Leading Global Thinkers 2016. Creativity, design & data.

No responses yet