QDialog&QMessageBox 사용법
반응형
import sys, os
from PyQt5.QtWidgets import *


class BaseWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.lbl = QLabel("EXAMPLE")

        self.btn_1 = QPushButton("QInputDialog")
        self.btn_1.clicked.connect(self.Input_showDialog)
        self.btn_2 = QPushButton("QColorDialog")
        self.btn_2.clicked.connect(self.Color_showDialog)
        self.btn_3 = QPushButton("QFontDialog")
        self.btn_3.clicked.connect(self.Font_showDialog)
        self.btn_4 = QPushButton("QFileDialog")
        self.btn_4.clicked.connect(self.File_showDialog)
        self.btn_5 = QPushButton("QMessageBox")
        self.btn_5.clicked.connect(self.Message_showDialog)

        vbox = QVBoxLayout()
        vbox.addStretch(3)
        vbox.addWidget(self.lbl)
        vbox.addWidget(self.btn_1)
        vbox.addWidget(self.btn_2)
        vbox.addWidget(self.btn_3)
        vbox.addWidget(self.btn_4)
        vbox.addWidget(self.btn_5)
        vbox.addStretch(1)

        self.setLayout(vbox)

        self.setWindowTitle('QDialog')
        self.setGeometry(0, 0, 500, 500)
        self.show()

    def Input_showDialog(self):
        text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')

        if ok:
            self.lbl.setText(str(text))

    def Color_showDialog(self):
        col = QColorDialog.getColor()

        if col.isValid():
            self.lbl.setStyleSheet('QWidget { background-color: %s }' % col.name())

    def Font_showDialog(self):
        # PyQT5
        font, ok = QFontDialog.getFont()
        
        # PySide6
        # ok, font = QFontDialog.getFont()

        if ok:
            self.lbl.setFont(font)

    def File_showDialog(self):
        fname = QFileDialog.getOpenFileName(self, 'Open file', './')

        if fname[0]:
            f = open(fname[0], 'r')

            with f:
                data = f.read()
                self.lbl.setText(data)

    def Message_showDialog(self):
        reply = QMessageBox.question(self, 'Message', 'Are you sure to quit?',
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            print("YES")
        else:
            print("NO")


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

 

 

 

 

 

 

QInputDialog

PyQT5 : https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtwidgets/qinputdialog.html?highlight=qinputdialog#QInputDialog

PySide 6 :https://doc.qt.io/qtforpython/PySide6/QtWidgets/QInputDialog.html?highlight=qinputdialog

 

QColorDialog

PyQT5 :https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtwidgets/qcolordialog.html?highlight=qcolordialog#QColorDialog

PySide 6 :https://doc.qt.io/qtforpython/PySide6/QtWidgets/QColorDialog.html?highlight=qcolordialog

 

QFontDialog

PyQT5 : https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtwidgets/qfontdialog.html?highlight=qfontdialog#QFontDialog

PySide 6 : https://doc.qt.io/qtforpython/PySide6/QtWidgets/QFontDialog.html?highlight=qfontdialog

 

QFileDialog

PyQT5 :https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtwidgets/qfiledialog.html?highlight=qfiledialog#QFileDialog

PySide 6 :https://doc.qt.io/qtforpython/PySide6/QtWidgets/QFileDialog.html?highlight=qfiledialog

 

QMessageBox

PyQT5 :https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtwidgets/qmessagebox.html?highlight=qmessagebox#QMessageBox

PySide 6 :https://doc.qt.io/qtforpython/PySide6/QtWidgets/QMessageBox.html?highlight=qmessagebox

반응형