본문 바로가기
Beginning PyQt

Dialog Classes

by 어린왕자1234 2022. 5. 21.

The QFileDialog Class

file_name, ok = QFileDialog.getOpenFileName(
    self, 
    "Open  File",
    "/Users/user_name/Desktop/",
    "Image  Files  (*.png  *.jpg  *.bmp)"
)

 

file_name, ok = QFileDialog.getSaveFileName(
    self, 
    "Save File",
    "/Users/user_name/Desktop/",
    "Text   Files   (*.txt)"
)

 

file_name, ok = QFileDialog.getOpenFileName(
    self, 
    "Open  File",
    "/Users/user_name/Desktop/",
    "Image  Files  (*.png  *.jpg  *.bmp)",
    options   =   QFileDialog.Option.DontUseNativeDialog
)

 

QInputDialog Class

find_text,  ok  =  QInputDialog.getText(
    self, 
    "Search Text",
    "Find:"
)

 

QInputDialog methods:

       getMultiLineText()   Method to get a multiline string from the user

       getInt() Method to get an integer from the user

       getDouble()   Method to get a floating-point number from the user

       getItem() Method to let the user select an item from a list of strings

 

QFontDialog Class

font, ok = QFontDialog.getFont()

 

font, ok = QFontDialog.getFont( 
    QFont("Helvetica",  10), 
    self
)

 

QColorDialog Class

color  =  QColorDialog.getColor()

 

QMessageBox

QMessageBox.about(
    self, 
    "About Notepad",
    """<p>Beginner's  Practical  Guide  to  PyQt</p><p>Project 5.1 - Notepad GUI</p>"""
)

 

Rich Text Notepad GUI

 

#   richtext_notepad.py

