0% found this document useful (0 votes)
18 views

Lab Exercise 6 (PyQt Widgets)

This document provides a tutorial on creating a PyQt application with various widgets. It begins by creating a basic PyQt app with a main window. Then it adds widgets like labels, buttons, and text fields to the main window and arranges them in a vertical layout. Finally, it adds functionality by programming the button to change the window title in response to user input in the text field. The tutorial demonstrates how to create and work with different widgets to build graphical user interfaces in PyQt.

Uploaded by

hitesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab Exercise 6 (PyQt Widgets)

This document provides a tutorial on creating a PyQt application with various widgets. It begins by creating a basic PyQt app with a main window. Then it adds widgets like labels, buttons, and text fields to the main window and arranges them in a vertical layout. Finally, it adds functionality by programming the button to change the window title in response to user input in the text field. The tutorial demonstrates how to create and work with different widgets to build graphical user interfaces in PyQt.

Uploaded by

hitesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Exercise 6 – PyQT Widgets

Creating widgets in PyQt is a fundamental aspect of building graphical user


interfaces (GUIs) using Python. PyQt provides a wide range of widgets like buttons,
labels, text fields, and more. In this tutorial, we'll create a PyQt application with
various widgets and demonstrate how to work with them.

Prerequisites:

Before starting this tutorial, make sure you have PyQt5 installed. You can install it
using pip:

pip install PyQt5

Step 1: Creating a Basic PyQt Application

We'll start by creating a simple PyQt application that displays a main window.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
app = QApplication(sys.argv)
window = QMainWindow()
window.setWindowTitle('PyQt Widgets Tutorial')
window.setGeometry(100, 100, 400, 300) # (x, y, width, height)
window.show()
sys.exit(app.exec_())

This code initializes a PyQt application, creates a main window, sets its title, size, and
position, and finally, shows the window.
Step 2: Adding Widgets to the Main Window

Now, let's add some widgets to the main window. We'll add a label, a button, and a
text input field.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton,
QLineEdit, QVBoxLayout, QWidget
app = QApplication(sys.argv)
window = QMainWindow()
window.setWindowTitle('PyQt Widgets Tutorial')
window.setGeometry(100, 100, 400, 300)
# Create a central widget to hold other widgets
central_widget = QWidget()
window.setCentralWidget(central_widget)
# Create widgets
label = QLabel('Enter your name:')
name_input = QLineEdit()
greet_button = QPushButton('Greet')
# Create a layout for widgets
layout = QVBoxLayout()
layout.addWidget(label)
layout.addWidget(name_input)
layout.addWidget(greet_button)
# Set the layout for the central widget
central_widget.setLayout(layout)
window.show()
sys.exit(app.exec_())

In this code, we've added three widgets: a label, a text input field, and a button.
We've also created a layout (a vertical layout) to arrange these widgets.
Step 3: Adding Functionality to Widgets

Now, let's add functionality to the button. When the button is clicked, it will display a
greeting message in the window's title.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton,
QLineEdit, QVBoxLayout, QWidget
def greet():
name = name_input.text()
if name:
greeting = f'Hello, {name}!'
else:
greeting = 'Hello, World!'
window.setWindowTitle(greeting)

app = QApplication(sys.argv)
window = QMainWindow()
window.setWindowTitle('PyQt Widgets Tutorial')
window.setGeometry(100, 100, 400, 300)

central_widget = QWidget()
window.setCentralWidget(central_widget)

label = QLabel('Enter your name:')


name_input = QLineEdit()
greet_button = QPushButton('Greet')
greet_button.clicked.connect(greet)

layout = QVBoxLayout()
layout.addWidget(label)
layout.addWidget(name_input)
layout.addWidget(greet_button)
central_widget.setLayout(layout)
window.show()
sys.exit(app.exec_())

Now, when you enter your name in the text input field and click the "Greet" button, it
will change the window's title to greet you.

Step 4: Running the PyQt Application

Save the script with a .py extension, and then run it using Python:

python your_script_name.py

You should see the PyQt application with the widgets. Experiment with different
widgets and their properties to create more complex GUIs.

You might also like