Python chat application with PyQt - Messaging interface

Python PyQt Widgets: Exercise-11 with Solution

Write a Python program to build a chat application that displays messages after typing a new message. Use the PyQt module.

From doc.qt.io:

QApplication Class: The QApplication class manages the GUI application's control flow and main settings.

QMainWindow Class: The QMainWindow class provides a main application window.

QWidget Class: The QWidget class is the base class of all user interface objects.

QVBoxLayout Class: The QVBoxLayout class lines up widgets vertically.

QLabel Class: The QLabel widget provides a text or image display.

QLineEdit Class: The QLineEdit widget is a one-line text editor.

Sample Solution:

Python Code:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QLineEdit

class ChatApp(QMainWindow):
    def __init__(self):

        # Set the window properties (title and initial size)
        self.setWindowTitle("Chat Application")
        self.setGeometry(100, 100, 400, 300)  # (x, y, width, height)

        # Create a central widget for the main window
        central_widget = QWidget()

        # Create a QVBoxLayout to arrange the widgets
        layout = QVBoxLayout()

        # Create a QLabel widget to display chat messages
        self.chat_label = QLabel()
        self.chat_label.setWordWrap(True)  # Wrap long messages

        # Create a QLineEdit for typing new messages
        self.message_input = QLineEdit()
        self.message_input.setPlaceholderText("Type your message here...and press Enter key.")

        # Set the layout for the central widget

        # Initialize chat history
        self.chat_history = []

    def send_message(self):
        # Get the message from the input field
        message = self.message_input.text()

        # Append the message to the chat history

        # Update the chat display

        # Clear the input field

    def update_chat_display(self):
        # Display the chat history in the QLabel
        chat_text = "\n".join(self.chat_history)

def main():
    app = QApplication(sys.argv)
    window = ChatApp()

if __name__ == "__main__":


In the exercise above -

  • Import the necessary modules.
  • Create a "QMainWindow" named ChatApp with a central widget.
  • Set the window's title and initial size.
  • Create a QVBoxLayout named layout to arrange the QLabel for displaying chat messages and the QLineEdit for typing new messages.
  • Create a QLabel widget (chat_label) to display chat messages and set setWordWrap(True) to wrap long messages.
  • Create a QLineEdit widget (message_input) for typing new messages and connect its returnPressed signal to the send_message method.
  • The "send_message()" method retrieves the message from the input field, appends it to the chat history list, updates the chat display, and clears the input field.
  • The "update_chat_display()" method updates the QLabel with the chat history.
  • In the main function, we create the PyQt application, create an instance of the ChatApp class, show the window, and run the application's event loop.


PyQt: Python chat application with PyQt - Messaging interface. Part-1
PyQt: Python chat application with PyQt - Messaging interface. Part-2


Flowchart: Python chat application with PyQt - Messaging interface.
Flowchart: Python chat application with PyQt - Messaging interface.

Python Code Editor:

Previous: Button grid.
Next: Python custom progress bar widget with PyQt.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.