Python PyQt5 standard dialog box usage example, pythonpyqt5

Source: Internet
Author: User

Python PyQt5 standard dialog box usage example, pythonpyqt5

This article describes how to use the Python PyQt5 standard dialog box. We will share this with you for your reference. The details are as follows:

Complete Qt standard dialog box, including QInputDialog, QColorDialog, QFontDialog, QMessageBox, QOpenFileDialog...

All are converted from the C ++ version on the official website to the PyQt5 version.

Some details are ignored, because I don't know how to convert them. It was a good time to complete the evening!

:

Complete code:

#-*-Coding: UTF-8-*-from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * import sysclass DialogOptionsWidget (QWidget): def _ init _ (self, parent = None): super (DialogOptionsWidget, self ). _ init _ (parent) def addCheckBox (self, text, value): pass def addSpacer (): pass def value (): passclass StandardDialog (QDialog ): def _ init _ (self, parent = None): super (StandardDialog, self ). _ Init _ (parent) self. setWindowTitle ("Standard Dialog") frameStyle = QFrame. sunken | QFrame. panel mainLayout = QVBoxLayout (self) toolbox = QToolBox () mainLayout. addWidget (toolbox) self. errorMessageDialog = QErrorMessage (self) pushButton_integer = QPushButton ("QInputDialog. get & Int () ") pushButton_double = QPushButton (" QInputDialog. get & Double () ") pushButton_item = QPushButton (" QInputDialog. getIte & m () ") PushButton_text = QPushButton (" QInputDialog. get & Text () ") pushButton_multiLineText = QPushButton (" QInputDialog. get & MultiLineText () ") pushButton_color = QPushButton (" QColorDialog. get & Color () ") pushButton_font = QPushButton (" QFontDialog. get & Font () ") pushButton_directory = QPushButton (" QFileDialog. getE & xistingDirectory () ") pushButton_openFileName = QPushButton (" QFileDialog. get & OpenFileName () ") push Button_openFileNames = QPushButton ("QFileDialog. & getOpenFileNames () ") pushButton_saveFileName = QPushButton (" QFileDialog. get & SaveFileName () ") pushButton_critical = QPushButton (" QMessageBox. critica & l () ") pushButton_information = QPushButton (" QMessageBox. I & nformation () ") pushButton_question = QPushButton (" QQMessageBox. & question () ") pushButton_warning = QPushButton (" QMessageBox. & warning () ") pushButto N_error = QPushButton ("QErrorMessage. showM & essage () ") self. label_integer = QLabel () self. label_double = QLabel () self. label_item = QLabel () self. label_text = QLabel () self. label_multiLineText = QLabel () self. label_color = QLabel () self. label_font = QLabel () self. label_directory = QLabel () self. label_openFileName = QLabel () self. label_openFileNames = QLabel () self. label_saveFileName = QLabel () self. Label_critical = QLabel () self. label_information = QLabel () self. label_question = QLabel () self. label_warning = QLabel () self. label_error = QLabel () self. label_integer.setFrameStyle (frameStyle) self. label_double.setFrameStyle (frameStyle) self. label_item.setFrameStyle (frameStyle) self. label_text.setFrameStyle (frameStyle) self. label_multiLineText.setFrameStyle (frameStyle) self. label_color.setFrameS Tyle (frameStyle) self. label_font.setFrameStyle (frameStyle) self. label_directory.setFrameStyle (frameStyle) self. label_openFileName.setFrameStyle (frameStyle) self. label_openFileNames.setFrameStyle (frameStyle) self. label_saveFileName.setFrameStyle (frameStyle) self. label_critical.setFrameStyle (frameStyle) self. label_information.setFrameStyle (frameStyle) self. label_question.setFrameStyle (frameStyle) se Lf. label_warning.setFrameStyle (frameStyle) self. label_error.setFrameStyle (frameStyle) page = QWidget () layout = QGridLayout (page) layout. setColumnStretch (1, 1) layout. setColumnMinimumWidth (1,250) layout. addWidget (pushButton_integer, 0, 0) layout. addWidget (self. label_integer, 0, 1) layout. addWidget (pushButton_double, 1, 0) layout. addWidget (self. label_double, 1, 1) layout. addWidget (pushButton_item, 2, 0) layou T. addWidget (self. label_item, 2, 1) layout. addWidget (pushButton_text, 3, 0) layout. addWidget (self. label_text, 3,1) layout. addWidget (pushButton_multiLineText, 4,0) layout. addWidget (self. label_multiLineText, 4,1) layout. addItem (QSpacerItem (0, 0, QSizePolicy. ignored, QSizePolicy. minimumExpanding), 5, 0) toolbox. addItem (page, "Input Dialog") page = QWidget () layout = QGridLayout (page) layout. setColumnStretch (1, 1) # Layout. setColumnMinimumWidth (1,250) layout. addWidget (pushButton_color, 0, 0) layout. addWidget (self. label_color, 0, 1) colorDialogOptionsWidget = DialogOptionsWidget () colorDialogOptionsWidget. addCheckBox ("Do not use native dialog", QColorDialog. dontUseNativeDialog) colorDialogOptionsWidget. addCheckBox ("Show alpha channel", QColorDialog. showAlphaChannel) colorDialogOptionsWidget. addCheckBox ("No butto Ns ", QColorDialog. noButtons) layout. addItem (QSpacerItem (0, 0, QSizePolicy. ignored, QSizePolicy. minimumExpanding), 1, 0) layout. addWidget (colorDialogOptionsWidget, 2, 0, 1, 2) toolbox. addItem (page, "Color Dialog") page = QWidget () layout = QGridLayout (page) layout. setColumnStretch (1, 1) layout. addWidget (pushButton_font, 0, 0) layout. addWidget (self. label_font, 0, 1) fontDialogOptionsWidget = DialogOptio NsWidget () fontDialogOptionsWidget. addCheckBox ("Do not use native dialog", QFontDialog. dontUseNativeDialog) fontDialogOptionsWidget. addCheckBox ("No buttons", QFontDialog. noButtons) layout. addItem (QSpacerItem (0, 0, QSizePolicy. ignored, QSizePolicy. minimumExpanding), 1, 0) layout. addWidget (fontDialogOptionsWidget, 2, 0, 1, 2) toolbox. addItem (page, "Font Dialog") page = QWidget () layout = QGridLayout (Page) layout. setColumnStretch (1, 1) layout. addWidget (pushButton_directory, 0, 0) layout. addWidget (self. label_directory, 0, 1) layout. addWidget (pushButton_openFileName, 1, 0) layout. addWidget (self. label_openFileName, 1, 1) layout. addWidget (pushButton_openFileNames, 2, 0) layout. addWidget (self. label_openFileNames, 2, 1) layout. addWidget (pushButton_saveFileName, 3, 0) layout. addWidget (self. label_sav EFileName, 3, 1) fileDialogOptionsWidget = DialogOptionsWidget () fileDialogOptionsWidget. addCheckBox ("Do not use native dialog", QFileDialog. dontUseNativeDialog) fileDialogOptionsWidget. addCheckBox ("Show directories only", QFileDialog. showDirsOnly) fileDialogOptionsWidget. addCheckBox ("Do not resolve symlinks", QFileDialog. dontResolveSymlinks) fileDialogOptionsWidget. addCheckBox ("Do not confirm ove Rwrite ", QFileDialog. dontConfirmOverwrite) fileDialogOptionsWidget. addCheckBox ("Do not use sheet", QFileDialog. dontUseSheet) fileDialogOptionsWidget. addCheckBox ("Readonly", QFileDialog. readOnly) fileDialogOptionsWidget. addCheckBox ("Hide name filter details", QFileDialog. hideNameFilterDetails) layout. addItem (QSpacerItem (0, 0, QSizePolicy. ignored, QSizePolicy. minimumExpanding), 4, 0) layout. addWidge T (fileDialogOptionsWidget, 5, 0, 1, 2) toolbox. addItem (page, "File Dialogs") page = QWidget () layout = QGridLayout (page) layout. setColumnStretch (1, 1) layout. addWidget (pushButton_critical, 0, 0) layout. addWidget (self. label_critical, 0, 1) layout. addWidget (pushButton_information, 1, 0) layout. addWidget (self. label_information, 1, 1) layout. addWidget (pushButton_question, 2, 0) layout. addWidget (self. l Abel_question, 2, 1) layout. addWidget (pushButton_warning, 3, 0) layout. addWidget (self. label_warning, 3, 1) layout. addWidget (pushButton_error, 4, 0) layout. addWidget (self. label_error, 4, 1) layout. addItem (QSpacerItem (0, 0, QSizePolicy. ignored, QSizePolicy. minimumExpanding), 5, 0) toolbox. addItem (page, "Message Boxes") pushButton_integer.clicked.connect (self. setInteger) pushButton_double.clicked.con Nect (self. setDouble) pushButton_item.clicked.connect (self. setItem) pushButton_text.clicked.connect (self. setText) pushButton_multiLineText.clicked.connect (self. setMultiLineText) pushButton_color.clicked.connect (self. setColor) pushButton_font.clicked.connect (self. setFont) pushButton_directory.clicked.connect (self. setExistingDirectory) pushButton_openFileName.clicked.connect (self. setOpenFileName) pus HButton_openFileNames.clicked.connect (self. setOpenFileNames) pushButton_saveFileName.clicked.connect (self. setSaveFileName) pushButton_critical.clicked.connect (self. criticalMessage) pushButton_information.clicked.connect (self. informationMessage) pushButton_question.clicked.connect (self. questionMessage) pushButton_warning.clicked.connect (self. warningMessage) pushButton_error.clicked.connect (self. err OrMessage) # in the input dialog box, take the integer def setInteger (self): intNum, OK = QInputDialog. getInt (self, "QInputDialog. getInteger () "," Percentage: ", 25, 0,100, 1) if OK: self. label_integer.setText (str (intNum) # in the input dialog box, set the real number def setDouble (self): doubleNum, OK = QInputDialog. getDouble (self, "QInputDialog. getDouble () "," Amount: ", 37.56,-10000,100 00, 2) if OK: self. label_double.setText (str (doubleNum) # in the input dialog box, retrieve the list item def setIte. M (self): items = ["Spring", "Summer", "Fall", "Winter"] item, OK = QInputDialog. getItem (self, "QInputDialog. getItem () "," Season: ", items, 0, False) if OK and item: self. label_item.setText (item) # in the input dialog box, retrieve the text def setText (self): text, OK = QInputDialog. getText (self, "QInputDialog. getText () "," User name: ", QLineEdit. normal, QDir. home (). dirName () if OK and text: self. label_text.setText (text) # multiple lines of text in the input dialog box Def setMultiLineText (self): text, OK = QInputDialog. getMultiLineText (self, "QInputDialog. getMultiLineText () "," Address: "," John Doe \ nFreedom Street ") if OK and text: self. label_multiLineText.setText (text) # color dialog box color def setColor (self): # options = QColorDialog. colorDialogOptions (QFlag. QFlag (colorDialogOptionsWidget. value () color = QColorDialog. getColor (Qt. green, self, "Select Color") if color. is Valid (): self. label_color.setText (color. name () self. label_color.setPalette (QPalette (color) self. label_color.setAutoFillBackground (True) # in the font dialog box, set the font def setFont (self): # options = QFontDialog. fontDialogOptions (QFlag (fontDialogOptionsWidget. value () # font, OK = QFontDialog. getFont (OK, QFont (self. label_font.text (), self, "Select Font", options) font, OK = QFontDialog. getFont () if OK: self. label_font. SetText (font. key () self. label_font.setFont (font) # in the directory dialog box, retrieve the directory def setExistingDirectory (self): # options = QFileDialog. options (QFlag (fileDialogOptionsWidget-> value () # options | = QFileDialog. dontResolveSymlinks | QFileDialog. showDirsOnly directory = QFileDialog. getExistingDirectory (self, "QFileDialog. getExistingDirectory () ", self. label_directory.text () if directory: self. label_directory.setText (di Rectory) # open the file dialog box and retrieve the file name def setOpenFileName (self): # options = QFileDialog. options (QFlag (fileDialogOptionsWidget. value () # selectedFilter fileName, filetype = QFileDialog. getOpenFileName (self, "QFileDialog. getOpenFileName () ", self. label_openFileName.text (), "All Files (*); Text Files (*. txt) ") if fileName: self. label_openFileName.setText (fileName) # in the open file dialog box, set the file name def setOpenFileNames (self): # option S = QFileDialog. options (QFlag (fileDialogOptionsWidget. value () # selectedFilter openFilesPath = "D:/documents/pyMarksix/draw/" files, OK = QFileDialog. getOpenFileNames (self, "QFileDialog. getOpenFileNames () ", openFilesPath," All Files (*); Text Files (*. txt) ") if len (files): self. label_openFileNames.setText (",". join (files) # Save the file dialog box to get the file name def setSaveFileName (self): # options = QFileDialog. options (QF Lag (fileDialogOptionsWidget. value () # selectedFilter fileName, OK = QFileDialog. getSaveFileName (self, "QFileDialog. getSaveFileName () ", self. label_saveFileName.text (), "All Files (*); Text Files (*. txt) ") if fileName: self. label_saveFileName.setText (fileName) def criticalMessage (self): # reply = QMessageBox. standardButton () MESSAGE = "criticism! "Reply = QMessageBox. critical (self, "QMessageBox. critical () ", MESSAGE, QMessageBox. abort | QMessageBox. retry | QMessageBox. ignore) if reply = QMessageBox. abort: self. label_critical.setText ("Abort") elif reply = QMessageBox. retry: self. label_critical.setText ("Retry") else: self. label_critical.setText ("Ignore") def informationMessage (self): MESSAGE = "MESSAGE" reply = QMessageBox. information (self, "QMe SsageBox. information () ", MESSAGE) if reply = QMessageBox. OK: self. label_information.setText ("OK") else: self. label_information.setText ("Escape") def questionMessage (self): MESSAGE = "Question" reply = QMessageBox. question (self, "QMessageBox. question () ", MESSAGE, QMessageBox. yes | QMessageBox. no | QMessageBox. cancel) if reply = QMessageBox. yes: self. label_question.setText ("Yes") elif reply = QMessageB Ox. no: self. label_question.setText ("No") else: self. label_question.setText ("Cancel") def warningMessage (self): MESSAGE = "warning text" msgBox = QMessageBox (QMessageBox. warning, "QMessageBox. warning () ", MESSAGE, QMessageBox. retry | QMessageBox. discard | QMessageBox. cancel, self) msgBox. setDetailedText ("details... ") # MsgBox. addButton ("Save & Again", QMessageBox. acceptRole) # msgBox. addButton ("& Continue", QMessageBox. rejectRole) if msgBox.exe c () = QMessageBox. acceptRole: self. label_warning.setText ("Retry") else: self. label_warning.setText ("Abort") def errorMessage (self): self. errorMessageDialog. showMessage ("This dialog shows and remembers error messages. "" If the checkbox is checked (as it is by default), "" the shown message will be shown again, "" but if the user unchecks the box the message "" will not appear again if QErrorMessage. showMessage () "" is called with the same message. ") self. label_error.setText ("If the box is unchecked, the message" "won't appear again. ")app=qapplication(sys.argv1_form1_standarddialog(appsform.show()app.exe c _()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.