# Import necessary modules 
import sys
from PyQt6.QtWidgets import (QApplication, QMainWindow, 
    QMessageBox, QTextEdit, QFileDialog, QInputDialog, 
    QFontDialog, QColorDialog)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon, QTextCursor, QColor, QAction

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initializeUI()

    def initializeUI(self):
        """Set up the application's GUI."""
        self.setMinimumSize(400, 500)
        self.setWindowTitle("5.1 – Rich Text Notepad GUI")

        self.setUpMainWindow()
        self.createActions()
        self.createMenu()
        self.show()

    def setUpMainWindow(self):
        """Create and arrange widgets in the main window."""
        self.text_edit = QTextEdit()
        self.text_edit.textChanged.connect(self.removeHighlights)
        self.setCentralWidget(self.text_edit)

    def createActions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.new_act = QAction(QIcon("images/new_file.png"), "New")
        self.new_act.setShortcut("Ctrl+N")
        self.new_act.triggered.connect(self.clearText)

        self.open_act = QAction(QIcon("images/open_file.png"), "Open")
        self.open_act.setShortcut("Ctrl+O")
        self.open_act.triggered.connect(self.openFile)

        self.save_act = QAction(QIcon("images/save_file.png"), "Save")
        self.save_act.setShortcut("Ctrl+S")
        self.save_act.triggered.connect(self.saveToFile)

        self.quit_act = QAction(QIcon("images/exit.png"), "Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.triggered.connect(self.close)

        # Create actions for Edit menu
        self.undo_act = QAction(QIcon("images/undo.png"), "Undo")
        self.undo_act.setShortcut("Ctrl+Z")
        self.undo_act.triggered.connect(self.text_edit.undo)

        self.redo_act = QAction(QIcon("images/redo.png"), "Redo")
        self.redo_act.setShortcut("Ctrl+Shift+Z")
        self.redo_act.triggered.connect(self.text_edit.redo)

        self.cut_act = QAction(QIcon("images/cut.png"), "Cut")
        self.cut_act.setShortcut("Ctrl+X")
        self.cut_act.triggered.connect(self.text_edit.cut)

        self.copy_act = QAction(QIcon("images/copy.png"), "Copy")
        self.copy_act.setShortcut("Ctrl+C")
        self.copy_act.triggered.connect(self.text_edit.copy)

        self.paste_act = QAction(QIcon("images/paste.png"), "Paste")
        self.paste_act.setShortcut("Ctrl+V")
        self.paste_act.triggered.connect(self.text_edit.paste)

        self.find_act = QAction(QIcon("images/find.png"), "Find All")
        self.find_act.setShortcut("Ctrl+F")
        self.find_act.triggered.connect(self.searchText)

        # Create actions for Tools menu
        self.font_act = QAction(QIcon("images/font.png"), "Font")
        self.font_act.setShortcut("Ctrl+T")
        self.font_act.triggered.connect(self.chooseFont)

        self.color_act = QAction(QIcon("images/color.png"), "Color")
        self.color_act.setShortcut("Ctrl+Shift+C")
        self.color_act.triggered.connect(self.chooseFontColor)

        self.highlight_act = QAction(QIcon("images/highlight.png"), "Highlight")
        self.highlight_act.setShortcut("Ctrl+Shift+H")
        self.highlight_act.triggered.connect(self.chooseFontBackgroundColor)

        # Create actions for Help menu
        self.about_act = QAction("About")
        self.about_act.triggered.connect(self.aboutDialog)

    def createMenu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)

        # Create File menu and add actions 
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.new_act)
        file_menu.addSeparator()
        file_menu.addAction(self.open_act)
        file_menu.addAction(self.save_act)
        file_menu.addSeparator()
        file_menu.addAction(self.quit_act)

        # Create Edit menu and add actions 
        edit_menu = self.menuBar().addMenu("Edit")
        edit_menu.addAction(self.undo_act)
        edit_menu.addAction(self.redo_act) 
        edit_menu.addSeparator()       
        edit_menu.addAction(self.cut_act) 
        edit_menu.addAction(self.copy_act) 
        edit_menu.addAction(self.paste_act) 
        edit_menu.addSeparator()
        edit_menu.addAction(self.find_act)

        # Create Tools menu and add actions 
        tool_menu = self.menuBar().addMenu("Tools")
        tool_menu.addAction(self.font_act)
        tool_menu.addAction(self.color_act)
        tool_menu.addAction(self.highlight_act)

        # Create Help menu and add actions 
        help_menu = self.menuBar().addMenu("Help")
        help_menu.addAction(self.about_act)

    def clearText(self):
        """Clear the QTextEdit field."""
        answer = QMessageBox.question(self, "Clear Text", 
            "Do you want to clear the text?", 
            QMessageBox.StandardButton.No | \
            QMessageBox.StandardButton.Yes,
            QMessageBox.StandardButton.Yes)
        if answer == QMessageBox.StandardButton.Yes:
            self.text_edit.clear()

    def openFile(self):
        """Open a text or html file and display its contents in
        the text edit field."""
        file_name, _ = QFileDialog.getOpenFileName(
            self, "Open File", "", 
            "HTML Files (*.html);;Text Files (*.txt)")

        if file_name:
            with open(file_name, "r") as f:
                notepad_text = f.read()
            self.text_edit.setText(notepad_text)

    def saveToFile(self):
        """If the save button is clicked, display dialog asking 
        user if they want to save the text in the text edit 
        field to a text file.""" 
        file_name, _ = QFileDialog.getSaveFileName(self, "Save File",
            "","HTML Files (*.html);;Text Files (*.txt)")

        if file_name.endswith(".txt"):
            notepad_text = self.text_edit.toPlainText()
            with open(file_name, "w") as f:            
                f.write(notepad_text)
        elif file_name.endswith(".html"):
            notepad_richtext = self.text_edit.toHtml()
            with open(file_name, "w") as f:            
                f.write(notepad_richtext)
        else:
            QMessageBox.information(
                self, "Not Saved", "Text not saved.", 
                QMessageBox.StandardButton.Ok)

    def searchText(self):
        """Search for text."""
        # Display input dialog to ask user for text to find
        find_text, ok = QInputDialog.getText(
            self, "Search Text", "Find:")

        if ok:
            extra_selections = []
            # Set the cursor in the text edit field to the beginning
            self.text_edit.moveCursor(QTextCursor.MoveOperation.Start)
            color = QColor(Qt.GlobalColor.gray)

            while(self.text_edit.find(find_text)):
                # Use ExtraSelection() to mark the text you are
                # searching for as gray 
                selection = QTextEdit.ExtraSelection()  
                selection.format.setBackground(color)

                # Set the cursor of the selection                 
                selection.cursor = self.text_edit.textCursor()
                extra_selections.append(selection)

            # Highlight all selections in the QTextEdit widget
            self.text_edit.setExtraSelections(extra_selections)

    def removeHighlights(self):
        """Reset extra selections after editing text."""
        self.text_edit.setExtraSelections([])
            
    def chooseFont(self):
        """Select a font from the QFontDialog."""
        current = self.text_edit.currentFont()

        opt = QFontDialog.FontDialogOption.DontUseNativeDialog
        font, ok = QFontDialog.getFont(current, self, 
            options=opt)
        if ok:
            self.text_edit.setCurrentFont(font) 
                
    def chooseFontColor(self):
        """Select a font from the QColorDialog."""
        color = QColorDialog.getColor()
        if color.isValid():
            self.text_edit.setTextColor(color)

    def chooseFontBackgroundColor(self):
        """Select a color for text's background."""
        color = QColorDialog.getColor()
        if color.isValid():
            self.text_edit.setTextBackgroundColor(color)

    def aboutDialog(self):
        """Display the About dialog."""
        QMessageBox.about(self, "About Notepad", 
            """<p>Beginner's Practical Guide to PyQt</p>
            <p>Project 5.1 - Notepad GUI</p>""")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec())

 

