PythonQt Uncategorized

Serial Communication PyQt

from PyQt5 import QtCore, QtWidgets, QtSerialPort

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.message_le = QtWidgets.QLineEdit()
        self.send_btn = QtWidgets.QPushButton(
        self.output_te = QtWidgets.QTextEdit(readOnly=True)
        self.button = QtWidgets.QPushButton(
        lay = QtWidgets.QVBoxLayout(self)
        hlay = QtWidgets.QHBoxLayout()

        self.serial = QtSerialPort.QSerialPort(

    def receive(self):
        while self.serial.canReadLine():
            text = self.serial.readLine().data().decode()
            text = text.rstrip('\r\n')

    def send(self):

    def on_toggled(self, checked):
        self.button.setText("Disconnect" if checked else "Connect")
        if checked:
            if not self.serial.isOpen():
                if not

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
programming QT

Serial Port handling in QT C++

To check what serial ports are available, follow this code.

#include <QApplication>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
int main(int argc, char *argv[])
    QApplication a(argc, argv);
    SerialPortTest w;;

    // Example use QSerialPortInfo
        foreach (const QSerialPortInfo &amp;info, QSerialPortInfo::availablePorts()) {
            qDebug() << "Name        : " << info.portName();
            qDebug() << "Description : " << info.description();
            qDebug() << "Manufacturer: " << info.manufacturer();
            qDebug() << "Serial Number: " << info.serialNumber();
            qDebug() << "System Location: " << info.systemLocation();
            // Example use QSerialPort
            QSerialPort serial;
            if (

    return a.exec();

To read and Write some data on serial port Use Following Code


Common QT Qidgets

Any Qt GUI is made up of widgets. There are several type of widgets which are used in making a GUI application.

Most common GUI widgets available in PyQt are
Check Boxes,
Radio Buttons,
Input Fields,
Progress Bar,
Dialog Box etc.


PyQt – Event Handling, Signal and Slots

Any QT application is made up of widgets. Qt provides several built-in widgets which we can use in designing our GUI application.

What is an Event Handling
GUI applications are meant to be used for user interactions. User interacts with the help of mouse, keyboard, touch etc. and typical user actions are clicking, dragging, resizing etc. Whenever an user interacts with a GUI, some events are triggered like mouse clicked, mouse dragged, key pressed, screen touched. These events are then processed by Qt event handling mechanism.

There are several types of events which can be implemented in a GUI applications. So we need to register only those events which we are going to use in our application. Handlers related to those events are implemented in the code. Whenever an event is fired, it’s related handler immediately captures the command of execution and do the processing which we want it to do.

In Qt, these events are called Signals and event handlers are called Slots.

To understand Signal and slot in a better way let’s place a QLabel widget and a QPushButton widget in our GUI application

In this application :
Event we want to capture : Button Pressed
Task we want to do : Update the label text

As we are going to add two widgets in our GUI. We need to tell Qt about the position of these widgets. This can be done using Qt Layouts. We will discuss about layouts in detail in next lessons.
For now here we are using QVerticalLayout which will place the widgets in vertical manner

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout 
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Signal-Slot Test")

label = QLabel('Hello World!')
button = QPushButton('Click me')

layout = QVBoxLayout()

def on_button_clicked():        
     label.setText('You clicked the button!')    

app.exec_() #This starts the event loop

Python GUI – First PyQt Application

After installing PyQt in the last lesson, we are now experimenting with a basic Gui program in VS Code IDE.
Create a new python file in VS Code and type following program.

import sys
from PyQt5.QtWidgets import QApplication, QWidget

app = QApplication(sys.argv)

window = QWidget()

app.exec_() #This starts the event loop

To test and run the program click on Run menu item and click Run Without Debugging or just press shortcut Ctrl+F5

A window will popup which is the result of this simple Python Qt GUI program.

Let’s now understand this basic Gui program.

Import statements loads the library modules which can be used in our program further.

Next the line
app = QApplication(sys.args)
creates an QApplication object. Every Gui program in Qt require exactly one instance of QApplication.

Then we make one object of QWidget
window = Qwidget()

QWidget class is the base class of all user interface objects in Qt.
To show this widget on our Gui, we need to use show() function.
will display the widget on screen.

To change the title of GUI window we can use
window.setWindowTitle(“My Python GUI”)

In the end we have used app.exec_()
which starts the event loop and program is not reached up to the end.
If we don’t use this function, program will immediately reach to end making the window appear for a moment only.

In the Next lesson we will cover about Event Handling, Signal and Slots

python PythonQt

Installing PyQt

Here we are using conda virtual environment for learning Python Qt programming. Here is how we can install PyQt library withing conda virtual environment.

Let us start by creating a new virtual environment named “pythonQt” (You can name it anything you want).

1. Start Anaconda navigator. Click on Tab
2. Click on
3. Write name of new virtual environment as pythonQt in Name field.
4. Click on Create .

After few moments new virtual environment will be installed.

To use the new pythonQt virtual environment.
Click on Home tab and select virtual environment from drop down.

Next step is to install PyQt library within our virtual environment.

start anaconda prompt in windows (or terminal in Linux)
and activate new virtual environment “pythonQt” by typing following command.

conda activate path-to-virtualEnv

pip install pyqt5

Now pyqt5 is installed successfully. We are going to use Visual Studio Code IDE for developing our PyQt applications.

Start VS Code IDE from anaconda navigator. When we launch VS Code IDE from anaconda navigator, VS code automatically utilizes python and installed libraries from the selected virtual environment. In our case pythonQt virtual environment.

If the desired virtual environment is not loaded then click on the bottom and select the one from the list.

In the next lesson we will see how to start making Qt Gui using Python.


Capture Video from Camera in OpenCV-Python

programming python

PyCharm with Conda Environment

Pycharm is one of the famous Python IDE. We can create virtual environments in Pycharm. But there are sometimes when we want to use Conda environment with-in Pycharm.


Taking Input With Switches – Arduino


LCD Interfacing Arduino