Skip to content

Commit 65b7518

Browse files
authored
Merge pull request #292 from realpython/python-pyqt-tutorial-update
Update sample code PyQt Calculator
2 parents 667cdd4 + 7fae108 commit 65b7518

File tree

14 files changed

+192
-296
lines changed

14 files changed

+192
-296
lines changed

pyqt-calculator-tutorial/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python and PyQt: Building a GUI Desktop Calculator
2+
3+
This folder provides the code examples for the article [Python and PyQt: Building a GUI Desktop Calculator](https://realpython.com/python-and-pyqt-building-a-gui-desktop-calculator/).
4+
5+
## Folder Content
6+
7+
The current folder, `pyqt-calculator-tutorial/`, hosts two subfolders:
8+
9+
1. `examples/` contains the source file of every example in the tutorial.
10+
2. `pycalc/` stores the code and related resources for your PyQt Calculator app.
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
# Filename: dialog.py
2-
3-
"""Dialog-Style application."""
1+
"""Dialog-style application."""
42

53
import sys
64

7-
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QDialog
9-
from PyQt5.QtWidgets import QDialogButtonBox
10-
from PyQt5.QtWidgets import QFormLayout
11-
from PyQt5.QtWidgets import QLineEdit
12-
from PyQt5.QtWidgets import QVBoxLayout
13-
5+
from PyQt6.QtWidgets import (
6+
QApplication,
7+
QDialog,
8+
QDialogButtonBox,
9+
QFormLayout,
10+
QLineEdit,
11+
QVBoxLayout,
12+
)
1413

15-
class Dialog(QDialog):
16-
"""Dialog."""
1714

18-
def __init__(self, parent=None):
19-
"""Initializer."""
20-
super().__init__(parent)
15+
class Window(QDialog):
16+
def __init__(self):
17+
super().__init__(parent=None)
2118
self.setWindowTitle("QDialog")
22-
dlgLayout = QVBoxLayout()
19+
dialogLayout = QVBoxLayout()
2320
formLayout = QFormLayout()
2421
formLayout.addRow("Name:", QLineEdit())
2522
formLayout.addRow("Age:", QLineEdit())
2623
formLayout.addRow("Job:", QLineEdit())
2724
formLayout.addRow("Hobbies:", QLineEdit())
28-
dlgLayout.addLayout(formLayout)
29-
btns = QDialogButtonBox()
30-
btns.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
31-
dlgLayout.addWidget(btns)
32-
self.setLayout(dlgLayout)
25+
dialogLayout.addLayout(formLayout)
26+
buttons = QDialogButtonBox()
27+
buttons.setStandardButtons(
28+
QDialogButtonBox.StandardButton.Cancel
29+
| QDialogButtonBox.StandardButton.Ok
30+
)
31+
dialogLayout.addWidget(buttons)
32+
self.setLayout(dialogLayout)
3333

3434

3535
if __name__ == "__main__":
36-
app = QApplication(sys.argv)
37-
dlg = Dialog()
38-
dlg.show()
39-
sys.exit(app.exec_())
36+
app = QApplication([])
37+
window = Window()
38+
window.show()
39+
sys.exit(app.exec())
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
# Filename: f_layout.py
2-
31
"""Form layout example."""
42

53
import sys
64

7-
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QFormLayout
9-
from PyQt5.QtWidgets import QLineEdit
10-
from PyQt5.QtWidgets import QWidget
5+
from PyQt6.QtWidgets import QApplication, QFormLayout, QLineEdit, QWidget
116

12-
app = QApplication(sys.argv)
7+
app = QApplication([])
138
window = QWidget()
149
window.setWindowTitle("QFormLayout")
10+
1511
layout = QFormLayout()
1612
layout.addRow("Name:", QLineEdit())
1713
layout.addRow("Age:", QLineEdit())
1814
layout.addRow("Job:", QLineEdit())
1915
layout.addRow("Hobbies:", QLineEdit())
2016
window.setLayout(layout)
17+
2118
window.show()
22-
sys.exit(app.exec_())
19+
sys.exit(app.exec())
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
# Filename: g_layout.py
2-
31
"""Grid layout example."""
42

53
import sys
64

7-
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QGridLayout
9-
from PyQt5.QtWidgets import QPushButton
10-
from PyQt5.QtWidgets import QWidget
5+
from PyQt6.QtWidgets import QApplication, QGridLayout, QPushButton, QWidget
116

12-
app = QApplication(sys.argv)
7+
app = QApplication([])
138
window = QWidget()
149
window.setWindowTitle("QGridLayout")
10+
1511
layout = QGridLayout()
1612
layout.addWidget(QPushButton("Button (0, 0)"), 0, 0)
1713
layout.addWidget(QPushButton("Button (0, 1)"), 0, 1)
@@ -22,5 +18,6 @@
2218
layout.addWidget(QPushButton("Button (2, 0)"), 2, 0)
2319
layout.addWidget(QPushButton("Button (2, 1) + 2 Columns Span"), 2, 1, 1, 2)
2420
window.setLayout(layout)
21+
2522
window.show()
26-
sys.exit(app.exec_())
23+
sys.exit(app.exec())
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
# Filename: h_layout.py
2-
31
"""Horizontal layout example."""
42

53
import sys
64

7-
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QHBoxLayout
9-
from PyQt5.QtWidgets import QPushButton
10-
from PyQt5.QtWidgets import QWidget
5+
from PyQt6.QtWidgets import QApplication, QHBoxLayout, QPushButton, QWidget
116

12-
app = QApplication(sys.argv)
7+
app = QApplication([])
138
window = QWidget()
149
window.setWindowTitle("QHBoxLayout")
10+
1511
layout = QHBoxLayout()
1612
layout.addWidget(QPushButton("Left"))
1713
layout.addWidget(QPushButton("Center"))
1814
layout.addWidget(QPushButton("Right"))
1915
window.setLayout(layout)
16+
2017
window.show()
21-
sys.exit(app.exec_())
18+
sys.exit(app.exec())
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
# Filename: hello.py
2-
3-
"""Simple Hello World example with PyQt5."""
1+
"""Simple Hello, World example with PyQt6."""
42

53
import sys
64

7-
# 1. Import `QApplication` and all the required widgets
8-
from PyQt5.QtWidgets import QApplication
9-
from PyQt5.QtWidgets import QLabel
10-
from PyQt5.QtWidgets import QWidget
5+
# 1. Import QApplication, and all the required widgets
6+
from PyQt6.QtWidgets import QApplication, QLabel, QWidget
7+
8+
WIN_X = 100
9+
WIN_Y = 100
10+
WIDTH = 280
11+
HEIGHT = 80
12+
LABEL_X = 60
13+
LABEL_Y = 15
1114

1215
# 2. Create an instance of QApplication
1316
app = QApplication(sys.argv)
1417

1518
# 3. Create an instance of your application's GUI
1619
window = QWidget()
17-
window.setWindowTitle("PyQt5 App")
18-
window.setGeometry(100, 100, 280, 80)
19-
window.move(60, 15)
20-
helloMsg = QLabel("<h1>Hello World!</h1>", parent=window)
21-
helloMsg.move(60, 15)
20+
window.setWindowTitle("PyQt App")
21+
window.setGeometry(WIN_X, WIN_Y, WIDTH, HEIGHT)
22+
helloMsg = QLabel("<h1>Hello, World!</h1>", parent=window)
23+
helloMsg.move(LABEL_X, LABEL_Y)
2224

2325
# 4.Show your application's GUI
2426
window.show()
2527

26-
# 5. Run your application's event loop (or main loop)
27-
sys.exit(app.exec_())
28+
# 5. Run your application's event loop
29+
sys.exit(app.exec())
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
# Filename: main_window.py
2-
3-
"""Main Window-Style application."""
1+
"""Main window-style application."""
42

53
import sys
64

7-
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QLabel
9-
from PyQt5.QtWidgets import QMainWindow
10-
from PyQt5.QtWidgets import QStatusBar
11-
from PyQt5.QtWidgets import QToolBar
5+
from PyQt6.QtWidgets import (
6+
QApplication,
7+
QLabel,
8+
QMainWindow,
9+
QStatusBar,
10+
QToolBar,
11+
)
1212

1313

1414
class Window(QMainWindow):
15-
"""Main Window."""
16-
17-
def __init__(self, parent=None):
18-
"""Initializer."""
19-
super().__init__(parent)
15+
def __init__(self):
16+
super().__init__(parent=None)
2017
self.setWindowTitle("QMainWindow")
2118
self.setCentralWidget(QLabel("I'm the Central Widget"))
2219
self._createMenu()
2320
self._createToolBar()
2421
self._createStatusBar()
2522

2623
def _createMenu(self):
27-
self.menu = self.menuBar().addMenu("&Menu")
28-
self.menu.addAction("&Exit", self.close)
24+
menu = self.menuBar().addMenu("&Menu")
25+
menu.addAction("&Exit", self.close)
2926

3027
def _createToolBar(self):
3128
tools = QToolBar()
@@ -39,7 +36,7 @@ def _createStatusBar(self):
3936

4037

4138
if __name__ == "__main__":
42-
app = QApplication(sys.argv)
43-
win = Window()
44-
win.show()
45-
sys.exit(app.exec_())
39+
app = QApplication([])
40+
window = Window()
41+
window.show()
42+
sys.exit(app.exec())
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Signals and slots example."""
2+
3+
import sys
4+
5+
from PyQt6.QtWidgets import (
6+
QApplication,
7+
QLabel,
8+
QPushButton,
9+
QVBoxLayout,
10+
QWidget,
11+
)
12+
13+
14+
def greet():
15+
if msgLabel.text():
16+
msgLabel.setText("")
17+
else:
18+
msgLabel.setText("Hello World!")
19+
20+
21+
app = QApplication([])
22+
window = QWidget()
23+
window.setWindowTitle("Signals and slots")
24+
layout = QVBoxLayout()
25+
26+
button = QPushButton("Greet")
27+
button.clicked.connect(greet)
28+
29+
layout.addWidget(button)
30+
msgLabel = QLabel("")
31+
layout.addWidget(msgLabel)
32+
window.setLayout(layout)
33+
window.show()
34+
sys.exit(app.exec())

pyqt-calculator-tutorial/examples/signals_slots.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

pyqt-calculator-tutorial/examples/signals_slots_partial.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)