PyQt is a desktop-oriented software. It is a Python module that provides the widgets needed to create applications with a GUI. It’s a pretty cool API, so if you’re into making applications for the desktop, PyQt5 is worth checking out. In this article, we’ll look at some ways to use PyQt5 Terminal Widget with Python.
In this article, we’ll be taking a look at the new terminal widget in PyQt5, which allows you to create a custom terminal application that you can use to access the shell environment. We’ll also discuss how to use the terminal widget in Python and its meaning and purpose.
Terminal Widget in PyQt5
A widget accommodates user input during program execution. It enhances user experience by creating an interactive environment.
The terminal is not a part of PyQt5. So, you have to implement using Q Process. With the help of a terminal, one can easily communicate with the Operating system. A terminal widget will create a replica of a terminal in Python.
The Syntax
import sys
from PyQt5 import QtCore, QtWidgets
class EmbTerminal(QtWidgets.QWidget):
def __init__(self, parent=None):
super(EmbTerminal, self).__init__(parent)
self.process = QtCore.QProcess(self)
self.terminal = QtWidgets.QWidget(self)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.terminal)
# Works also with urxvt:
self.process.start('urxvt',['-embed', str(int(self.winId()))])
self.setFixedSize(640, 480)
class mainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(mainWindow, self).__init__(parent)
central_widget = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(central_widget)
self.setCentralWidget(central_widget)
tab_widget = QtWidgets.QTabWidget()
lay.addWidget(tab_widget)
tab_widget.addTab(EmbTerminal(), "EmbTerminal")
tab_widget.addTab(QtWidgets.QTextEdit(), "QTextEdit")
tab_widget.addTab(QtWidgets.QMdiArea(), "QMdiArea")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main = mainWindow()
main.show()
sys.exit(app.exec_())
Here, QtCore helps get non-GUI applications as our interface. We imported the QWidget class. Several parent and child widgets control the mouse, keyboard, and other such events.
The EmbTerminal class introduces a widget. Include it with addTab(). Check that you have installed the urxvt terminal. Run urxvt in the terminal to check if the widget is installed correctly.
Display Terminal Output in PyQt5
A QProcess is meant to deal with processes. It considers a process an IO device and executes the related functionality. QVBoxLayout creates a box-like structure. It is vertical, and you can add push buttons as per your choice. With QWidget, you can make a new widget as per your choice. It provides you with this feasibility. A QWidget can have multiple widgets under it. It acts as a subclass at some point in time. Here, xterm refers to a terminal emulator.
class embterminal(QWidget):
def __init__(self):
QWidget.__init__(self)
self.process = QProcess(self)
self.terminal = QWidget(self)
layout = QVBoxLayout(self)
layout.addWidget(self.terminal)
self.process.start(
'xterm',['-into', str(self.terminal.winId())])
Other terminal modules in Pypi
window-terminal module
It will give you the freedom to print or enter input. This will enable the commencement of a new terminal window and related commands. It provides access to several functions. For example, you can use the open() function to open the terminal or sleep(), which puts it in sleep mode. You can specify a time limit for sleep.
pip install window-terminal
from window_terminal import WindowTerminal
T_window = WindowTerminal.create_window()
T_window.open()
pty module
It deals with pseudo-terminal concepts. You can effortlessly import the pty module. Its functions include pty.fork(), pty.openpty(), and others. It will also help to connect the terminal to the required processes. Besides this, you’ll get master and slave names, too.
import pty
PyQt or Tkinter for Widgets?
PyQt is considered to be the better option. Tkinter is created from the original Toolkit library. So if you wish to start from scratch and learn how to make widgets, you can choose Tkinter.
More about PyQt5
PyQt5 is a Python module for the Qt 5 library. It allows you to write applications that use the graphical user interface (GUI) facilities of the Qt Toolkit in a Pythonic way. PyQt5 provides rich classes and functions to create sophisticated graphical user interfaces or GUIs.
It is a Python binding for the Qt 5.x widget toolkit and UI framework. It has been released under the GNU Lesser General Public License (LGPL) version 3 or later. PyQt5 provides classes that allow you to create native-looking applications for various platforms. It’s based on the Qt framework but does not require additional software installation, making it ideal for embedded systems.
Uses of PyQt5 module
PyQt5’s capabilities include the following:
- A complete set of widgets, including buttons, checkboxes, sliders, text edit boxes, and other standard components;
- Support for both 2D and 3D graphics;
- Support for internationalization;
- Advanced text layout engine with support for CJK languages;
- Full support for advanced window system features like minimize/maximize/close buttons, dialogs, and child windows;
- The ability to customize how your application looks by defining styles.
FAQs
PyQt5 works better for desktop applications. On the other hand, the former suits mobile interface applications well.
No, it would be best if you coded it yourself.
Conclusion
PyQt5 can also create terminal widgets using the code snippet mentioned above. Apart from this, it can also help to curate other widgets in Python.