Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Raspberry Pi GUI development using GTK+ and event handling

February 24, 2021 By Usman ali Butt

In this tutorial, we’ll learn how to develop a graphical user interface (GUI) application for Raspberry Pi (RPi) using Python. This is not an easy task, particularly for a system that has limited resources (memory, CPU, RAM, cache, etc.).

Granted, the latest version, RPi 4.0, offers 8GB of RAM, which allows for rendering and for video games. But its central processing unit (CPU) and cache still lag behind compared to desktop PC GUI applications. Nevertheless, RPi offers several packages for GUI development. Tkinter is its default GUI development package.

Tkinter contains a few widgets (such as a button, text box, scroll bar, etc.) that can be imported into a project and actions can be set against each one. But Tkinter has no GUI interface where, ideally, each widget would be placed at the desired position in a window. This means users have to manually set the coordinates and functions for each widget.

GTK+ is RPi’s package, where a user interface can be developed manually by dragging and dropping the widgets in a window. This is done by using the Glade user interface development tool. Additional parameters for widgets can also be set by using this tool.

Glade generates an XML file of the user interface, where all of the widgets are present. From there, it’s possible to import the widgets from the XML file to the main window.     

GTK is a cross platform, meaning we can develop applications in C or Python by using it. For this project, we used Python 3. To follow our lead, you’ll need to install python3, GTK, and Glade on your RPi. An easy way to do so is by simply using the RPi console.

For python3, enter these commands in the console:

sudo apt update
sudo apt install python3

For GTK, enter this command:

sudo apt-get install libgtk-3-dev

And for Glade, enter this command:

sudo apt-get install glade

After successful installation, you’ll find Glade in the menu bar. Now, let’s add a button and a text box in the Glade window. Then, open Glade.

From the top level, select a window widget. The window is where we’ll place all of the widgets. Widgets inside a window are called, the “child widgets” of the top level of the main window. Several main top-level windows are available in GTK.

For this project, we selected the GTKWindow, which is at the very top. Give your window a name (we used, TopWindow). Below, you can see the window widget in the selected widget menu.

After selecting a window, you’ll also need to choose a container for it. Containers are an ideal way to put and arrange any child widgets (either in a horizontal or vertical arrangement). Additional containers can be added inside a container.

We added a single GTKFixed container in the window. It will be a child widget that appears under the TopWindow.    

Next, let’s add a button. Go to the control and from the dropdown menu, choose the GTKButton. A button will appear under the container, which is called the “child of the container.” To set actions or to use it in the code, you’ll have to name it.

We named ours, UploadButton. You’ll note, the default button label can also be set by inputting text in the label box.

The text view is in the display menu. Select it and it will appear under the container as a child widget. You’ll also have to name it so to use it in the code. The default text can also be set up by using the label (we didn’t set any default text when using Glade).

Our test GUI XML file is ready. As soon as you close the glade application, the XML code file will appear in the working directory.

Import note: Plenty of other parameters and options — such as the height, width, label, position, alignment, or actions, etc. — can be set by using Glade. We highlighted a few in the above example.  

The code
The GitHub code is available here.

Essentially, the code is commented against each statement. We used Thony IDE to compile and run the Python code. Upon execution, you’ll see the GUI window contains a button and a text view pane. The button’s name and the default text in this text view can be seen below.

The event that we set on the button click copies the text already present in the text view and appends it with the “Text Inserted.” Below is a screenshot of what happens when we press the button.

If we the press the button repeatedly, the “Text Inserted” will be appended in the text view.

As you can see here, the four windows are technically one window that’s stacked, based on the number of clicks (three button clicks). As a result, the window resizes on each text amendment. It’s also possible to fix the window and text view size when using Glade.

Your turn…try building the RPi GUI application by using GTK.

Where to purchase Raspberry Pi? Mouse

 

 

You may also like:


  • Interfacing Raspberry Pi with Arduino
  • beginners guide
    Basic Electronics 01 – Beginners guide to setting up an…

  • Understanding computer vision and its application in electronics
  • Raspberry Pi 4
    RPi Python Programming 01: Introduction to Raspberry Pi 4

Related Articles Read More >

How to monitor pH levels in an aquarium using Arduino
TV remote hack using Arduino and IR sensor
Gesture sensor using Arduino
How to build a metal detector using an inductive proximity sensor with Arduino

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Introduction to Wireless Modules
  • ADC with NRF24LE1 (Part 2/14)
  • Interrupts with NRF24LE1 (Part 3/14)
  • Power Failure Indicator in NRF24LE1 (Part 4/14)
  • Using EEPROM of NRF24LE1 (Part 5/14)
  • PWM with NRF24LE1 (Part 6/14)

Stay Up To Date

Newsletter Signup

EE Training Center Classrooms

“ee

“ee

“ee

“ee

“ee

Recent Articles

  • Introduction to LUFA
  • What are the different types of integrated circuits?
  • Introduction to Wireless Modules
  • ADC with NRF24LE1 (Part 2/14)
  • Interrupts with NRF24LE1 (Part 3/14)

RSS EDABOARD.com Discussions

  • How to find 1's and 2's complement without using CMA operation in 8085 microprocessor?
  • Non-Inverting Amplifier (AC Coupled) : How to solve this circuit with lower and high frequency?
  • Electrolytic capacitor lifetime and rated voltage?
  • Syntax error in verilog
  • 2SC1971 Vs 2SC1972

RSS Electro-Tech-Online.com Discussions

  • Wirer up stereo needle
  • Learning C
  • Post some funny stuff...
  • Looking for an SOIC socket
  • QA pass
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2021 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering