Raspberry Pi Graphical User Interface 49

[otw_is sidebar=otw-sidebar-1]In this post I will discuss how to build the Graphical User Interface using Python in the Raspberry Pi. This post can be considered as first part where you will learn to build the basic Graphical User Interface (GUI) and in the second part you will learn the controlling of the home appliances using the Graphical User Interface in the Raspberry Pi.

[otw_is sidebar=otw-sidebar-3]

Raspberry Pi Graphical User Interface (GUI)

After reading this post you will learn how to build the GUI in the Raspberry Pi, what libraries are required to build the GUI and how to code for the Raspberry PI Graphical User Interface. So sit back keep reading and enjoy learning.
Although the main benefit of the Graphical User Interface is with the touch screen I am not using the touch screen but I have already discussed how to interface the Touch Screen with the Raspberry Pi in my previous post. I am using the laptop here to build the Graphical User Interface and the GUI will appear on the Laptop screen.

[otw_is sidebar=otw-sidebar-3]

Tkinter Library for Graphical User Interface (GUI):

We have learned so far that the Python has specific libraries to program for the specific tasks for example to program the General Purpose Input / Output pins of the Raspberry Pi using the Python language RPi.GPIO library for the Python is used, in similar way to program the Raspberry Pi for Bluetooth application Bluez library is used, to program for the Serial Peripheral Interface and Inter Integrated Circuit (I2C) their specific libraries are used. Some of these libraries are already available in the Raspbian (recommended Operating System) distribution and some libraries needs to be downloaded for which you need to connect your Raspberry Pi to the internet I have discussed the connection of the Raspberry Pi to the Internet using the Wi Fi and Ethernet cable. So you must have figured out that to program for Graphical User Interface in the Raspberry PI using the Python language there must be a specific library. This library is called the Tkinter and fortunately this Python library is already installed in the Raspbian and you do not need to download any library or support package to build the Graphical User Interface in the Raspberry Pi using the Python language. Here in this post I will discuss the basics to build the GUI and some of the commonly used functions for Graphical User Interface and then in the second part of this post I will discuss the use of the Graphical User Interface to control the home appliances.

[otw_is sidebar=otw-sidebar-3]

Raspberry Pi Graphical User Interface:

Follow the following simple steps to build your own Graphical User Interface:

Step1:

First of all open your Python IDLE.

Step2:

Now copy and paste the following code to build the Graphical User Interface:

from tkinter import *
import tkfont
win = tk()
myfont = tkfont.Font(family = ‘’Times New Roman’, size = 36, weight = ‘bold’ )
win.title(“first gui”)
win.geometry(‘800 x 480’)
exitButton = Button(win, text = “Exit”, font = myfont, command = exitProgram, height = 2, width = 6)
exitButtoon.pack(side = BOTTOM)
exitButton = Button(win, text = “LED”, font = myfont, height = 2, width = 10)
ledButton.pack(side = BOTTOM)
def exitProgram():
win.quit()

 

 

from tkinter import *:

Now let us discuss in detail what each function is doing in the above code. Notice that in the first line of code we have imported the Tkinter library. Once this library is imported in the Python script you can use any function in the library to customize your Graphical User Interface in the Raspberry Pi, so it is important that every time you write the code for the Raspberry PI GUI this library should be imported in the Python script.

import tkfont:

The second line of the code imports another library for the Graphical User Interface. This library has functions that are meant to customize the text in the Graphical User Interface (GUI). That is if you want to add any text on the button for example “led on” or “led off” this tkfont library lets you customize the text by allowing you to modify the color of the text, the font-size and the font style.

win = tk:

In the third line of the code we have created the object called “win” you can write any name for the object in replace of the win. This object is used in the code so that anything regarding the GUI can refer to this “win”. By the way its use in the code is self-explanatory so look at the rest of the code and you will understand its use.

[otw_is sidebar=otw-sidebar-3]

myfont = tkfont.Font(family = ‘’Times New Roman’, size = 36, weight = ‘bold’):

In the next line of the code I am defining the font that I will be using in the rest of the code. Using this line of code you can customize your text in the Graphical User Interface where ever you want. Just type the name of your customize font style here the name of mine is myfont. So here myfont is carrying information regarding all the styles that we will be using.

win.title(“first gui”):

The next line of code gives the name of name window that will appear when you run the code that is the name of your GUI.

win.geometry(‘800 x 480’):

This line of code determines the dimensions of the window of the Graphical User Interface. I have set the height of my GUI to be 480 and the width as 800. You can select the dimensions according to your requirement.

Defining Buttons:

My GUI will consist of two buttons one will be carrying the name “exitButton” and the second button will carry the name of “LED”. This is done by the line code as above. Notice that I have associated the action to the exitButton that is when the user will click on this button the certain action will be performed and this action will be determined by the user defined function.

Step3:

Save your Python script with any name you like but do not forget to add the extension .py at the end of the name. You can run this Python script either form the Python IDLE or from the terminal window of the Raspberry Pi. Simply write the name of the file in the Raspberry Pi terminal window and the File will get executed and your GUI will appear on the Laptop screen.
That is all for now in the next post I will discuss how to control the Home Appliances using the Graphical User Interface in the Raspberry Pi using the Tkinter library and the RPi.GPIO library. I hope this article would be helpful for you. To learn more about Raspberry Pi stay connected, keep reading and enjoy learning.

 

Raspberry Pi gpio interface with Temperature sensor ds18b20

Raspberry Pi interface with Ultrasonic sensor using Python

Raspberry Pi 3 GPIO programming

This educational journey is never ending and I hope I will play some role in helping you out in this trip. Stay blessed, keep reading and enjoy learning.

Getting Started With Raspberry Pi

 

Leave a Reply

Your email address will not be published. Required fields are marked *