10000 Merge pull request #60 from realpython/pyqt-calculator-tutorial · realpython/materials@06bcbdf · GitHub
[go: up one dir, main page]

Skip to content

Commit 06bcbdf

Browse files
authored
Merge pull request #60 from realpython/pyqt-calculator-tutorial
Pyqt Calculator Tutorial
2 parents 8931940 + 05486bf commit 06bcbdf

File tree

15 files changed

+486
-0
lines changed

15 files changed

+486
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Filename: dialog.py
2+
3+
"""Dialog-Style application."""
4+
5+
import sys
6+
7+
from PyQt5.QtWidgets import QApplication
8+
from PyQt5.QtWidgets import QDialog
9+
from PyQt5.QtWidgets import QLineEdit
10+
from PyQt5.QtWidgets import QVBoxLayout
11+
from PyQt5.QtWidgets import QFormLayout
12+
from PyQt5.QtWidgets import QDialogButtonBox
13+
14+
15+
class Dialog(QDialog):
16+
"""Dialog."""
17+
18+
def __init__(self, parent=None):
19+
"""Initializer."""
20+
super().__init__(parent)
21+
self.setWindowTitle("QDialog")
22+
dlgLayout = QVBoxLayout()
23+
formLayout = QFormLayout()
24+
formLayout.addRow("Name:", QLineEdit())
25+
formLayout.addRow("Age:", QLineEdit())
26+
formLayout.addRow("Job:", QLineEdit())
27+
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)
33+
34+
35+
if __name__ == "__main__":
36+
app = QApplication(sys.argv)
37+
dlg = Dialog()
38+
dlg.show()
39+
sys.exit(app.exec_())
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Filename: f_layout.py
2+
3+
"""Form layout example."""
4+
5+
import sys
6+
7+
from PyQt5.QtWidgets import QApplication
8+
from PyQt5.QtWidgets import QWidget
9+
from PyQt5.QtWidgets import QLineEdit
10+
from PyQt5.QtWidgets import QFormLayout
11+
12+
app = QApplication(sys.argv)
13+
window = QWidget()
14+
window.setWindowTitle("QFormLayout")
15+
layout = QFormLayout()
16+
layout.addRow("Name:", QLineEdit())
17+
layout.addRow("Age:", QLineEdit())
18+
layout.addRow("Job:", QLineEdit())
19+
layout.addRow("Hobbies:", QLineEdit())
20+
window.setLayout(layout)
21+
window.show()
22+
sys.exit(app.exec_())
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Filename: g_layout.py
2+
3+
"""Grid layout example."""
4+
5+
import sys
6+
7+
from PyQt5.QtWidgets import QApplication
8+
from PyQt5.QtWidgets import QWidget
9+
from PyQt5.QtWidgets import QPushButton
10+
from PyQt5.QtWidgets import QGridLayout
11+
12+
app = QApplication(sys.argv)
13+
window = QWidget()
14+
window.setWindowTitle("QGridLayout")
15+
layout = QGridLayout()
16+
layout.addWidget(QPushButton("Button (0, 0)"), 0, 0)
17+
layout.addWidget(QPushButton("Button (0, 1)"), 0, 1)
18+
layout.addWidget(QPushButton("Button (0, 2)"), 0, 2)
19+
layout.addWidget(QPushButton("Button (1, 0)"), 1, 0)
20+
layout.addWidget(QPushButton("Button (1, 1)"), 1, 1)
21+
layout.addWidget(QPushButton("Button (1, 2)"), 1, 2)
22+
layout.addWidget(QPushButton("Button (2, 0)"), 2, 0)
23+
layout.addWidget(QPushButton("Button (2, 1) + 2 Columns Span"), 2, 1, 1, 2)
24+
window.setLayout(layout)
25+
window.show()
26+
sys.exit(app.exec_())
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Filename: h_layout.py
2+
3+
"""Horizontal layout example."""
4+
5+
import sys
6+
7+
from PyQt5.QtWidgets import QApplication
8+
from PyQt5.QtWidgets import QWidget
9+
from PyQt5.QtWidgets import QPushButton
10+
from PyQt5.QtWidgets import QHBoxLayout
11+
12+
app = QApplication(sys.argv)
13+
window = QWidget()
14+
window.setWindowTitle("QHBoxLayout")
15+
layout = QHBoxLayout()
16+
layout.addWidget(QPushButton("Right"))
17+
layout.addWidget(QPushButton("Center"))
18+
layout.addWidget(QPushButton("Left"))
19+
window.setLayout(layout)
20+
window.show()
21+
sys.exit(app.exec_())
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Filename: hello.py
2+
3+
"""Simple Hello World example with PyQt5."""
4+
5+
import sys
6+
7+
# 1. Import `QApplication`, and all the required widgets
8+
from PyQt5.QtWidgets import QApplication
9+
from PyQt5.QtWidgets import QWidget
10+
from PyQt5.QtWidgets import QLabel
11+
12+
# 2. Create an instance of QApplication
13+
app = QApplication(sys.argv)
14+
15+
# 3. Create an instance of your application's GUI
16+
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)
22+
23+
# 4.Show your application's GUI
24+
window.show()
25+
26+
# 5. Run your application's event loop (or main loop)
27+
sys.exit(app.exec_())
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Filename: main_window.py
2+
3+
"""Main Window-Style application."""
4+
5+
import sys
6+
7+
from PyQt5.QtWidgets import QApplication
8+
from PyQt5.QtWidgets import QMainWindow
9+
from PyQt5.QtWidgets import QLabel
10+
from PyQt5.QtWidgets import QStatusBar
11+
from PyQt5.QtWidgets import QToolBar
12+
13+
14+
class Window(QMainWindow):
15+
"""Main Window."""
16+
17+
def __init__(self, parent=None):
18+
"""Initializer."""
19+
super().__init__(parent)
20+
self.setWindowTitle("QMainWindow")
21+
self.setCentralWidget(QLabel("I'm the Central Widget"))
22+
self._createMenu()
23+
self._createToolBar()
24+
self._createStatusBar()
25+
26+
def _createMenu(self):
27+
self.menu = self.menuBar().addMenu("&Menu")
28+
self.menu.addAction("&Exit", self.close)
29+
30+
def _createToolBar(self):
31+
tools = QToolBar()
32+
self.addToolBar(tools)
33+
tools.addAction("Exit", self.close)
34+
35+
def _createStatusBar(self):
36+
status = QStatusBar()
37+
status.showMessage("I'm the Status Bar")
38+
self.setStatusBar(status)
39+
40+
41+
if __name__ == "__main__":
42+
app = QApplication(sys.argv)
43+
win = Window()
44+
win.show()
45+
sys.exit(app.exec_())
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Filename: signals_slots.py
2+
3+
"""Signals, and Slots example."""
4+
5+
import sys
6+
7+
from PyQt5.QtWidgets import QApplication
8+
from PyQt5.QtWidgets import QWidget
9+
from PyQt5.QtWidgets import QPushButton
10+
from PyQt5.QtWidgets import QLabel
11+
from PyQt5.QtWidgets import QVBoxLayout
12+
13+
14+
def greeting():
15+
"""Slot function."""
16+
msg.setText("Hello World!")
17+
18+
19+
app = QApplication(sys.argv)
20+
window = QWidget()
21+
window.setWindowTitle("Signals, and Slots")
22+
layout = QVBoxLayout()
23+
24+
btn = QPushButton("Greet")
25+
btn.clicked.connect(greeting) # Connect clicked to greeting()
26+
27+
layout.addWidget(btn)
28+
msg = QLabel("")
29+
layout.addWidget(msg)
30+
window.setLayout(layout)
31+
window.show()
32+
sys.exit(app.exec_())
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Filename: v_layout.py
2+
3+
"""Vertical layout example."""
4+
5+
import sys
6+
7+
from PyQt5.QtWidgets import QApplication
8+
from PyQt5.QtWidgets import QWidget
9+
from PyQt5.QtWidgets import QPushButton
10+
from PyQt5.QtWidgets import QVBoxLayout
11+
12+
app = QApplication(sys.argv)
13+
window = QWidget()
14+
window.setWindowTitle("QVBoxLayout")
15+
layout = QVBoxLayout()
16+
layout.addWidget(QPushButton("Top"))
17+
layout.addWidget(QPushButton("Center"))
18+
layout.addWidget(QPushButton("Bottom"))
19+
window.setLayout(layout)
20+
window.show()
21+
sys.exit(app.exec_())
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Byte-compiled / optimized
2+
__pycache__/
3+
*.py[cod]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Prahlad Yeri
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)
0