Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyqt6 can not run normally in several example because it upgrade version #24

Open
wants to merge 9 commits into
base: _
Choose a base branch
from
7 changes: 4 additions & 3 deletions src/07 Qt Text Editor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ def closeEvent(self, e):
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([])
Expand Down
12 changes: 6 additions & 6 deletions src/08 PyQt6 exe/src/main/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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):
Expand All @@ -15,11 +15,11 @@ def closeEvent(self, e):
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()
Expand All @@ -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")
Expand All @@ -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...")
Expand Down
39 changes: 20 additions & 19 deletions src/09 Qt dark theme/main.py
Original file line number Diff line number Diff line change
@@ -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([])
Expand All @@ -9,19 +9,19 @@

# 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.
Expand All @@ -37,11 +37,12 @@ def closeEvent(self, e):
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()
Expand All @@ -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")
Expand All @@ -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...")
Expand Down
20 changes: 12 additions & 8 deletions src/10 QPainter Python example/main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
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):
super().__init__()
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())
Expand All @@ -34,11 +37,12 @@ def closeEvent(self, e):
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")
Expand All @@ -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")
Expand All @@ -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...")
Expand Down
2 changes: 1 addition & 1 deletion src/11 PyQt Thread example/01_single_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# 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)
Expand Down
2 changes: 1 addition & 1 deletion src/11 PyQt Thread example/02_multithreaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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)
Expand Down
2 changes: 1 addition & 1 deletion src/11 PyQt Thread example/03_with_threadutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# 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)
Expand Down