# main_window_extras.py

# Import necessary modules
import sys
from PyQt6.QtWidgets import (QApplication, QMainWindow, 
    QWidget, QCheckBox, QTextEdit, QDockWidget, QToolBar, 
    QStatusBar, QVBoxLayout)
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QIcon, QAction

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initializeUI()

    def initializeUI(self):
        """Set up the application's GUI."""
        self.setMinimumSize(450, 350)
        self.setWindowTitle("Adding More Window Features")

        self.setUpMainWindow()
        self.createDockWidget()
        self.createActions()
        self.createMenu()
        self.createToolBar()
        self.show()

    def setUpMainWindow(self):
        """Create and arrange widgets in the main window."""
        # Create and set the central widget
        self.text_edit = QTextEdit()
        self.setCentralWidget(self.text_edit)

        # Create the status bar
        self.setStatusBar(QStatusBar())

    def createActions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.quit_act = QAction(QIcon("images/exit.png"), "Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.setStatusTip("Quit program")
        self.quit_act.triggered.connect(self.close)

        # Create actions for View menu
        self.full_screen_act = QAction("Full Screen", checkable=True)
        self.full_screen_act.setStatusTip("Switch to full screen mode")
        self.full_screen_act.triggered.connect(self.switchToFullScreen)

    def createMenu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)

        # Create File menu and add actions 
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.quit_act)

        # Create View menu, Appearance submenu and add actions 
        view_menu = self.menuBar().addMenu("View")
        appearance_submenu = view_menu.addMenu("Appearance")
        appearance_submenu.addAction(self.full_screen_act)

    def createToolBar(self):
        """Create the application's toolbar."""
        toolbar = QToolBar("Main Toolbar")
        toolbar.setIconSize(QSize(16, 16))
        self.addToolBar(toolbar)

        # Add actions to the toolbar
        toolbar.addAction(self.quit_act)

    def createDockWidget(self):
        """Create the application's dock widget."""
        dock_widget = QDockWidget()
        dock_widget.setWindowTitle("Formatting")
        dock_widget.setAllowedAreas(Qt.DockWidgetArea.AllDockWidgetAreas)
 
        # Create widget examples to add to the dock
        auto_bullet_cb = QCheckBox("Auto Bullet List")
        auto_bullet_cb.toggled.connect(self.changeTextEditSettings)

        # Create layout for dock widget
        dock_v_box = QVBoxLayout()
        dock_v_box.addWidget(auto_bullet_cb)
        dock_v_box.addStretch(1)

        # Create a QWidget that acts as a container to 
        # hold other widgets
        dock_container = QWidget()
        dock_container.setLayout(dock_v_box)

        # Set the main widget for the dock widget
        dock_widget.setWidget(dock_container)

        # Set initial location of dock widget in main window
        self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, dock_widget)

    def switchToFullScreen(self, state):
        """If state is True, then display the main window 
        in full screen. Otherwise, return the the window to normal."""
        if state: self.showFullScreen()
        else: self.showNormal()

    def changeTextEditSettings(self, checked):
        """Change formatting features for QTextEdit."""
        if checked:
            self.text_edit.setAutoFormatting(
                QTextEdit.AutoFormattingFlag.AutoBulletList)
        else:
            self.text_edit.setAutoFormatting(
                QTextEdit.AutoFormattingFlag.AutoNone)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec())

 

 

#  photo_editor.py

# Import necessary modules
import sys
from PyQt6.QtWidgets import (QApplication, QMainWindow, 
    QWidget, QLabel, QPushButton, QDockWidget, QDialog,
    QFileDialog, QMessageBox, QToolBar, QStatusBar,
    QVBoxLayout)
