In this chapter we will learn about graphical user interface (GUI) development. Python has many libraries for making GUIs. We will learn PyQt5, a Python library, to create guIs.

In this chapter we will study the following topics:

  • GUI Introduction to the graphical interface
  • Use libraries to create GUI-based applications
  • Install and use the Apache Log Viewer application

Note: This chapter covers graphical interfaces. Please test it out on your Own Windows or Mac

GUI Introduction to the graphical interface

In this section we will learn about GUI. Python has many frameworks for graphical interfaces. Let’s look at PyQt5 in this section. PyQt5 has different graphical components, also known as object tools, that can be displayed on the screen and interact with the user. These components are listed below:

  • PyQt5 Window: The PyQt5 window creates a simple app window
  • PyQt5 button: A PyQt5 button is a button that triggers an action when clicked
  • PyQt5 TextBox: The PyQt5 TextBox component allows the user to enter text
  • PyQt5 Landing TAB: The PyQt5 label component displays a single line of text or an image
  • The PyQt5 Combo Box is a combination of buttons and pop-up lists
  • PyQt5 Check Box: The PyQt5 check Box component is an option button that can be checked and unchecked
  • PyQt5 Single Line Button: The PyQt5 Radio button component is an option button that can be checked and unchecked. In a group of radio buttons, only one of them can be checked at a time
  • PyQt5 Message Box: The PyQt5 Message Box component displays messages
  • PyQt5 Menu: The PyQt5 menu component provides different options for display
  • PyQt5 Table: The PyQt5 table component provides the application’s standard table display capability and can be constructed with multiple rows and columns
  • PyQt5 Signals/slots: Signals allow us to respond to events that occur, and slots are simply functions that are called when the signal occurs
  • PyQt5 Layout: A PyQt5 layout consists of several components

PyQt5 has several classes that you can use, divided into different modules. These modules are as follows:

  • QtGui: QtGui contains event handling, graphics, fonts, text, and base image classes
  • QtWidgets: QtWidgets contains classes that create desktop-style user interfaces
  • QtCore: QtCore contains non-graphical functions such as time, directories, files, streams, urls, data types, threads, and processes
  • QtBluetooth: QtBluetooth contains classes to connect to and interact with devices
  • QtPositioning: QtPositioning includes positioning classes
  • QtMultimedia: QtMultimedia classes that contain apis and multimedia content
  • QtNetwork: QtNetwork contains classes for network programming
  • QtWebKit: QtWebKit contains classes for web browser implementations
  • QtXml: QtXml contains the class of XML files
  • QtSql: QtSql contains classes for databases

GUI is driven by events. So what is an event? An event is a signal that something has happened in a program, such as a menu selection, mouse movement, or button click. Events are triggered when a function processes them or when a user performs some action on an object. Listeners listen for events and fire event handlers when they occur.

Use libraries to create GUI-based applications

Now we will use the PyQt5 library to create a simple GUI application. In this section we will create a simple window. In this window, we will add a button and a label. After the button is clicked, some messages are printed in the label.

Let’s start by looking at how to create a button component. The following code creates a button component:

b = QPushButton('Click', self)
Copy the code

Now let’s look at how to create a label. The following code creates a tag:

l = QLabel(self)
Copy the code

Now I’ll look at how to create the button and label and how to perform the action once the button is clicked. Create the script print_message.py and write the following code in it:

import sys from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QWidget from PyQt5.QtCore import pyqtSlot from PyQt5.QtGui import QIcon class simple_app(QWidget): def __init__(self): super().__init__() self.title = 'Main app window' self.left = 20 self.top = 20 self.height = 300 self.width = 400 self.app_initialize() def app_initialize(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.height, self.width) b = QPushButton('Click', self) b.setToolTip('Click on the button!! ') b.move(100, 70) self.l = QLabel(self) self.l.resize(100, 50) self.l.move(100, 200) b.clicked.connect(self.on_click) self.show() @pyqtSlot() def on_click(self): self.l.setText("Hello World") if __name__ == "__main__": app1 = QApplication(sys.argv) ex = simple_app() sys.exit(app1.exec_())Copy the code

Run the script and get the following output:

$ python3 print_message.py
Copy the code

In the previous example, we imported the PyQt5 module that we need to use. Then you create the application. QPushButton creates a component where the file for the first parameter entered is displayed on the button. We then add a QLabel component that prints a message, which is printed when the button is clicked. Next we create a function on_click() that prints after the button is clicked. On_click () is the slot we created.

Let’s look at an example of a box layout. Create a script box_layout.py and write the following in it:

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout

app1 = QApplication([])
make_window = QWidget()
layout = QVBoxLayout()

layout.addWidget(QPushButton('Button 1'))
layout.addWidget(QPushButton('Button 2'))

make_window.setLayout(layout)
make_window.show()

app1.exec()
Copy the code

Running the script, we get the following output:

$ python3 box_layout.py
Copy the code

In the example above, we created a box layout. We put two buttons in there. This script is only used to explain the sample layout. Layout = QVBoxLayout() creates a box layout.

Install and use the Apache Log Viewer application

Already have an Apache Log Viewer Log Viewer application, can be downloaded from the following link: www.apacheviewer.com/download/

Once downloaded, install the application on your computer. This application can analyze log connection status, IP address and other information. Therefore, to analyze log files, we simply browse access log files or error log files. After obtaining the files, we can perform different operations on the log files, such as adding filters, filtering out failed connections in access.log, or filtering out specified IP addresses, and so on.

Apache Log Viewer native supports Windows. The following screenshots use Windows VIRTUAL machines

The following screenshot shows that the Apache log viewer does not add filtering to the access.log view:

The following screenshot shows the filters added to the access.log file in the Apache log viewer:

In the first example, we took the Access log file and opened it in the Apache log viewer. The Apache Logs Viewer can easily see open records in the access log, such as authorized and unauthorized records, including status, IP address, request, and so on. But in the second example we apply filtering to access log files so that only unauthorized requests are viewed in the log (Alan filters 404 and unauthorized 401), as shown in the figure above.

conclusion

In this chapter we learned about GUI. We learned about the components used in the GUI. You also learned the PyQt5 module in Python. Using the PyQt5 module, we created a simple application to print a message after a button is clicked.

In the next chapter, we’ll learn how to handle Apache log files.

Questions after class

  1. What is a GUI?
  2. What are constructors and destructors in Python?
  3. What’s self good for?
  4. Compare Tkinter, PyQt, and wxPython
  5. Create a Python program to copy the contents of one file into another
  6. Create a Python program that reads a text file and counts the number of occurrences of the specified letter in the file

Further reading

  • Tkinter GUI library document: docs.python.org/3/library/t…
  • PyQt GUI library document: wiki.python.org/moin/PyQt