2
0
mirror of https://github.com/pyqt/examples.git synced 2025-09-02 06:55:17 +00:00

Integrate unofficial examples with screenshots

This commit is contained in:
Michael Herrmann
2019-06-27 14:38:52 +02:00
parent 2646734e4c
commit c0adffcf62
90 changed files with 2185 additions and 2 deletions

View File

@@ -0,0 +1,18 @@
# Qt Text Editor
This example implements a simple text editor with (Py)Qt.
![Qt Text Editor](screenshots/qt-text-editor-windows.png) ![QMenu example](screenshots/qmenu-example.png)
![Qt QMenu](screenshots/qt-qmenu.png) ![QDialog example](screenshots/qdialog-example.png)
![QMessageBox example](screenshots/qmessagebox-example.png)
It has a surprising number of features:
* A *File* menu for opening and saving files.
* Keyboard shortcuts.
* An *About* dialog.
* A warning *Do you want to save before quitting?* if there are unmodified changes.
The full source code is in [`main.py`](main.py). For instructions on how to run it, please see [here](https://github.com/1mh/pyqt-examples#running-the-examples).

View File

@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg id="svg11300" xmlns="http://www.w3.org/2000/svg" height="48" width="48" version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs id="defs">
<radialGradient id="R1" xlink:href="#G1" gradientUnits="userSpaceOnUse" cy="486.65" cx="605.71" gradientTransform="matrix(-2.7744 0 0 1.9697 112.76 -872.89)" r="117.14"/>
<linearGradient id="G1">
<stop id="s1" offset="0"/>
<stop id="s2" stop-opacity="0" offset="1"/>
</linearGradient>
<radialGradient id="R2" xlink:href="#G1" gradientUnits="userSpaceOnUse" cy="486.65" cx="605.71" gradientTransform="matrix(2.7744 0 0 1.9697 -1891.6 -872.89)" r="117.14"/>
<linearGradient id="G3" y2="609.51" gradientUnits="userSpaceOnUse" x2="302.86" gradientTransform="matrix(2.7744 0 0 1.9697 -1892.2 -872.89)" y1="366.65" x1="302.86">
<stop id="s3" stop-opacity="0" offset="0"/>
<stop id="s4" offset=".5"/>
<stop id="s5" stop-opacity="0" offset="1"/>
</linearGradient>
<linearGradient id="G12" y2="6.8334" gradientUnits="userSpaceOnUse" x2="14.284" gradientTransform="matrix(1.1379 0 0 1 -2.6609 0)" y1="42.833" x1="21.043">
<stop id="s21" stop-color="#dfdfdf" offset="0"/>
<stop id="s22" stop-color="#fff" offset="1"/>
</linearGradient>
<linearGradient id="G13" y2="42.833" gradientUnits="userSpaceOnUse" x2="26.228" y1="28.083" x1="26.612">
<stop id="s17" stop-color="#939393" offset="0"/>
<stop id="s18" stop-color="#424242" offset="1"/>
</linearGradient>
<linearGradient id="G14" y2="7.5625" gradientUnits="userSpaceOnUse" x2="40.984" y1="7.5625" x1="6">
<stop id="s15" stop-color="#a3a4a0" offset="0"/>
<stop id="s16" stop-color="#888a85" offset="1"/>
</linearGradient>
<linearGradient id="G15" y2="22.251" gradientUnits="userSpaceOnUse" x2="50.988" gradientTransform="translate(-5.6693)" y1="17.376" x1="48.906">
<stop id="s12" stop-color="#ffd1d1" offset="0"/>
<stop id="s13" stop-color="#ff1d1d" offset=".5"/>
<stop id="s14" stop-color="#6f0000" offset="1"/>
</linearGradient>
<linearGradient id="G16" y2="22.625" gradientUnits="userSpaceOnUse" x2="47.688" gradientTransform="translate(-5.6693)" y1="19.812" x1="46">
<stop id="s10" stop-color="#c1c1c1" offset="0"/>
<stop id="s11" stop-color="#acacac" offset="1"/>
</linearGradient>
<radialGradient id="radialGradient2990" gradientUnits="userSpaceOnUse" cy="27.641" cx="29.053" gradientTransform="matrix(2.9236 0 0 2.0297 -61.555 -27.884)" r="3.2409">
<stop id="s8" stop-color="#e7e2b8" offset="0"/>
<stop id="s9" stop-color="#e7e2b8" stop-opacity="0" offset="1"/>
</radialGradient>
<linearGradient id="G17" y2="30.703" gradientUnits="userSpaceOnUse" x2="25.515" gradientTransform="translate(-5.8255 .125)" y1="31.047" x1="25.719">
<stop id="s6" offset="0"/>
<stop id="s7" stop-color="#c9c9c9" offset="1"/>
</linearGradient>
<radialGradient id="R3" gradientUnits="userSpaceOnUse" cy="40.438" cx="23.562" gradientTransform="matrix(1 0 0 .34824 0 26.355)" r="19.562">
<stop id="s19" offset="0"/>
<stop id="s20" stop-opacity="0" offset="1"/>
</radialGradient>
</defs>
<g id="layer1">
<g id="g6707" transform="matrix(.024176 0 0 .020868 45.128 40.154)">
<rect id="rect6709" opacity=".4" height="478.36" width="1339.6" y="-150.7" x="-1559.3" fill="url(#G3)"/>
<path id="p1" opacity=".4" fill="url(#R2)" d="m-219.62-150.68v478.33c142.88 0.9 345.4-107.17 345.4-239.2 0-132.02-159.44-239.13-345.4-239.13z"/>
<path id="p2" opacity=".4" fill="url(#R1)" d="m-1559.3-150.68v478.33c-142.8 0.9-345.4-107.17-345.4-239.2 0-132.02 159.5-239.13 345.4-239.13z"/>
</g>
<path id="p3" fill="url(#G12)" d="m7.1639 4.5064h32.649c0.763 0 1.377 0.5324 1.377 1.1938l2.401 34.169 0.012 2.348c0 0.661-0.614 1.193-1.376 1.193h-37.477c-0.7625 0-1.3764-0.532-1.3764-1.193l-0.0112-2.167 2.425-34.35c0-0.6612 0.6139-1.1936 1.3765-1.1936z" stroke="url(#G13)"/>
<path id="p4" opacity=".31579" d="m43.125 40.438a19.562 6.8125 0 1 1 -39.125 0 19.562 6.8125 0 1 1 39.125 0z" transform="matrix(.61661 0 0 .44037 10.614 13.943)" fill="url(#R3)"/>
<rect id="rect2851" rx=".67938" ry=".67938" height="3.0715" width="39.048" y="39.868" x="3.977" fill="#a4a4a4"/>
<path id="p5" fill="#fff" d="m3.9268 40.443s0.1508-0.531 0.704-0.575h37.564c0.755 0 0.805 0.752 0.805 0.752s0.024-1.62-1.284-1.62h-36.412c-1.0055 0.088-1.3772 0.78-1.3772 1.443z"/>
<path id="p6" fill="url(#G14)" d="m6.25 5.7344l-0.25 4.3906s0.3125-1.125 1-1.125h33.125c0.703-0.0156 0.734 0.3125 0.859 0.8281l-0.25-3.875c-0.031-0.5469-0.218-0.9531-0.781-0.9531h-32.89c-0.4536 0-0.7661 0.3437-0.813 0.7344z"/>
<path id="p7" opacity=".43860" d="m7.8126 5.5405h31.132c0.722 0 1.303-0.1522 1.303 0.4741 0 0 2.274 33.008 2.274 33.008l0.1 2.709c0 0.626-0.139 0.644-0.861 0.644h-36.899c-0.4126 0-0.4194-0.106-0.4194-0.511l-0.0106-2.671 2.2961-33.148c0-0.6264 0.3625-0.5055 1.0845-0.5055z" stroke="#fff" fill="none"/>
<g id="g2950" stroke="#886f00" fill="#fce94f">
<rect id="rect2899" rx="1" ry="1" height="5" width="2" y="2.5" x="8.5"/>
<rect id="rect2901" rx="1" ry="1" height="5" width="2" y="2.5" x="12.5"/>
<rect id="rect2903" rx="1" ry="1" height="5" width="2" y="2.5" x="16.5"/>
<rect id="rect2905" rx="1" ry="1" height="5" width="2" y="2.5" x="20.5"/>
<rect id="rect2907" rx="1" ry="1" height="5" width="2" y="2.5" x="24.5"/>
<rect id="rect2909" rx="1" ry="1" height="5" width="2" y="2.5" x="28.5"/>
<rect id="rect2911" rx="1" ry="1" height="5" width="2" y="2.5" x="32.5"/>
<rect id="rect2913" rx="1" ry="1" height="5" width="2" y="2.5" x="36.5"/>
</g>
<g id="g2941">
<rect id="rect2927" opacity=".2807" height="1" width="29" y="12" x="9"/>
<rect id="rect2929" opacity=".2807" height="1" width="29" y="14.982" x="9"/>
<rect id="rect2931" opacity=".2807" height="1" width="13" y="18.004" x="9"/>
<rect id="rect2933" opacity=".2807" height="1" width="29" y="22.986" x="9"/>
<rect id="rect2935" opacity=".2807" height="1" width="29" y="26.008" x="9"/>
<rect id="rect2937" opacity=".2807" height="1" width="29" y="29.03" x="9"/>
<rect id="rect2939" opacity=".2807" height="1" width="8" y="32.052" x="9"/>
</g>
<path id="path2960" fill="#cb9022" d="m17.341 32.5l5.625-5.625 20.094-9.75c3.25-1.25 5.187 3.375 2.312 5l-20.031 9.375-8 1z" stroke="#5c410c"/>
<path id="p8" fill="url(#G15)" d="m38.331 20s1.437 0.094 2 1.344c0.579 1.288 0 2.656 0 2.656l5.031-2.469s1.452-0.881 0.656-2.843c-0.785-1.936-2.687-1.157-2.687-1.157l-5 2.469z"/>
<path id="p9" fill="url(#G16)" d="m38.331 20s1.437 0.094 2 1.344c0.579 1.288 0 2.656 0 2.656l2-1s0.827-1.319 0.218-2.688c-0.625-1.406-2.218-1.312-2.218-1.312l-2 1z"/>
<path id="p10" fill="url(#radialGradient2990)" d="m18.768 31.781l4.5-4.5c1.5 0.813 2.281 2.157 1.875 3.719l-6.375 0.781z"/>
<path id="p11" fill="url(#G17)" d="m20.112 30.375l-1.625 1.594 2.344-0.313c0.218-0.718-0.188-1.062-0.719-1.281z"/>
<path id="p12" fill-opacity=".36364" fill="#fff" d="m23.268 27.25l1.563 1.25 15.387-7.319c-0.444-0.856-1.242-1.084-1.903-1.162l-15.047 7.231z"/>
<path id="p13" fill-opacity=".36364" d="m25.143 31.062l0.188-0.75 15.231-7.129s-0.11 0.614-0.216 0.749l-15.203 7.13z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 7.0 KiB

View File

@@ -0,0 +1,79 @@
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QKeySequence
class MainWindow(QMainWindow):
def closeEvent(self, e):
if not text.document().isModified():
return
answer = QMessageBox.question(
window, None,
"You have unsaved changes. Save before closing?",
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel
)
if answer & QMessageBox.Save:
save()
elif answer & QMessageBox.Cancel:
e.ignore()
app = QApplication([])
app.setApplicationName("Text Editor")
text = QPlainTextEdit()
window = MainWindow()
window.setCentralWidget(text)
file_path = None
menu = window.menuBar().addMenu("&File")
open_action = QAction("&Open")
def open_file():
global file_path
path = QFileDialog.getOpenFileName(window, "Open")[0]
if path:
text.setPlainText(open(path).read())
file_path = path
open_action.triggered.connect(open_file)
open_action.setShortcut(QKeySequence.Open)
menu.addAction(open_action)
save_action = QAction("&Save")
def save():
if file_path is None:
save_as()
else:
with open(file_path, "w") as f:
f.write(text.toPlainText())
text.document().setModified(False)
save_action.triggered.connect(save)
save_action.setShortcut(QKeySequence.Save)
menu.addAction(save_action)
save_as_action = QAction("Save &As...")
def save_as():
global file_path
path = QFileDialog.getSaveFileName(window, "Save As")[0]
if path:
file_path = path
save()
save_as_action.triggered.connect(save_as)
menu.addAction(save_as_action)
close = QAction("&Close")
close.triggered.connect(window.close)
menu.addAction(close)
help_menu = window.menuBar().addMenu("&Help")
about_action = QAction("&About")
help_menu.addAction(about_action)
def show_about_dialog():
text = "<center>" \
"<h1>Text Editor</h1>" \
"&#8291;" \
"<img src=icon.svg>" \
"</center>" \
"<p>Version 31.4.159.265358<br/>" \
"Copyright &copy; Company Inc.</p>"
QMessageBox.about(window, "About Text Editor", text)
about_action.triggered.connect(show_about_dialog)
window.show()
app.exec_()

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB