PyQt5 GUI之用鼠标画图
先上题目
第一眼看到题目还觉得挺简单的
直到老师给了下面的提示
…
对不起
我太年轻了
原地昏迷
老师上课给了一个范本,是对图片进行打开翻转等操作的窗口,右边的日志挺好玩的但是我跟着写写不出来…
长这样↓
附一个翻转猫咪的代码(就是猫咪图要自己找了 还有菜单栏里的icon图标都要自己改一下哟
import os
import platform
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class myMainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.image = QImage()
self.dirty = False
self.filename = None
self.mirroredvertically = False
self.mirroredhorizontally = False
#图像
self.imageLabel = QLabel()
self.imageLabel.setAlignment(Qt.AlignCenter)
self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)
self.setCentralWidget(self.imageLabel)
#右侧停靠窗口
logDockWidget = QDockWidget("Log", self)
logDockWidget.setAllowedAreas(Qt.LeftDockWidgetArea|Qt.RightDockWidgetArea)
self.listWidget = QListWidget()
logDockWidget.setWidget(self.listWidget)
self.addDockWidget(Qt.RightDockWidgetArea, logDockWidget)
#状态栏
self.sizeLabel = QLabel()
self.sizeLabel.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken)
status = self.statusBar()
status.setSizeGripEnabled(False)
status.addPermanentWidget(self.sizeLabel)
status.showMessage("Ready", 5000)
#各种动作
self.fileOpenAction = QAction(QIcon("images/fileopen.png"), "&Open", self)
self.fileOpenAction.setShortcut(QKeySequence.Open)
self.fileOpenAction.setToolTip("Open an image.")
self.fileOpenAction.setStatusTip("Open an image.")
self.fileOpenAction.triggered.connect(self.fileOpen)
self.fileSaveAction = QAction(QIcon("images/filesave.png"), "&Save", self)
self.fileSaveAction.setShortcut(QKeySequence.Save)
self.fileSaveAction.setToolTip("Save an image.")
self.fileSaveAction.setStatusTip("Save an image.")
self.fileSaveAction.triggered.connect(self.fileSaveAs)
self.editUnMirrorAction = QAction(QIcon("images/editunmirror.png"), "&Unmirror", self)
self.editUnMirrorAction.setShortcut("Ctrl+U")
self.editUnMirrorAction.setToolTip("Unmirror the image")
self.editUnMirrorAction.setStatusTip("Unmirror the image")
self.editUnMirrorAction.setCheckable(True)
self.editUnMirrorAction.setChecked(True)
self.editUnMirrorAction.toggled.connect(self.editUnMirror)
editMirrorHorizontalAction = QAction(QIcon("images/editmirrorhoriz.png"), "Mirror &Horizontally", self)
editMirrorHorizontalAction.setShortcut("Ctrl+H")
editMirrorHorizontalAction.setToolTip("Horizontally mirror the image")
editMirrorHorizontalAction.setStatusTip("Horizontally mirror the image")
editMirrorHorizontalAction.setCheckable(True)
editMirrorHorizontalAction.toggled.connect(self.editMirrorHorizontal)
editMirrorVerticalAction = QAction(QIcon("images/editmirrorvert.png"), "Mirror &Vertically", self)
editMirrorVerticalAction.setShortcut("Ctrl+V")
editMirrorVerticalAction.setToolTip("Vertically mirror the image")
editMirrorVerticalAction.setStatusTip("Vertically mirror the image")
editMirrorVerticalAction.setCheckable(True)
editMirrorVerticalAction.toggled.connect(self.editMirrorVertical)
mirrorGroup = QActionGroup(self)
mirrorGroup.addAction(self.editUnMirrorAction)
mirrorGroup.addAction(editMirrorHorizontalAction)
mirrorGroup.addAction(editMirrorVerticalAction)
#菜单栏
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.fileOpenAction)
self.fileMenu.addAction(self.fileSaveAction)
editMenu = self.menuBar().addMenu("&Edit")
editMenu.addAction(self.editUnMirrorAction)
editMenu.addAction(editMirrorHorizontalAction)
editMenu.addAction(editMirrorVerticalAction)
#工具栏
fileToolbar = self.addToolBar("File")
fileToolbar.addAction(self.fileOpenAction)
fileToolbar.addAction(self.fileSaveAction)
editToolbar = self.addToolBar("Edit")
editToolbar.addAction(self.editUnMirrorAction)
editToolbar.addAction(editMirrorHorizontalAction)
editToolbar.addAction(editMirrorVerticalAction)
self.recentFiles = []
self.setWindowTitle("Image Changer")
def okToContinue(self): #警告当前图像未保存
if self.dirty:
reply = QMessageBox.question(self,
"Image Changer - Unsaved Changes",
"Save unsaved changes?",
QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel)
if reply == QMessageBox.Cancel:
return False
elif reply == QMessageBox.Yes:
return self.fileSaveAs()
return True
def fileOpen(self):
if not self.okToContinue():
return
dir = (os.path.dirname(self.filename)
if self.filename is not None else ".")
formats = (["*.{}".format(format.data().decode(