2
0
mirror of https://github.com/pyqt/examples.git synced 2025-08-22 17:47:10 +00:00

Fixes for PyQt6 (#24)

This commit is contained in:
九是否随意的称呼 2025-01-07 14:29:45 +08:00 committed by GitHub
parent a72871707b
commit 83eed05145
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 45 additions and 39 deletions

View File

@ -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([])

View File

@ -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...")

View File

@ -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...")

View File

@ -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...")

View File

@ -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)

View File

@ -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)

View File

@ -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)