from PyQt6.QtCore import Qt, QSize, QRect
from PyQt6.QtGui import (QIcon, QAction, QPixmap, QTransform, 
    QPainter)
from PyQt6.QtPrintSupport import QPrinter, QPrintDialog

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initializeUI()

    def initializeUI(self):
        """Set up the application's GUI."""
        self.setFixedSize(650, 650)
        self.setWindowTitle("5.2 - Photo Editor GUI")

        self.setUpMainWindow()
        self.createToolsDockWidget()
        self.createActions()
        self.createMenu()
        self.createToolBar()
        self.show()

    def setUpMainWindow(self):
        """Create and arrange widgets in the main window."""
        self.image = QPixmap()

        self.image_label = QLabel()
        self.image_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.setCentralWidget(self.image_label)

        # Create the status bar
        self.setStatusBar(QStatusBar())

    def createActions(self):
        """Create the application's menu actions."""
        # Create actions for File menu
        self.open_act = QAction(QIcon("images/open_file.png"),"Open")
        self.open_act.setShortcut("Ctrl+O")
        self.open_act.setStatusTip("Open a new image")
        self.open_act.triggered.connect(self.openImage)

        self.save_act = QAction(QIcon("images/save_file.png"),"Save")
        self.save_act.setShortcut("Ctrl+S")
        self.save_act.setStatusTip("Save image")
        self.save_act.triggered.connect(self.saveImage)

        self.print_act = QAction(QIcon("images/print.png"), "Print")
        self.print_act.setShortcut("Ctrl+P")
        self.print_act.setStatusTip("Print image")
        self.print_act.triggered.connect(self.printImage)
        self.print_act.setEnabled(False)

        self.quit_act = QAction(QIcon("images/exit.png"), "Quit")
        self.quit_act.setShortcut("Ctrl+Q")
        self.quit_act.setStatusTip("Quit program")
        self.quit_act.triggered.connect(self.close)

        # Create actions for Edit menu
        self.rotate90_act = QAction("Rotate 90º")
        self.rotate90_act.setStatusTip('Rotate image 90º clockwise')
        self.rotate90_act.triggered.connect(self.rotateImage90)

        self.rotate180_act = QAction("Rotate 180º")
        self.rotate180_act.setStatusTip("Rotate image 180º clockwise")
        self.rotate180_act.triggered.connect(self.rotateImage180)

        self.flip_hor_act = QAction("Flip Horizontal")
        self.flip_hor_act.setStatusTip("Flip image across horizontal axis")
        self.flip_hor_act.triggered.connect(self.flipImageHorizontal)

        self.flip_ver_act = QAction("Flip Vertical")
        self.flip_ver_act.setStatusTip("Flip image across vertical axis")
        self.flip_ver_act.triggered.connect(self.flipImageVertical)

        self.resize_act = QAction("Resize Half")
        self.resize_act.setStatusTip("Resize image to half the original size")
        self.resize_act.triggered.connect(self.resizeImageHalf)

        self.clear_act = QAction(QIcon("images/clear.png"), "Clear Image")
        self.clear_act.setShortcut("Ctrl+D")
        self.clear_act.setStatusTip("Clear the current image")
        self.clear_act.triggered.connect(self.clearImage)

    def createMenu(self):
        """Create the application's menu bar."""
        self.menuBar().setNativeMenuBar(False)

        # Create File menu and add actions 
        file_menu = self.menuBar().addMenu("File")
        file_menu.addAction(self.open_act)
        file_menu.addAction(self.save_act)
        file_menu.addSeparator()
        file_menu.addAction(self.print_act)
        file_menu.addSeparator()
        file_menu.addAction(self.quit_act)

        # Create Edit menu and add actions 
        edit_menu = self.menuBar().addMenu("Edit")
        edit_menu.addAction(self.rotate90_act)
        edit_menu.addAction(self.rotate180_act)
        edit_menu.addSeparator()
        edit_menu.addAction(self.flip_hor_act)
        edit_menu.addAction(self.flip_ver_act)
        edit_menu.addSeparator()
        edit_menu.addAction(self.resize_act)
        edit_menu.addSeparator()
        edit_menu.addAction(self.clear_act)

        # Create View menu and add actions 
        view_menu = self.menuBar().addMenu("View")
        view_menu.addAction(self.toggle_dock_act)

    def createToolBar(self):
        """Create the application's toolbar."""
        tool_bar = QToolBar("Photo Editor Toolbar")
        tool_bar.setIconSize(QSize(24,24))
        self.addToolBar(tool_bar)

        # Add actions to the toolbar
        tool_bar.addAction(self.open_act)
        tool_bar.addAction(self.save_act)
        tool_bar.addAction(self.print_act)
        tool_bar.addAction(self.clear_act)
        tool_bar.addSeparator()
        tool_bar.addAction(self.quit_act)

    def createToolsDockWidget(self):
        """Create the application's dock widget.Use 
        View -> Edit Image Tools menu to show/hide the dock."""
        dock_widget = QDockWidget()
        dock_widget.setWindowTitle("Edit Image Tools")
        dock_widget.setAllowedAreas(
            Qt.DockWidgetArea.LeftDockWidgetArea |
            Qt.DockWidgetArea.RightDockWidgetArea)

        # Create buttons for editing images
        self.rotate90 = QPushButton("Rotate 90º")
        self.rotate90.setMinimumSize(QSize(130, 40))
        self.rotate90.setStatusTip("Rotate image 90º clockwise")
        self.rotate90.clicked.connect(self.rotateImage90)

        self.rotate180 = QPushButton("Rotate 180º")
        self.rotate180.setMinimumSize(QSize(130, 40))
        self.rotate180.setStatusTip("Rotate image 180º clockwise")
        self.rotate180.clicked.connect(self.rotateImage180)

        self.flip_horizontal = QPushButton("Flip Horizontal")
        self.flip_horizontal.setMinimumSize(QSize(130, 40))
        self.flip_horizontal.setStatusTip("Flip image across horizontal axis")
        self.flip_horizontal.clicked.connect(self.flipImageHorizontal)

        self.flip_vertical = QPushButton("Flip Vertical")
        self.flip_vertical.setMinimumSize(QSize(130, 40))
        self.flip_vertical.setStatusTip("Flip image across vertical axis")
        self.flip_vertical.clicked.connect(self.flipImageVertical)

        self.resize_half = QPushButton("Resize Half")
        self.resize_half.setMinimumSize(QSize(130, 40))
        self.resize_half.setStatusTip("Resize image to half the original size")
        self.resize_half.clicked.connect(self.resizeImageHalf)

        # Create layout for dock widget 
        dock_v_box = QVBoxLayout()
        dock_v_box.addWidget(self.rotate90)
        dock_v_box.addWidget(self.rotate180)
        dock_v_box.addStretch(1)
        dock_v_box.addWidget(self.flip_horizontal)
        dock_v_box.addWidget(self.flip_vertical)
        dock_v_box.addStretch(1)
        dock_v_box.addWidget(self.resize_half)
        dock_v_box.addStretch(10)

        # Create QWidget that acts as a container and
        # set the layout for the dock
        tools_contents = QWidget()
        tools_contents.setLayout(dock_v_box)
        dock_widget.setWidget(tools_contents)
        
        # Set initial location of dock widget
        self.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, 
            dock_widget)

        # Handle the visibility of the dock widget
        self.toggle_dock_act = dock_widget.toggleViewAction()

    def openImage(self):
        """Open an image file and display its contents on the 
        QLabel widget."""
        image_file, _ = QFileDialog.getOpenFileName(
            self, "Open Image", "", 
            "JPG Files (*.jpeg *.jpg );;PNG Files (*.png);;\
                Bitmap Files (*.bmp);;GIF Files (*.gif)")

        if image_file:
            self.image = QPixmap(image_file)

            self.image_label.setPixmap(self.image.scaled(self.image_label.size(), 
                Qt.AspectRatioMode.KeepAspectRatio, 
                Qt.TransformationMode.SmoothTransformation))
        else:
            QMessageBox.information(self, "No Image", 
                "No Image Selected.", QMessageBox.StandardButton.Ok)
        self.print_act.setEnabled(True)

    def saveImage(self):
        """Display QFileDialog to select image location and 
        save the image."""
        image_file, _ = QFileDialog.getSaveFileName(
            self, "Save Image", "", 
            "JPG Files (*.jpeg *.jpg );;PNG Files (*.png);;\
                Bitmap Files (*.bmp);;GIF Files (*.gif)")

        if image_file and self.image.isNull() == False:
            self.image.save(image_file)
        else:
            QMessageBox.information(self, "Not Saved", 
                "Image not saved.", QMessageBox.StandardButton.Ok)

    def printImage(self):
        """Print image and use QPrinter to select the 
        native system format for the printer dialog."""
        printer = QPrinter()
        # Configure the printer
        print_dialog = QPrintDialog(printer)
        if print_dialog.exec() == QDialog.DialogCode.Accepted:
            # Use QPainter to output a PDF file 
            painter = QPainter()
            painter.begin(printer)
            # Create QRect object to hold the painter's current 
            # viewport, which is the image_label 
            rect = QRect(painter.viewport())
            # Get the size of image_label and use it to set the size 
            # of the viewport
            size = QSize(self.image_label.pixmap().size())
            size.scale(rect.size(), Qt.AspectRatioMode.KeepAspectRatio)
            painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
            painter.setWindow(self.image_label.pixmap().rect())
            # Scale the image_label to fit the rect source (0, 0) 
            painter.drawPixmap(0, 0, self.image_label.pixmap())
            painter.end()
            
    def clearImage(self):
        """Clears current image in the QLabel widget."""
        self.image_label.clear()
        self.image = QPixmap() # Reset pixmap so that isNull() = True
        self.print_act.setEnabled(False)

    def rotateImage90(self):
        """Rotate image 90º clockwise."""
        if self.image.isNull() == False:
            transform90 = QTransform().rotate(90)
            pixmap = QPixmap(self.image)
            mode = Qt.TransformationMode.SmoothTransformation
            rotated = pixmap.transformed(transform90, 
                mode=mode)

            self.image_label.setPixmap(rotated.scaled(self.image_label.size(), 
                Qt.AspectRatioMode.KeepAspectRatio, 
                Qt.TransformationMode.SmoothTransformation))
            self.image = QPixmap(rotated) 
            self.image_label.repaint() # Repaint the child widget

    def rotateImage180(self):
        """Rotate image 180º clockwise."""
        if self.image.isNull() == False:
            transform180 = QTransform().rotate(180)
            pixmap = QPixmap(self.image)
            rotated = pixmap.transformed(transform180, 
                mode=Qt.TransformationMode.SmoothTransformation)

            self.image_label.setPixmap(rotated.scaled(self.image_label.size(), 
                Qt.AspectRatioMode.KeepAspectRatio, 
                Qt.TransformationMode.SmoothTransformation))
            # In order to keep from being allowed to rotate 
            # the image, set the rotated image as self.image 
            self.image = QPixmap(rotated) 
            self.image_label.repaint() # Repaint the child widget

    def flipImageHorizontal(self):
        """Mirror the image across the horizontal axis."""
        if self.image.isNull() == False:
            flip_h = QTransform().scale(-1, 1)
            pixmap = QPixmap(self.image)
            flipped = pixmap.transformed(flip_h)

            self.image_label.setPixmap(flipped.scaled(self.image_label.size(), 
                Qt.AspectRatioMode.KeepAspectRatio, 
                Qt.TransformationMode.SmoothTransformation))
            self.image = QPixmap(flipped)
            self.image_label.repaint()

    def flipImageVertical(self):
        """Mirror the image across the vertical axis."""
        if self.image.isNull() == False:
            flip_v = QTransform().scale(1, -1)
            pixmap = QPixmap(self.image)
            flipped = pixmap.transformed(flip_v)

            self.image_label.setPixmap(flipped.scaled(self.image_label.size(), 
                Qt.AspectRatioMode.KeepAspectRatio, 
                Qt.TransformationMode.SmoothTransformation))
            self.image = QPixmap(flipped)
            self.image_label.repaint()

    def resizeImageHalf(self):
        """Resize the image to half its current size."""
        if self.image.isNull() == False:
            resize = QTransform().scale(0.5, 0.5)
            pixmap = QPixmap(self.image)
            resized = pixmap.transformed(resize)

            self.image_label.setPixmap(resized.scaled(self.image_label.size(), 
                Qt.AspectRatioMode.KeepAspectRatio,
                Qt.TransformationMode.SmoothTransformation))
            self.image = QPixmap(resized)
            self.image_label.repaint()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setAttribute(
        Qt.ApplicationAttribute.AA_DontShowIconsInMenus, True)
    window = MainWindow()
    sys.exit(app.exec())

'Beginning PyQt' 카테고리의 다른 글

Handling Events  (0) 2022.05.26
Styling Your GUIs  (0) 2022.05.21
QIcon Class  (0) 2022.05.21
Menus, Toolbars, and More  (0) 2022.05.21
Nested Layouts  (0) 2022.05.15