diff --git a/src/07 Qt Text Editor/main.py b/src/07 Qt Text Editor/main.py index fe49858..1362369 100644 --- a/src/07 Qt Text Editor/main.py +++ b/src/07 Qt Text Editor/main.py @@ -8,16 +8,17 @@ class MainWindow(QMainWindow): answer = QMessageBox.question( window, None, "You have unsaved changes. Save before closing?", - QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel + QMessageBox.StandardButton.Save | QMessageBox.StandardButton.Discard \ + | QMessageBox.StandardButton.Cancel ) - if answer & QMessageBox.Save: + if answer & QMessageBox.StandardButton.Save: save() if text.document().isModified(): # This happens when the user closes the Save As... dialog. # We do not want to close the window in this case because it # would throw away unsaved changes. e.ignore() - elif answer & QMessageBox.Cancel: + elif answer & QMessageBox.StandardButton.Cancel: e.ignore() app = QApplication([]) diff --git a/src/08 PyQt6 exe/src/main/python/main.py b/src/08 PyQt6 exe/src/main/python/main.py index c07724f..ca1b489 100644 --- a/src/08 PyQt6 exe/src/main/python/main.py +++ b/src/08 PyQt6 exe/src/main/python/main.py @@ -6,7 +6,7 @@ import sys appctxt = ApplicationContext() # 1. Instantiate ApplicationContext from PyQt6.QtWidgets import * -from PyQt6.QtGui import QKeySequence +from PyQt6.QtGui import QKeySequence, QAction class MainWindow(QMainWindow): def closeEvent(self, e): @@ -15,11 +15,11 @@ class MainWindow(QMainWindow): answer = QMessageBox.question( window, None, "You have unsaved changes. Save before closing?", - QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel + QMessageBox.StandardButton.Save | QMessageBox.StandardButton.Discard | QMessageBox.StandardButton.Cancel ) - if answer & QMessageBox.Save: + if answer & QMessageBox.StandardButton.Save: save() - elif answer & QMessageBox.Cancel: + elif answer & QMessageBox.StandardButton.Cancel: e.ignore() text = QPlainTextEdit() @@ -37,7 +37,7 @@ def open_file(): text.setPlainText(open(path).read()) file_path = path open_action.triggered.connect(open_file) -open_action.setShortcut(QKeySequence.Open) +open_action.setShortcut(QKeySequence.StandardKey.Open) menu.addAction(open_action) save_action = QAction("&Save") @@ -49,7 +49,7 @@ def save(): f.write(text.toPlainText()) text.document().setModified(False) save_action.triggered.connect(save) -save_action.setShortcut(QKeySequence.Save) +save_action.setShortcut(QKeySequence.StandardKey.Save) menu.addAction(save_action) save_as_action = QAction("Save &As...") diff --git a/src/09 Qt dark theme/main.py b/src/09 Qt dark theme/main.py index a405db2..b54d210 100644 --- a/src/09 Qt dark theme/main.py +++ b/src/09 Qt dark theme/main.py @@ -1,5 +1,5 @@ from PyQt6.QtWidgets import * -from PyQt6.QtGui import QKeySequence, QPalette, QColor +from PyQt6.QtGui import QKeySequence, QPalette, QColor, QAction from PyQt6.QtCore import Qt app = QApplication([]) @@ -9,19 +9,19 @@ app.setStyle("Fusion") # Now use a palette to switch to dark colors: palette = QPalette() -palette.setColor(QPalette.Window, QColor(53, 53, 53)) -palette.setColor(QPalette.WindowText, Qt.white) -palette.setColor(QPalette.Base, QColor(25, 25, 25)) -palette.setColor(QPalette.AlternateBase, QColor(53, 53, 53)) -palette.setColor(QPalette.ToolTipBase, Qt.white) -palette.setColor(QPalette.ToolTipText, Qt.white) -palette.setColor(QPalette.Text, Qt.white) -palette.setColor(QPalette.Button, QColor(53, 53, 53)) -palette.setColor(QPalette.ButtonText, Qt.white) -palette.setColor(QPalette.BrightText, Qt.red) -palette.setColor(QPalette.Link, QColor(42, 130, 218)) -palette.setColor(QPalette.Highlight, QColor(42, 130, 218)) -palette.setColor(QPalette.HighlightedText, Qt.black) +palette.setColor(QPalette.ColorRole.Window, QColor(53, 53, 53)) +palette.setColor(QPalette.ColorRole.WindowText, Qt.GlobalColor.white) +palette.setColor(QPalette.ColorRole.Base, QColor(25, 25, 25)) +palette.setColor(QPalette.ColorRole.AlternateBase, QColor(53, 53, 53)) +palette.setColor(QPalette.ColorRole.ToolTipBase, Qt.GlobalColor.white) +palette.setColor(QPalette.ColorRole.ToolTipText, Qt.GlobalColor.white) +palette.setColor(QPalette.ColorRole.Text, Qt.GlobalColor.white) +palette.setColor(QPalette.ColorRole.Button, QColor(53, 53, 53)) +palette.setColor(QPalette.ColorRole.ButtonText, Qt.GlobalColor.white) +palette.setColor(QPalette.ColorRole.BrightText, Qt.GlobalColor.red) +palette.setColor(QPalette.ColorRole.Link, QColor(42, 130, 218)) +palette.setColor(QPalette.ColorRole.Highlight, QColor(42, 130, 218)) +palette.setColor(QPalette.ColorRole.HighlightedText, Qt.GlobalColor.black) app.setPalette(palette) # The rest of the code is the same as for the "normal" text editor. @@ -37,11 +37,12 @@ class MainWindow(QMainWindow): answer = QMessageBox.question( window, None, "You have unsaved changes. Save before closing?", - QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel + QMessageBox.StandardButton.Save | QMessageBox.StandardButton.Discard \ + | QMessageBox.StandardButton.Cancel ) - if answer & QMessageBox.Save: + if answer & QMessageBox.StandardButton.Save: save() - elif answer & QMessageBox.Cancel: + elif answer & QMessageBox.StandardButton.Cancel: e.ignore() window = MainWindow() @@ -58,7 +59,7 @@ def open_file(): text.setPlainText(open(path).read()) file_path = path open_action.triggered.connect(open_file) -open_action.setShortcut(QKeySequence.Open) +open_action.setShortcut(QKeySequence.StandardKey.Open) menu.addAction(open_action) save_action = QAction("&Save") @@ -70,7 +71,7 @@ def save(): f.write(text.toPlainText()) text.document().setModified(False) save_action.triggered.connect(save) -save_action.setShortcut(QKeySequence.Save) +save_action.setShortcut(QKeySequence.StandardKey.Save) menu.addAction(save_action) save_as_action = QAction("Save &As...") diff --git a/src/10 QPainter Python example/main.py b/src/10 QPainter Python example/main.py index 3144fc1..6d9f13f 100644 --- a/src/10 QPainter Python example/main.py +++ b/src/10 QPainter Python example/main.py @@ -1,7 +1,7 @@ from PyQt6.QtWidgets import * from PyQt6.QtGui import * from PyQt6.QtCore import * -from PyQt6.QtMultimedia import QSound +from PyQt6.QtMultimedia import QSoundEffect class PlainTextEdit(QPlainTextEdit): def __init__(self): @@ -9,12 +9,15 @@ class PlainTextEdit(QPlainTextEdit): self._holes = [] self._bullet = QPixmap("bullet.png") size = self._bullet.size() - self._offset = QPoint(size.width() / 2, size.height() / 2) + self._offset = QPoint(size.width() // 2, size.height() // 2) + self.effect = QSoundEffect() + self.effect.setSource(QUrl.fromLocalFile("shot.wav")) + self.effect.setVolume(0.26) def mousePressEvent(self, e): self._holes.append(e.pos()) super().mousePressEvent(e) self.viewport().update() - QSound.play("shot.wav") + self.effect.play() def paintEvent(self, e): super().paintEvent(e) painter = QPainter(self.viewport()) @@ -34,11 +37,12 @@ class MainWindow(QMainWindow): answer = QMessageBox.question( window, None, "You have unsaved changes. Save before closing?", - QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel + QMessageBox.StandardButton.Save | QMessageBox.StandardButton.Discard \ + | QMessageBox.StandardButton.Cancel ) - if answer & QMessageBox.Save: + if answer & QMessageBox.StandardButton.Save: save() - elif answer & QMessageBox.Cancel: + elif answer & QMessageBox.StandardButton.Cancel: e.ignore() app.setApplicationName("Text Editor") @@ -56,7 +60,7 @@ def open_file(): text.setPlainText(open(path).read()) file_path = path open_action.triggered.connect(open_file) -open_action.setShortcut(QKeySequence.Open) +open_action.setShortcut(QKeySequence.StandardKey.Open) menu.addAction(open_action) save_action = QAction("&Save") @@ -68,7 +72,7 @@ def save(): f.write(text.toPlainText()) text.document().setModified(False) save_action.triggered.connect(save) -save_action.setShortcut(QKeySequence.Save) +save_action.setShortcut(QKeySequence.StandardKey.Save) menu.addAction(save_action) save_as_action = QAction("Save &As...") diff --git a/src/11 PyQt Thread example/01_single_threaded.py b/src/11 PyQt Thread example/01_single_threaded.py index 533674f..e7333af 100644 --- a/src/11 PyQt Thread example/01_single_threaded.py +++ b/src/11 PyQt Thread example/01_single_threaded.py @@ -9,7 +9,7 @@ server = Session() # GUI: app = QApplication([]) text_area = QPlainTextEdit() -text_area.setFocusPolicy(Qt.NoFocus) +text_area.setFocusPolicy(Qt.FocusPolicy.NoFocus) message = QLineEdit() layout = QVBoxLayout() layout.addWidget(text_area) diff --git a/src/11 PyQt Thread example/02_multithreaded.py b/src/11 PyQt Thread example/02_multithreaded.py index dcfa85a..2d408f1 100644 --- a/src/11 PyQt Thread example/02_multithreaded.py +++ b/src/11 PyQt Thread example/02_multithreaded.py @@ -11,7 +11,7 @@ server = Session() # GUI: app = QApplication([]) text_area = QPlainTextEdit() -text_area.setFocusPolicy(Qt.NoFocus) +text_area.setFocusPolicy(Qt.FocusPolicy.NoFocus) message = QLineEdit() layout = QVBoxLayout() layout.addWidget(text_area) diff --git a/src/11 PyQt Thread example/03_with_threadutil.py b/src/11 PyQt Thread example/03_with_threadutil.py index 69cdd48..81daa95 100644 --- a/src/11 PyQt Thread example/03_with_threadutil.py +++ b/src/11 PyQt Thread example/03_with_threadutil.py @@ -12,7 +12,7 @@ server = Session() # GUI: app = QApplication([]) text_area = QPlainTextEdit() -text_area.setFocusPolicy(Qt.NoFocus) +text_area.setFocusPolicy(Qt.FocusPolicy.NoFocus) message = QLineEdit() layout = QVBoxLayout() layout.addWidget(text_area)