Pyqt5_tutorial
Pyqt5_tutorial
Introduction
This tutorial introduces the basics of GUI programming with PyQt5 and concludes with a mini-project
to control an RC car over Wi-Fi using an ESP8266 and HTTP requests.
1 Installing PyQt5
Run the following command:
pip install PyQt5
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('My First App')
window.setGeometry(100, 100, 300, 200)
window.show()
sys.exit(app.exec_())
def greet():
name = input_box.text()
label.setText(f"Hello {name}!")
btn.clicked.connect(greet)
1
4 Using Layouts
layout = QVBoxLayout()
layout.addWidget(input_box)
layout.addWidget(btn)
layout.addWidget(label)
window.setLayout(layout)
5 Creating Tables
table = QTableWidget(3, 2)
table.setHorizontalHeaderLabels(["Name", "Age"])
table.setItem(0, 0, QTableWidgetItem("Ali"))
table.setItem(0, 1, QTableWidgetItem("21"))
layout.addWidget(table)
Install Requests
pip install requests
import sys
import requests
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class RCCarController(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("RC Car Controller")
self.setGeometry(200, 200, 300, 300)
layout = QVBoxLayout()
layout.addWidget(self.up)
layout.addWidget(self.down)
layout.addWidget(self.left)
layout.addWidget(self.right)
self.setLayout(layout)
2
self.up.clicked.connect(lambda: self.send_command(1))
self.down.clicked.connect(lambda: self.send_command(2))
self.left.clicked.connect(lambda: self.send_command(3))
self.right.clicked.connect(lambda: self.send_command(4))
app = QApplication(sys.argv)
controller = RCCarController()
controller.show()
sys.exit(app.exec_())
Conclusion
You now have a complete GUI that sends HTTP requests to control your RC car! Try expanding the
app with:
• Speed control