diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 4a9d371..0000000 --- a/LICENSE +++ /dev/null @@ -1,72 +0,0 @@ -Apache License -Version 2.0, January 2004 -http://www.apache.org/licenses/ -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - -"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. - -"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. - -"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. - -"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. - -"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. - -"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. - -"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). - -"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. - -"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." - -"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. - -4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: - -(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and - -(b) You must cause any modified files to carry prominent notices stating that You changed the files; and - -(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and - -(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. - -You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. - -5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. - -6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. - -8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS - -APPENDIX: How to apply the Apache License to your work. - -To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. - -Copyright [yyyy] [name of copyright owner] - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md index ce6ead1..e2064f7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,15 @@ # PyQt5基础教程(持续更新中。。。) +* p39_QTextToSpeech [源码下载](p39_QTextToSpeech) [博文地址](https://xugaoxiang.com/2023/04/18/pyqt5-39-qtexttospeech/) + +* p38_多窗口 [源码下载](p38_多窗口) [博文地址](https://xugaoxiang.com/2023/02/02/pyqt5-38-multiwindow/) + +* p37_大图片放大缩小 [源码下载](p37_大图片放大缩小) [博文地址](https://xugaoxiang.com/2022/12/06/pyqt5-37-image-zoom/) + +* p36_`QLabel` 滚动条 [源码下载](p36_QLabel滚动条) [博文地址](https://xugaoxiang.com/2022/12/04/pyqt5-36-make-label-scrollable/) + +* p35_`QGraphicsView` 显示图片 [源码下载](p35_QGraphicsView显示图片) [博文地址](https://xugaoxiang.com/2022/12/01/pyqt5-35-qgraphicsview/) + * p34_`QComboBox` [源码下载](p34_QComboBox) [博文地址](https://xugaoxiang.com/2022/10/20/pyqt5-34-qcombobox/) * p33_窗口大小设置 [源码下载](p33_窗口大小设置) [博文地址](https://xugaoxiang.com/2022/10/06/pyqt5-33-window-size-setting/) diff --git "a/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/Lenna.jpg" "b/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/Lenna.jpg" new file mode 100644 index 0000000..e37e5c9 Binary files /dev/null and "b/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/Lenna.jpg" differ diff --git "a/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/main.py" "b/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/main.py" new file mode 100644 index 0000000..65959af --- /dev/null +++ "b/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/main.py" @@ -0,0 +1,24 @@ +import sys + +from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene +from PyQt5.QtGui import QPixmap +from ui import Ui_MainWindow + + +class MainWindow(QMainWindow, Ui_MainWindow): + + def __init__(self, parent=None): + super(MainWindow, self).__init__(parent) + self.setupUi(self) + + scene = QGraphicsScene() + img = QPixmap('Lenna.jpg') + img = img.scaled(self.window().width(), self.window().height()) + scene.addPixmap(img) + self.graphicsView.setScene(scene) + +if __name__ == '__main__': + app = QApplication(sys.argv) + windows = MainWindow() + windows.show() + sys.exit(app.exec_()) \ No newline at end of file diff --git "a/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/project.ui" "b/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/project.ui" new file mode 100644 index 0000000..fb66652 --- /dev/null +++ "b/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/project.ui" @@ -0,0 +1,37 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + QGraphicsView示例 + + + + + + + + + + + + 0 + 0 + 800 + 26 + + + + + + + + diff --git "a/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/ui.py" "b/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/ui.py" new file mode 100644 index 0000000..b95817a --- /dev/null +++ "b/p35_QGraphicsView\346\230\276\347\244\272\345\233\276\347\211\207/ui.py" @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +# Form implementation generated from reading ui file '.\project.ui' +# +# Created by: PyQt5 UI code generator 5.9.2 +# +# WARNING! All changes made in this file will be lost! + +from PyQt5 import QtCore, QtGui, QtWidgets + +class Ui_MainWindow(object): + def setupUi(self, MainWindow): + MainWindow.setObjectName("MainWindow") + MainWindow.resize(800, 600) + self.centralwidget = QtWidgets.QWidget(MainWindow) + self.centralwidget.setObjectName("centralwidget") + self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) + self.gridLayout.setObjectName("gridLayout") + self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget) + self.graphicsView.setObjectName("graphicsView") + self.gridLayout.addWidget(self.graphicsView, 0, 0, 1, 1) + MainWindow.setCentralWidget(self.centralwidget) + self.menubar = QtWidgets.QMenuBar(MainWindow) + self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) + self.menubar.setObjectName("menubar") + MainWindow.setMenuBar(self.menubar) + self.statusbar = QtWidgets.QStatusBar(MainWindow) + self.statusbar.setObjectName("statusbar") + MainWindow.setStatusBar(self.statusbar) + + self.retranslateUi(MainWindow) + QtCore.QMetaObject.connectSlotsByName(MainWindow) + + def retranslateUi(self, MainWindow): + _translate = QtCore.QCoreApplication.translate + MainWindow.setWindowTitle(_translate("MainWindow", "QGraphicsView示例")) + diff --git "a/p36_QLabel\346\273\232\345\212\250\346\235\241/main.py" "b/p36_QLabel\346\273\232\345\212\250\346\235\241/main.py" new file mode 100644 index 0000000..03dd39a --- /dev/null +++ "b/p36_QLabel\346\273\232\345\212\250\346\235\241/main.py" @@ -0,0 +1,56 @@ +import sys + +from PyQt5.QtWidgets import QWidget, QLabel, QScrollArea, QApplication, QVBoxLayout, QMainWindow +from PyQt5.QtCore import Qt + + +class MainWindow(QMainWindow): + + def __init__(self): + super().__init__() + self.initUI() + + def initUI(self): + # 实例化滚动区域 + self.scrollarea = QScrollArea() + + self.widget = QWidget() + + # 垂直布局 + self.vboxlayout = QVBoxLayout() + + for i in range(0, 10): + # label显示的文本 + label = QLabel("This is a label.") + + # label加入到垂直布局中 + self.vboxlayout.addWidget(label) + + # 第四个label中显示xml文件的内容 + if i == 3: + # 读取test.xml文件的内容并显示在label上 + with open('test.xml', encoding='UTF-8') as f: + text_label = f.read() + + label.setText(text_label) + + self.widget.setLayout(self.vboxlayout) + + # 设置滚动条的属性 + self.scrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) + self.scrollarea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn) + self.scrollarea.setWidgetResizable(True) + self.scrollarea.setWidget(self.widget) + + # 居中 + self.setCentralWidget(self.scrollarea) + + self.setGeometry(600, 100, 1000, 900) + self.setWindowTitle('PyQt5 Label滚动条示例') + self.show() + + +if __name__ == '__main__': + app = QApplication(sys.argv) + main = MainWindow() + sys.exit(app.exec_()) diff --git "a/p36_QLabel\346\273\232\345\212\250\346\235\241/test.xml" "b/p36_QLabel\346\273\232\345\212\250\346\235\241/test.xml" new file mode 100644 index 0000000..7fee9b1 --- /dev/null +++ "b/p36_QLabel\346\273\232\345\212\250\346\235\241/test.xml" @@ -0,0 +1,62 @@ + + 电动车 + 0.jpg + D:\数据集\电动车\0.jpg + + Unknown + + + 500 + 500 + 3 + + 0 + + 电动车 + Unspecified + 0 + 0 + + 9 + 70 + 492 + 439 + + + + 电动车 + Unspecified + 0 + 0 + + 9 + 70 + 492 + 439 + + + + 电动车 + Unspecified + 0 + 0 + + 9 + 70 + 492 + 439 + + + + 电动车 + Unspecified + 0 + 0 + + 9 + 70 + 492 + 439 + + + diff --git "a/p37_\345\244\247\345\233\276\347\211\207\346\224\276\345\244\247\347\274\251\345\260\217/main.py" "b/p37_\345\244\247\345\233\276\347\211\207\346\224\276\345\244\247\347\274\251\345\260\217/main.py" new file mode 100644 index 0000000..bf98b4c --- /dev/null +++ "b/p37_\345\244\247\345\233\276\347\211\207\346\224\276\345\244\247\347\274\251\345\260\217/main.py" @@ -0,0 +1,308 @@ +import os.path +import sys + +from PyQt5.QtCore import Qt, QRectF, QPoint, QPointF, pyqtSignal, QEvent, QSize +from PyQt5.QtGui import QImage, QPixmap, QPainterPath, QMouseEvent, QPainter, QPen +from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QFileDialog, QSizePolicy +from PyQt5.QtWidgets import QApplication + +class QtImageViewer(QGraphicsView): + + # 定义使用到的鼠标操作信号 + leftMouseButtonPressed = pyqtSignal(float, float) + leftMouseButtonReleased = pyqtSignal(float, float) + middleMouseButtonPressed = pyqtSignal(float, float) + middleMouseButtonReleased = pyqtSignal(float, float) + rightMouseButtonPressed = pyqtSignal(float, float) + rightMouseButtonReleased = pyqtSignal(float, float) + leftMouseButtonDoubleClicked = pyqtSignal(float, float) + rightMouseButtonDoubleClicked = pyqtSignal(float, float) + viewChanged = pyqtSignal() + mousePositionOnImageChanged = pyqtSignal(QPoint) + roiSelected = pyqtSignal(int) + + def __init__(self): + QGraphicsView.__init__(self) + + self.scene = QGraphicsScene() + self.setScene(self.scene) + + # 显示的图片 + self._image = None + self.aspectRatioMode = Qt.AspectRatioMode.KeepAspectRatio + + # 默认没有滚动条 + self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) + self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) + + # 左键区域放大 + self.regionZoomButton = Qt.MouseButton.LeftButton + # 右键缩小 + self.zoomOutButton = Qt.MouseButton.RightButton + # 中键拖动 + self.panButton = Qt.MouseButton.MiddleButton + # 放大或缩小的因子 + self.wheelZoomFactor = 1.25 + + self.zoomStack = [] + + # Flags for active zooming/panning. + self._isZooming = False + self._isPanning = False + + self._pixelPosition = QPoint() + self._scenePosition = QPointF() + + self.ROIs = [] + + self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) + + def hasImage(self): + return self._image is not None + + def setImage(self, image): + + # 同时支持 QPixmap 和 QImage + if type(image) is QPixmap: + pixmap = image + elif type(image) is QImage: + pixmap = QPixmap.fromImage(image) + else: + raise RuntimeError("ImageViewer.setImage: Argument must be a QImage, QPixmap.") + if self.hasImage(): + self._image.setPixmap(pixmap) + else: + self._image = self.scene.addPixmap(pixmap) + + self.setSceneRect(QRectF(pixmap.rect())) # Set scene size to image size. + self.updateViewer() + + def open(self, filepath=None): + + # 不带参数,默认打开文件选择器选择文件 + if filepath is None: + filepath, dummy = QFileDialog.getOpenFileName(self, "Open image file.") + if len(filepath) and os.path.isfile(filepath): + image = QImage(filepath) + self.setImage(image) + + def updateViewer(self): + + if not self.hasImage(): + return + if len(self.zoomStack): + self.fitInView(self.zoomStack[-1], self.aspectRatioMode) + else: + self.fitInView(self.sceneRect(), self.aspectRatioMode) + + def clearZoom(self): + if len(self.zoomStack) > 0: + self.zoomStack = [] + self.updateViewer() + self.viewChanged.emit() + + def resizeEvent(self, event): + # 事件响应 + self.updateViewer() + + def mousePressEvent(self, event): + dummyModifiers = Qt.KeyboardModifier(Qt.KeyboardModifier.ShiftModifier | Qt.KeyboardModifier.ControlModifier + | Qt.KeyboardModifier.AltModifier | Qt.KeyboardModifier.MetaModifier) + if event.modifiers() == dummyModifiers: + QGraphicsView.mousePressEvent(self, event) + event.accept() + return + + if (self.regionZoomButton is not None) and (event.button() == self.regionZoomButton): + self._pixelPosition = event.pos() + self.setDragMode(QGraphicsView.DragMode.RubberBandDrag) + QGraphicsView.mousePressEvent(self, event) + event.accept() + self._isZooming = True + return + + if (self.zoomOutButton is not None) and (event.button() == self.zoomOutButton): + if len(self.zoomStack): + self.zoomStack.pop() + self.updateViewer() + self.viewChanged.emit() + event.accept() + return + + if (self.panButton is not None) and (event.button() == self.panButton): + self._pixelPosition = event.pos() # store pixel position + self.setDragMode(QGraphicsView.DragMode.ScrollHandDrag) + if self.panButton == Qt.MouseButton.LeftButton: + QGraphicsView.mousePressEvent(self, event) + else: + self.viewport().setCursor(Qt.CursorShape.ClosedHandCursor) + dummyModifiers = Qt.KeyboardModifier(Qt.KeyboardModifier.ShiftModifier + | Qt.KeyboardModifier.ControlModifier + | Qt.KeyboardModifier.AltModifier + | Qt.KeyboardModifier.MetaModifier) + dummyEvent = QMouseEvent(QEvent.Type.MouseButtonPress, QPointF(event.pos()), Qt.MouseButton.LeftButton, + event.buttons(), dummyModifiers) + self.mousePressEvent(dummyEvent) + sceneViewport = self.mapToScene(self.viewport().rect()).boundingRect().intersected(self.sceneRect()) + self._scenePosition = sceneViewport.topLeft() + event.accept() + self._isPanning = True + return + + scenePos = self.mapToScene(event.pos()) + if event.button() == Qt.MouseButton.LeftButton: + self.leftMouseButtonPressed.emit(scenePos.x(), scenePos.y()) + elif event.button() == Qt.MouseButton.MiddleButton: + self.middleMouseButtonPressed.emit(scenePos.x(), scenePos.y()) + elif event.button() == Qt.MouseButton.RightButton: + self.rightMouseButtonPressed.emit(scenePos.x(), scenePos.y()) + + QGraphicsView.mousePressEvent(self, event) + + def mouseReleaseEvent(self, event): + dummyModifiers = Qt.KeyboardModifier(Qt.KeyboardModifier.ShiftModifier | Qt.KeyboardModifier.ControlModifier + | Qt.KeyboardModifier.AltModifier | Qt.KeyboardModifier.MetaModifier) + if event.modifiers() == dummyModifiers: + QGraphicsView.mouseReleaseEvent(self, event) + event.accept() + return + + if (self.regionZoomButton is not None) and (event.button() == self.regionZoomButton): + QGraphicsView.mouseReleaseEvent(self, event) + zoomRect = self.scene.selectionArea().boundingRect().intersected(self.sceneRect()) + self.scene.setSelectionArea(QPainterPath()) + self.setDragMode(QGraphicsView.DragMode.NoDrag) + zoomPixelWidth = abs(event.pos().x() - self._pixelPosition.x()) + zoomPixelHeight = abs(event.pos().y() - self._pixelPosition.y()) + if zoomPixelWidth > 3 and zoomPixelHeight > 3: + if zoomRect.isValid() and (zoomRect != self.sceneRect()): + self.zoomStack.append(zoomRect) + self.updateViewer() + self.viewChanged.emit() + event.accept() + self._isZooming = False + return + + if (self.panButton is not None) and (event.button() == self.panButton): + if self.panButton == Qt.MouseButton.LeftButton: + QGraphicsView.mouseReleaseEvent(self, event) + else: + self.viewport().setCursor(Qt.CursorShape.ArrowCursor) + dummyModifiers = Qt.KeyboardModifier(Qt.KeyboardModifier.ShiftModifier + | Qt.KeyboardModifier.ControlModifier + | Qt.KeyboardModifier.AltModifier + | Qt.KeyboardModifier.MetaModifier) + dummyEvent = QMouseEvent(QEvent.Type.MouseButtonRelease, QPointF(event.pos()), + Qt.MouseButton.LeftButton, event.buttons(), dummyModifiers) + self.mouseReleaseEvent(dummyEvent) + self.setDragMode(QGraphicsView.DragMode.NoDrag) + if len(self.zoomStack) > 0: + sceneViewport = self.mapToScene(self.viewport().rect()).boundingRect().intersected(self.sceneRect()) + delta = sceneViewport.topLeft() - self._scenePosition + self.zoomStack[-1].translate(delta) + self.zoomStack[-1] = self.zoomStack[-1].intersected(self.sceneRect()) + self.viewChanged.emit() + event.accept() + self._isPanning = False + return + + scenePos = self.mapToScene(event.pos()) + if event.button() == Qt.MouseButton.LeftButton: + self.leftMouseButtonReleased.emit(scenePos.x(), scenePos.y()) + elif event.button() == Qt.MouseButton.MiddleButton: + self.middleMouseButtonReleased.emit(scenePos.x(), scenePos.y()) + elif event.button() == Qt.MouseButton.RightButton: + self.rightMouseButtonReleased.emit(scenePos.x(), scenePos.y()) + + QGraphicsView.mouseReleaseEvent(self, event) + + def mouseDoubleClickEvent(self, event): + if (self.zoomOutButton is not None) and (event.button() == self.zoomOutButton): + self.clearZoom() + event.accept() + return + + scenePos = self.mapToScene(event.pos()) + if event.button() == Qt.MouseButton.LeftButton: + self.leftMouseButtonDoubleClicked.emit(scenePos.x(), scenePos.y()) + elif event.button() == Qt.MouseButton.RightButton: + self.rightMouseButtonDoubleClicked.emit(scenePos.x(), scenePos.y()) + + QGraphicsView.mouseDoubleClickEvent(self, event) + + def wheelEvent(self, event): + if self.wheelZoomFactor is not None: + if self.wheelZoomFactor == 1: + return + if event.angleDelta().y() < 0: + # zoom in + if len(self.zoomStack) == 0: + self.zoomStack.append(self.sceneRect()) + elif len(self.zoomStack) > 1: + del self.zoomStack[:-1] + zoomRect = self.zoomStack[-1] + center = zoomRect.center() + zoomRect.setWidth(zoomRect.width() / self.wheelZoomFactor) + zoomRect.setHeight(zoomRect.height() / self.wheelZoomFactor) + zoomRect.moveCenter(center) + self.zoomStack[-1] = zoomRect.intersected(self.sceneRect()) + self.updateViewer() + self.viewChanged.emit() + else: + if len(self.zoomStack) == 0: + return + + if len(self.zoomStack) > 1: + del self.zoomStack[:-1] + zoomRect = self.zoomStack[-1] + center = zoomRect.center() + zoomRect.setWidth(zoomRect.width() * self.wheelZoomFactor) + zoomRect.setHeight(zoomRect.height() * self.wheelZoomFactor) + zoomRect.moveCenter(center) + self.zoomStack[-1] = zoomRect.intersected(self.sceneRect()) + if self.zoomStack[-1] == self.sceneRect(): + self.zoomStack = [] + self.updateViewer() + self.viewChanged.emit() + event.accept() + return + + QGraphicsView.wheelEvent(self, event) + + def mouseMoveEvent(self, event): + if self._isPanning: + QGraphicsView.mouseMoveEvent(self, event) + if len(self.zoomStack) > 0: + sceneViewport = self.mapToScene(self.viewport().rect()).boundingRect().intersected(self.sceneRect()) + delta = sceneViewport.topLeft() - self._scenePosition + self._scenePosition = sceneViewport.topLeft() + self.zoomStack[-1].translate(delta) + self.zoomStack[-1] = self.zoomStack[-1].intersected(self.sceneRect()) + self.updateViewer() + self.viewChanged.emit() + + scenePos = self.mapToScene(event.pos()) + if self.sceneRect().contains(scenePos): + x = int(round(scenePos.x() - 0.5)) + y = int(round(scenePos.y() - 0.5)) + imagePos = QPoint(x, y) + else: + imagePos = QPoint(-1, -1) + self.mousePositionOnImageChanged.emit(imagePos) + + QGraphicsView.mouseMoveEvent(self, event) + + def enterEvent(self, event): + self.setCursor(Qt.CursorShape.CrossCursor) + + def leaveEvent(self, event): + self.setCursor(Qt.CursorShape.ArrowCursor) + +if __name__ == '__main__': + + app = QApplication(sys.argv) + viewer = QtImageViewer() + # viewer.open(filepath='test.jpg') + viewer.open() + viewer.show() + sys.exit(app.exec()) \ No newline at end of file diff --git "a/p38_\345\244\232\347\252\227\345\217\243/main_window.py" "b/p38_\345\244\232\347\252\227\345\217\243/main_window.py" new file mode 100644 index 0000000..158fc15 --- /dev/null +++ "b/p38_\345\244\232\347\252\227\345\217\243/main_window.py" @@ -0,0 +1,25 @@ +import sys + +from PyQt5.QtWidgets import QMainWindow, QApplication +from ui_mainwindow import Ui_MainWindow +from second_window import SecondWindow + + +class MainWindow(QMainWindow, Ui_MainWindow): + + def __init__(self, parent=None): + super(MainWindow, self).__init__(parent) + self.setupUi(self) + + self.pushButton.clicked.connect(self.showSecondWindow) + + def showSecondWindow(self): + self.secWin = SecondWindow("Hello SecondWindow.") + self.secWin.show() + + +if __name__ == '__main__': + app = QApplication(sys.argv) + windows = MainWindow() + windows.show() + sys.exit(app.exec_()) diff --git "a/p38_\345\244\232\347\252\227\345\217\243/mainwindow.ui" "b/p38_\345\244\232\347\252\227\345\217\243/mainwindow.ui" new file mode 100644 index 0000000..c13c9bf --- /dev/null +++ "b/p38_\345\244\232\347\252\227\345\217\243/mainwindow.ui" @@ -0,0 +1,50 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + 多窗口示例 + + + + + + 310 + 210 + 171 + 91 + + + + + 12 + + + + 开启第二个窗口 + + + + + + + 0 + 0 + 800 + 26 + + + + + + + + diff --git "a/p38_\345\244\232\347\252\227\345\217\243/second_window.py" "b/p38_\345\244\232\347\252\227\345\217\243/second_window.py" new file mode 100644 index 0000000..284b161 --- /dev/null +++ "b/p38_\345\244\232\347\252\227\345\217\243/second_window.py" @@ -0,0 +1,12 @@ +from PyQt5.QtWidgets import QMainWindow +from ui_secondwindow import Ui_MainWindow + + +class SecondWindow(QMainWindow, Ui_MainWindow): + + def __init__(self, param, parent=None): + super(SecondWindow, self).__init__(parent) + self.setupUi(self) + + print("从主窗口传递过来的字符是:{}".format(param)) + self.label.setText(param) \ No newline at end of file diff --git "a/p38_\345\244\232\347\252\227\345\217\243/secondwindow.ui" "b/p38_\345\244\232\347\252\227\345\217\243/secondwindow.ui" new file mode 100644 index 0000000..faeaaa6 --- /dev/null +++ "b/p38_\345\244\232\347\252\227\345\217\243/secondwindow.ui" @@ -0,0 +1,50 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + 第二个窗口 + + + + + + 170 + 190 + 501 + 91 + + + + + 14 + + + + 欢迎访问我的博客,https://xugaoxiang.com + + + + + + + 0 + 0 + 800 + 26 + + + + + + + + diff --git "a/p38_\345\244\232\347\252\227\345\217\243/ui_mainwindow.py" "b/p38_\345\244\232\347\252\227\345\217\243/ui_mainwindow.py" new file mode 100644 index 0000000..93bb6c3 --- /dev/null +++ "b/p38_\345\244\232\347\252\227\345\217\243/ui_mainwindow.py" @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- + +# Form implementation generated from reading ui file '.\mainwindow.ui' +# +# Created by: PyQt5 UI code generator 5.9.2 +# +# WARNING! All changes made in this file will be lost! + +from PyQt5 import QtCore, QtGui, QtWidgets + +class Ui_MainWindow(object): + def setupUi(self, MainWindow): + MainWindow.setObjectName("MainWindow") + MainWindow.resize(800, 600) + self.centralwidget = QtWidgets.QWidget(MainWindow) + self.centralwidget.setObjectName("centralwidget") + self.pushButton = QtWidgets.QPushButton(self.centralwidget) + self.pushButton.setGeometry(QtCore.QRect(310, 210, 171, 91)) + font = QtGui.QFont() + font.setPointSize(12) + self.pushButton.setFont(font) + self.pushButton.setObjectName("pushButton") + MainWindow.setCentralWidget(self.centralwidget) + self.menubar = QtWidgets.QMenuBar(MainWindow) + self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) + self.menubar.setObjectName("menubar") + MainWindow.setMenuBar(self.menubar) + self.statusbar = QtWidgets.QStatusBar(MainWindow) + self.statusbar.setObjectName("statusbar") + MainWindow.setStatusBar(self.statusbar) + + self.retranslateUi(MainWindow) + QtCore.QMetaObject.connectSlotsByName(MainWindow) + + def retranslateUi(self, MainWindow): + _translate = QtCore.QCoreApplication.translate + MainWindow.setWindowTitle(_translate("MainWindow", "多窗口示例")) + self.pushButton.setText(_translate("MainWindow", "开启第二个窗口")) + diff --git "a/p38_\345\244\232\347\252\227\345\217\243/ui_secondwindow.py" "b/p38_\345\244\232\347\252\227\345\217\243/ui_secondwindow.py" new file mode 100644 index 0000000..b203dba --- /dev/null +++ "b/p38_\345\244\232\347\252\227\345\217\243/ui_secondwindow.py" @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- + +# Form implementation generated from reading ui file '.\secondwindow.ui' +# +# Created by: PyQt5 UI code generator 5.9.2 +# +# WARNING! All changes made in this file will be lost! + +from PyQt5 import QtCore, QtGui, QtWidgets + +class Ui_MainWindow(object): + def setupUi(self, MainWindow): + MainWindow.setObjectName("MainWindow") + MainWindow.resize(800, 600) + self.centralwidget = QtWidgets.QWidget(MainWindow) + self.centralwidget.setObjectName("centralwidget") + self.label = QtWidgets.QLabel(self.centralwidget) + self.label.setGeometry(QtCore.QRect(170, 190, 501, 91)) + font = QtGui.QFont() + font.setPointSize(14) + self.label.setFont(font) + self.label.setObjectName("label") + MainWindow.setCentralWidget(self.centralwidget) + self.menubar = QtWidgets.QMenuBar(MainWindow) + self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) + self.menubar.setObjectName("menubar") + MainWindow.setMenuBar(self.menubar) + self.statusbar = QtWidgets.QStatusBar(MainWindow) + self.statusbar.setObjectName("statusbar") + MainWindow.setStatusBar(self.statusbar) + + self.retranslateUi(MainWindow) + QtCore.QMetaObject.connectSlotsByName(MainWindow) + + def retranslateUi(self, MainWindow): + _translate = QtCore.QCoreApplication.translate + MainWindow.setWindowTitle(_translate("MainWindow", "第二个窗口")) + self.label.setText(_translate("MainWindow", "欢迎访问我的博客,https://xugaoxiang.com")) + diff --git a/p39_QTextToSpeech/main_window.py b/p39_QTextToSpeech/main_window.py new file mode 100644 index 0000000..ffbbf6a --- /dev/null +++ b/p39_QTextToSpeech/main_window.py @@ -0,0 +1,52 @@ +import sys + +from PyQt5 import QtCore, QtGui, QtWidgets +from PyQt5.QtTextToSpeech import QTextToSpeech +from PyQt5.QtWidgets import QMainWindow, QApplication + +from ui_mainwindow import Ui_MainWindow + + +class MainWindow(QMainWindow, Ui_MainWindow): + + def __init__(self, parent=None): + super(MainWindow, self).__init__(parent) + self.setupUi(self) + + self.pushButton.clicked.connect(self.play) + + self.engine = None + engineNames = QTextToSpeech.availableEngines() + print('engine names: {}'.format(engineNames)) + + if len(engineNames) > 0: + engineName = engineNames[0] + self.engine = QTextToSpeech(engineName) + self.engine.stateChanged.connect(self.stateChanged) + + self.voices = [] + + for voice in self.engine.availableVoices(): + print('voice: {}'.format(voice)) + self.voices.append(voice) + self.comboBox.addItem(voice.name()) + + else: + self.pushButton.setEnabled(False) + + def play(self): + self.pushButton.setEnabled(False) + self.engine.setVoice(self.voices[self.comboBox.currentIndex()]) + # self.engine.setVolume(float(self.horizontalSlider.value() / 100)) + self.engine.say(self.lineEdit.text()) + + def stateChanged(self, state): + if (state == QTextToSpeech.State.Ready): + self.pushButton.setEnabled(True) + + +if __name__ == '__main__': + app = QApplication(sys.argv) + windows = MainWindow() + windows.show() + sys.exit(app.exec_()) diff --git a/p39_QTextToSpeech/mainwindow.ui b/p39_QTextToSpeech/mainwindow.ui new file mode 100644 index 0000000..f6129c3 --- /dev/null +++ b/p39_QTextToSpeech/mainwindow.ui @@ -0,0 +1,109 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + QtTextToSpeech示例 + + + + + + 140 + 140 + 501 + 151 + + + + + + + 0 + + + + + + 20 + + + + 选择声音 + + + + + + + + 20 + + + + + + + + + + + + + 20 + + + + 输入文本 + + + + + + + + 20 + + + + + + + + + 20 + + + + 播放语音 + + + + + + + + + + + + 0 + 0 + 800 + 23 + + + + + + + + diff --git a/p39_QTextToSpeech/ui_mainwindow.py b/p39_QTextToSpeech/ui_mainwindow.py new file mode 100644 index 0000000..fcb6556 --- /dev/null +++ b/p39_QTextToSpeech/ui_mainwindow.py @@ -0,0 +1,85 @@ +# -*- coding: utf-8 -*- + +# Form implementation generated from reading ui file '.\mainwindow.ui' +# +# Created by: PyQt5 UI code generator 5.15.9 +# +# WARNING: Any manual changes made to this file will be lost when pyuic5 is +# run again. Do not edit this file unless you know what you are doing. + + +from PyQt5 import QtCore, QtGui, QtWidgets + + +class Ui_MainWindow(object): + def setupUi(self, MainWindow): + MainWindow.setObjectName("MainWindow") + MainWindow.resize(800, 600) + self.centralwidget = QtWidgets.QWidget(MainWindow) + self.centralwidget.setObjectName("centralwidget") + self.widget = QtWidgets.QWidget(self.centralwidget) + self.widget.setGeometry(QtCore.QRect(140, 140, 501, 151)) + self.widget.setObjectName("widget") + self.verticalLayout = QtWidgets.QVBoxLayout(self.widget) + self.verticalLayout.setContentsMargins(0, 0, 0, 0) + self.verticalLayout.setObjectName("verticalLayout") + self.horizontalLayout = QtWidgets.QHBoxLayout() + self.horizontalLayout.setContentsMargins(-1, -1, 0, -1) + self.horizontalLayout.setObjectName("horizontalLayout") + self.label_voice = QtWidgets.QLabel(self.widget) + font = QtGui.QFont() + font.setPointSize(20) + self.label_voice.setFont(font) + self.label_voice.setObjectName("label_voice") + self.horizontalLayout.addWidget(self.label_voice) + self.comboBox = QtWidgets.QComboBox(self.widget) + font = QtGui.QFont() + font.setPointSize(20) + self.comboBox.setFont(font) + self.comboBox.setObjectName("comboBox") + self.horizontalLayout.addWidget(self.comboBox) + self.horizontalLayout.setStretch(0, 1) + self.horizontalLayout.setStretch(1, 3) + self.verticalLayout.addLayout(self.horizontalLayout) + self.horizontalLayout_2 = QtWidgets.QHBoxLayout() + self.horizontalLayout_2.setObjectName("horizontalLayout_2") + self.label = QtWidgets.QLabel(self.widget) + font = QtGui.QFont() + font.setPointSize(20) + self.label.setFont(font) + self.label.setObjectName("label") + self.horizontalLayout_2.addWidget(self.label) + self.lineEdit = QtWidgets.QLineEdit(self.widget) + font = QtGui.QFont() + font.setPointSize(20) + self.lineEdit.setFont(font) + self.lineEdit.setObjectName("lineEdit") + self.horizontalLayout_2.addWidget(self.lineEdit) + self.pushButton = QtWidgets.QPushButton(self.widget) + font = QtGui.QFont() + font.setPointSize(20) + self.pushButton.setFont(font) + self.pushButton.setObjectName("pushButton") + self.horizontalLayout_2.addWidget(self.pushButton) + self.horizontalLayout_2.setStretch(0, 1) + self.horizontalLayout_2.setStretch(1, 2) + self.horizontalLayout_2.setStretch(2, 1) + self.verticalLayout.addLayout(self.horizontalLayout_2) + MainWindow.setCentralWidget(self.centralwidget) + self.menubar = QtWidgets.QMenuBar(MainWindow) + self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) + self.menubar.setObjectName("menubar") + MainWindow.setMenuBar(self.menubar) + self.statusbar = QtWidgets.QStatusBar(MainWindow) + self.statusbar.setObjectName("statusbar") + MainWindow.setStatusBar(self.statusbar) + + self.retranslateUi(MainWindow) + QtCore.QMetaObject.connectSlotsByName(MainWindow) + + def retranslateUi(self, MainWindow): + _translate = QtCore.QCoreApplication.translate + MainWindow.setWindowTitle(_translate("MainWindow", "QtTextToSpeech示例")) + self.label_voice.setText(_translate("MainWindow", "选择声音")) + self.label.setText(_translate("MainWindow", "输入文本")) + self.pushButton.setText(_translate("MainWindow", "播放语音"))