Skip to content

Commit 8aa902c

Browse files
authored
Merge pull request #81 from realpython/pyqt-calculator-tutorial
Pyqt calculator tutorial
2 parents 6b7bfed + 34ad7bb commit 8aa902c

File tree

11 files changed

+80
-40
lines changed

11 files changed

+80
-40
lines changed

pyqt-calculator-tutorial/examples/dialog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
from PyQt5.QtWidgets import QApplication
88
from PyQt5.QtWidgets import QDialog
9+
from PyQt5.QtWidgets import QDialogButtonBox
10+
from PyQt5.QtWidgets import QFormLayout
911
from PyQt5.QtWidgets import QLineEdit
1012
from PyQt5.QtWidgets import QVBoxLayout
11-
from PyQt5.QtWidgets import QFormLayout
12-
from PyQt5.QtWidgets import QDialogButtonBox
1313

1414

1515
class Dialog(QDialog):

pyqt-calculator-tutorial/examples/f_layout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import sys
66

77
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QWidget
9-
from PyQt5.QtWidgets import QLineEdit
108
from PyQt5.QtWidgets import QFormLayout
9+
from PyQt5.QtWidgets import QLineEdit
10+
from PyQt5.QtWidgets import QWidget
1111

1212
app = QApplication(sys.argv)
1313
window = QWidget()

pyqt-calculator-tutorial/examples/g_layout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import sys
66

77
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QWidget
9-
from PyQt5.QtWidgets import QPushButton
108
from PyQt5.QtWidgets import QGridLayout
9+
from PyQt5.QtWidgets import QPushButton
10+
from PyQt5.QtWidgets import QWidget
1111

1212
app = QApplication(sys.argv)
1313
window = QWidget()

pyqt-calculator-tutorial/examples/h_layout.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
import sys
66

77
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QWidget
9-
from PyQt5.QtWidgets import QPushButton
108
from PyQt5.QtWidgets import QHBoxLayout
9+
from PyQt5.QtWidgets import QPushButton
10+
from PyQt5.QtWidgets import QWidget
1111

1212
app = QApplication(sys.argv)
1313
window = QWidget()
1414
window.setWindowTitle("QHBoxLayout")
1515
layout = QHBoxLayout()
16-
layout.addWidget(QPushButton("Right"))
17-
layout.addWidget(QPushButton("Center"))
1816
layout.addWidget(QPushButton("Left"))
17+
layout.addWidget(QPushButton("Center"))
18+
layout.addWidget(QPushButton("Right"))
1919
window.setLayout(layout)
2020
window.show()
2121
sys.exit(app.exec_())

pyqt-calculator-tutorial/examples/hello.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import sys
66

7-
# 1. Import `QApplication`, and all the required widgets
7+
# 1. Import `QApplication` and all the required widgets
88
from PyQt5.QtWidgets import QApplication
9-
from PyQt5.QtWidgets import QWidget
109
from PyQt5.QtWidgets import QLabel
10+
from PyQt5.QtWidgets import QWidget
1111

1212
# 2. Create an instance of QApplication
1313
app = QApplication(sys.argv)

pyqt-calculator-tutorial/examples/main_window.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import sys
66

77
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QMainWindow
98
from PyQt5.QtWidgets import QLabel
9+
from PyQt5.QtWidgets import QMainWindow
1010
from PyQt5.QtWidgets import QStatusBar
1111
from PyQt5.QtWidgets import QToolBar
1212

pyqt-calculator-tutorial/examples/signals_slots.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# Filename: signals_slots.py
22

3-
"""Signals, and Slots example."""
3+
"""Signals and slots example."""
44

55
import sys
66

77
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QWidget
9-
from PyQt5.QtWidgets import QPushButton
108
from PyQt5.QtWidgets import QLabel
9+
from PyQt5.QtWidgets import QPushButton
1110
from PyQt5.QtWidgets import QVBoxLayout
11+
from PyQt5.QtWidgets import QWidget
1212

1313

1414
def greeting():
1515
"""Slot function."""
16-
msg.setText("Hello World!")
16+
if msg.text():
17+
msg.setText("")
18+
else:
19+
msg.setText("Hello World!")
1720

1821

1922
app = QApplication(sys.argv)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Filename: signals_slots_partial.py
2+
3+
"""Signals and slots example."""
4+
5+
import functools
6+
import sys
7+
8+
from PyQt5.QtWidgets import QApplication
9+
from PyQt5.QtWidgets import QLabel
10+
from PyQt5.QtWidgets import QPushButton
11+
from PyQt5.QtWidgets import QVBoxLayout
12+
from PyQt5.QtWidgets import QWidget
13+
14+
15+
def greeting(who):
16+
"""Slot function."""
17+
if msg.text():
18+
msg.setText("")
19+
else:
20+
msg.setText(f"Hello {who}")
21+
22+
23+
app = QApplication(sys.argv)
24+
window = QWidget()
25+
window.setWindowTitle("Signals, and Slots")
26+
layout = QVBoxLayout()
27+
28+
btn = QPushButton("Greet")
29+
btn.clicked.connect(functools.partial(greeting, "World!"))
30+
31+
layout.addWidget(btn)
32+
msg = QLabel("")
33+
layout.addWidget(msg)
34+
window.setLayout(layout)
35+
window.show()
36+
sys.exit(app.exec_())

pyqt-calculator-tutorial/examples/v_layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import sys
66

77
from PyQt5.QtWidgets import QApplication
8-
from PyQt5.QtWidgets import QWidget
98
from PyQt5.QtWidgets import QPushButton
109
from PyQt5.QtWidgets import QVBoxLayout
10+
from PyQt5.QtWidgets import QWidget
1111

1212
app = QApplication(sys.argv)
1313
window = QWidget()

pyqt-calculator-tutorial/pycalc/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PyCalc
22

3-
PyCalc is a sample calculator implemented using Python 3, and with a [PyQt5](https://www.riverbankcomputing.com/static/Docs/PyQt5/introduction.html) GUI. PyCalc implements the most basic math operations, that is, division, multiplication, addition, and subtraction.
3+
PyCalc is a sample calculator implemented using Python 3 and with a [PyQt5](https://www.riverbankcomputing.com/static/Docs/PyQt5/introduction.html) GUI. PyCalc implements the most basic math operations, that is, division, multiplication, addition, and subtraction.
44

55
PyCalc is intended to be a demonstrative example on how you can implement a Python + PyQt5 GUI application using the Model-View-Controller (MVC) pattern.
66

@@ -40,7 +40,7 @@ After running this command, you'll see PyCalc running on your screen.
4040

4141
## How to Use PyCalc
4242

43-
To use PyCalc, you just need to enter a valid math expression using your mouse, and then press `Enter` or click on the `=` sign:
43+
To use PyCalc, you just need to enter a valid math expression using your mouse and then press `Enter` or click on the `=` sign:
4444

4545
![Screenshot](pycalc-howto.gif)
4646

0 commit comments

Comments
 (0)