Skip to content

Commit ddbdbec

Browse files
committed
Update sample code
1 parent 5e982b6 commit ddbdbec

File tree

11 files changed

+165
-254
lines changed

11 files changed

+165
-254
lines changed
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: 15 additions & 13 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+
window.setWindowTitle("PyQt6 App")
21+
window.setGeometry(WIN_X, WIN_Y, WIDTH, HEIGHT)
2022
helloMsg = QLabel("<h1>Hello World!</h1>", parent=window)
21-
helloMsg.move(60, 15)
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())

pyqt-calculator-tutorial/examples/signals_slots_partial.py

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
# Filename: v_layout.py
2-
31
"""Vertical layout example."""
42

53
import sys
64

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

12-
app = QApplication(sys.argv)
7+
app = QApplication([])
138
window = QWidget()
149
window.setWindowTitle("QVBoxLayout")
10+
1511
layout = QVBoxLayout()
1612
layout.addWidget(QPushButton("Top"))
1713
layout.addWidget(QPushButton("Center"))
1814
layout.addWidget(QPushButton("Bottom"))
1915
window.setLayout(layout)
16+
2017
window.show()
21-
sys.exit(app.exec_())
18+
sys.exit(app.exec())
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
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 [PyQt6](https://www.riverbankcomputing.com/static/Docs/PyQt6/introduction.html) GUI. PyCalc implements basic math operations, including division, multiplication, addition, and subtraction.
44

5-
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.
5+
PyCalc is intended to be a demonstrative example of how to build a Python + PyQt6 GUI application using the Model-View-Controller (MVC) pattern.
66

77
## Screenshot
88

99
![Screenshot](screenshot.png)
1010

1111
## Requirements
1212

13-
For PyCalc to work, you need to have a proper installation of [Python](https://www.python.org) >= 3.6. Then you need to install the PyQt5 library in your system. This can be done by using `pip3` as follows:
13+
For PyCalc to work, you need to have [Python](https://www.python.org) >= 3.6.1. Then you need to install the PyQt6 library. You can do this by using `pip` in a Python virtual environment:
1414

1515
```console
16-
$ sudo pip3 install pyqt5
16+
$ python -m venv venv
17+
$ source venv/bin/activate
18+
$ python -m pip install pyqt6
1719
```
1820

19-
We don't recommend you to install PyCalc's requirements directly into your system, as PyCalc is just a sample application. So, you can use a virtual environment to test PyCalc out. To do that, you can run the following commands:
20-
21-
```console
22-
$ python3 -m venv pycalc
23-
$ source pycalc/bin/activate
24-
$ pip install pyqt5
25-
```
26-
27-
After this steps are finished, you can run and test PyCalc as described in the next section.
28-
29-
**Note**: For more information on how to install PyQt5, you can take a look at the related topic on the [project's documentation](https://www.riverbankcomputing.com/static/Docs/PyQt5/installation.html).
21+
After these commands have finished, you can run PyCalc as described in the next section.
3022

3123
## How to Run PyCalc
3224

33-
To run PyCalc from your system's command-line and try it out, you can execute the following command:
25+
To run PyCalc from your system's command line or terminal, execute the following command:
3426

3527
```console
36-
$ python3 pycalc.py
28+
$ python pycalc.py
3729
```
3830

39-
After running this command, you'll see PyCalc running on your screen.
31+
After running this command, you'll get PyCalc running on your machine.
4032

4133
## How to Use PyCalc
4234

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:
35+
To use PyCalc, just enter a valid math expression using your mouse and then press `Enter` or click the `=` button to get the result:
4436

4537
![Screenshot](pycalc-howto.gif)
4638

4739
## About the Author
4840

49-
My name is Leodanis Pozo Ramos. I'm a **freelance Python Developer, and [author](https://realpython.com/team/lpozoramos/)**. If you need more information about me, and my work, then you can take a look at my [personal page](https://lpozo.github.com/).
50-
51-
## License
52-
53-
PyCalc is released under the [MIT License](https://opensource.org/licenses/MIT).
41+
Leodanis Pozo Ramos is a **self-taught Python Developer, and [content creator](https://realpython.com/team/lpozoramos/)** at Real Python.

0 commit comments

Comments
 (0)