很全的Qt的标准对话框,包含QInputDialog、QColorDialog、QFontDialog、QMessageBox、QOpenFileDialog...
全部是由官网的C++版本,转换成PyQt5版本。
有些细节忽略了,因为实在不知怎么转换过来。捣鼓了一晚上,总算完成了,好累啊,不过很开心!
效果图:
完整代码:
1 # -*- coding: utf-8 -*- 2 from PyQt5.QtGui import * 3 from PyQt5.QtCore import * 4 from PyQt5.QtWidgets import * 5 6 import sys 7 8 9 class DialogOptionsWidget(QWidget): 10 def __init__(self, parent=None): 11 super(DialogOptionsWidget,self).__init__(parent) 12 13 def addCheckBox(self, text, value): 14 pass 15 16 def addSpacer(): 17 pass 18 19 def value(): 20 pass 21 22 23 class StandardDialog(QDialog): 24 def __init__(self,parent=None): 25 super(StandardDialog,self).__init__(parent) 26 27 self.setWindowTitle("Standard Dialog") 28 29 frameStyle = QFrame.Sunken | QFrame.Panel 30 31 mainLayout = QVBoxLayout(self) 32 toolbox = QToolBox() 33 mainLayout.addWidget(toolbox) 34 35 self.errorMessageDialog = QErrorMessage(self) 36 37 pushButton_integer = QPushButton("QInputDialog.get&Int()") 38 pushButton_double = QPushButton("QInputDialog.get&Double()") 39 pushButton_item = QPushButton("QInputDialog.getIte&m()") 40 pushButton_text = QPushButton("QInputDialog.get&Text()") 41 pushButton_multiLineText = QPushButton("QInputDialog.get&MultiLineText()") 42 pushButton_color = QPushButton("QColorDialog.get&Color()") 43 pushButton_font = QPushButton("QFontDialog.get&Font()") 44 pushButton_directory = QPushButton("QFileDialog.getE&xistingDirectory()") 45 pushButton_openFileName = QPushButton("QFileDialog.get&OpenFileName()") 46 pushButton_openFileNames = QPushButton("QFileDialog.&getOpenFileNames()") 47 pushButton_saveFileName = QPushButton("QFileDialog.get&SaveFileName()") 48 pushButton_critical = QPushButton("QMessageBox.critica&l()") 49 pushButton_information = QPushButton("QMessageBox.i&nformation()") 50 pushButton_question = QPushButton("QQMessageBox.&question()") 51 pushButton_warning = QPushButton("QMessageBox.&warning()") 52 pushButton_error = QPushButton("QErrorMessage.showM&essage()") 53 54 self.label_integer = QLabel() 55 self.label_double = QLabel() 56 self.label_item = QLabel() 57 self.label_text = QLabel() 58 self.label_multiLineText = QLabel() 59 self.label_color = QLabel() 60 self.label_font = QLabel() 61 self.label_directory = QLabel() 62 self.label_openFileName = QLabel() 63 self.label_openFileNames = QLabel() 64 self.label_saveFileName = QLabel() 65 self.label_critical = QLabel() 66 self.label_information = QLabel() 67 self.label_question = QLabel() 68 self.label_warning = QLabel() 69 self.label_error = QLabel() 70 71 self.label_integer.setFrameStyle(frameStyle) 72 self.label_double.setFrameStyle(frameStyle) 73 self.label_item.setFrameStyle(frameStyle) 74 self.label_text.setFrameStyle(frameStyle) 75 self.label_multiLineText.setFrameStyle(frameStyle) 76 self.label_color.setFrameStyle(frameStyle) 77 self.label_font.setFrameStyle(frameStyle) 78 self.label_directory.setFrameStyle(frameStyle) 79 self.label_openFileName.setFrameStyle(frameStyle) 80 self.label_openFileNames.setFrameStyle(frameStyle) 81 self.label_saveFileName.setFrameStyle(frameStyle) 82 self.label_critical.setFrameStyle(frameStyle) 83 self.label_information.setFrameStyle(frameStyle) 84 self.label_question.setFrameStyle(frameStyle) 85 self.label_warning.setFrameStyle(frameStyle) 86 self.label_error.setFrameStyle(frameStyle) 87 88 89 page = QWidget() 90 layout = QGridLayout(page) 91 layout.setColumnStretch(1,1) 92 layout.setColumnMinimumWidth(1,250) 93 layout.addWidget(pushButton_integer,0,0) 94 layout.addWidget(self.label_integer,0,1) 95 layout.addWidget(pushButton_double,1,0) 96 layout.addWidget(self.label_double,1,1) 97 layout.addWidget(pushButton_item,2,0) 98 layout.addWidget(self.label_item,2,1) 99 layout.addWidget(pushButton_text,3,0) 100 layout.addWidget(self.label_text,3,1) 101 layout.addWidget(pushButton_multiLineText,4,0) 102 layout.addWidget(self.label_multiLineText,4,1) 103 layout.addItem(QSpacerItem(0,0,QSizePolicy.Ignored,QSizePolicy.MinimumExpanding),5,0) 104 toolbox.addItem(page, "Input Dialog") 105 106 page = QWidget() 107 layout = QGridLayout(page) 108 layout.setColumnStretch(1,1) 109 #layout.setColumnMinimumWidth(1,250) 110 layout.addWidget(pushButton_color,0,0) 111 layout.addWidget(self.label_color,0,1) 112 colorDialogOptionsWidget = DialogOptionsWidget() 113 colorDialogOptionsWidget.addCheckBox("Do not use native dialog", QColorDialog.DontUseNativeDialog) 114 colorDialogOptionsWidget.addCheckBox("Show alpha channel" , QColorDialog.ShowAlphaChannel) 115 colorDialogOptionsWidget.addCheckBox("No buttons" , QColorDialog.NoButtons) 116 layout.addItem(QSpacerItem(0,0,QSizePolicy.Ignored,QSizePolicy.MinimumExpanding),1,0) 117 layout.addWidget(colorDialogOptionsWidget, 2, 0, 1 ,2) 118 toolbox.addItem(page, "Color Dialog") 119 120 page = QWidget() 121 layout = QGridLayout(page) 122 layout.setColumnStretch(1, 1) 123 layout.addWidget(pushButton_font, 0, 0) 124 layout.addWidget(self.label_font, 0, 1) 125 fontDialogOptionsWidget = DialogOptionsWidget() 126 fontDialogOptionsWidget.addCheckBox("Do not use native dialog", QFontDialog.DontUseNativeDialog) 127 fontDialogOptionsWidget.addCheckBox("No buttons", QFontDialog.NoButtons) 128 layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 1, 0) 129 layout.addWidget(fontDialogOptionsWidget, 2, 0, 1 ,2) 130 toolbox.addItem(page, "Font Dialog") 131 132 page = QWidget() 133 layout = QGridLayout(page) 134 layout.setColumnStretch(1, 1) 135 layout.addWidget(pushButton_directory, 0, 0) 136 layout.addWidget(self.label_directory, 0, 1) 137 layout.addWidget(pushButton_openFileName, 1, 0) 138 layout.addWidget(self.label_openFileName, 1, 1) 139 layout.addWidget(pushButton_openFileNames, 2, 0) 140 layout.addWidget(self.label_openFileNames, 2, 1) 141 layout.addWidget(pushButton_saveFileName, 3, 0) 142 layout.addWidget(self.label_saveFileName, 3, 1) 143 fileDialogOptionsWidget = DialogOptionsWidget() 144 fileDialogOptionsWidget.addCheckBox("Do not use native dialog", QFileDialog.DontUseNativeDialog) 145 fileDialogOptionsWidget.addCheckBox("Show directories only", QFileDialog.ShowDirsOnly) 146 fileDialogOptionsWidget.addCheckBox("Do not resolve symlinks", QFileDialog.DontResolveSymlinks) 147 fileDialogOptionsWidget.addCheckBox("Do not confirm overwrite", QFileDialog.DontConfirmOverwrite) 148 fileDialogOptionsWidget.addCheckBox("Do not use sheet", QFileDialog.DontUseSheet) 149 fileDialogOptionsWidget.addCheckBox("Readonly", QFileDialog.ReadOnly) 150 fileDialogOptionsWidget.addCheckBox("Hide name filter details", QFileDialog.HideNameFilterDetails) 151 layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 4, 0) 152 layout.addWidget(fileDialogOptionsWidget, 5, 0, 1 ,2) 153 toolbox.addItem(page, "File Dialogs") 154 155 page = QWidget() 156 layout = QGridLayout(page) 157 layout.setColumnStretch(1, 1) 158 layout.addWidget(pushButton_critical, 0, 0) 159 layout.addWidget(self.label_critical, 0, 1) 160 layout.addWidget(pushButton_information, 1, 0) 161 layout.addWidget(self.label_information, 1, 1) 162 layout.addWidget(pushButton_question, 2, 0) 163 layout.addWidget(self.label_question, 2, 1) 164 layout.addWidget(pushButton_warning, 3, 0) 165 layout.addWidget(self.label_warning, 3, 1) 166 layout.addWidget(pushButton_error, 4, 0) 167 layout.addWidget(self.label_error, 4, 1) 168 layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 5, 0) 169 toolbox.addItem(page, "Message Boxes") 170 171 pushButton_integer.clicked.connect(self.setInteger) 172 pushButton_double.clicked.connect(self.setDouble) 173 pushButton_item.clicked.connect(self.setItem) 174 pushButton_text.clicked.connect(self.setText) 175 pushButton_multiLineText.clicked.connect(self.setMultiLineText) 176 pushButton_color.clicked.connect(self.setColor) 177 pushButton_font.clicked.connect(self.setFont) 178 pushButton_directory.clicked.connect(self.setExistingDirectory) 179 pushButton_openFileName.clicked.connect(self.setOpenFileName) 180 pushButton_openFileNames.clicked.connect(self.setOpenFileNames) 181 pushButton_saveFileName.clicked.connect(self.setSaveFileName) 182 pushButton_critical.clicked.connect(self.criticalMessage) 183 pushButton_information.clicked.connect(self.informationMessage) 184 pushButton_question.clicked.connect(self.questionMessage) 185 pushButton_warning.clicked.connect(self.warningMessage) 186 pushButton_error.clicked.connect(self.errorMessage) 187 188 #输入对话框 取整数 189 def setInteger(self): 190 intNum, ok = QInputDialog.getInt(self, "QInputDialog.getInteger()","Percentage:", 25, 0, 100, 1) 191 if ok: 192 self.label_integer.setText(str(intNum)) 193 194 #输入对话框 取实数 195 def setDouble(self): 196 doubleNum, ok = QInputDialog.getDouble(self, "QInputDialog.getDouble()", "Amount:", 37.56, -10000, 10000, 2) 197 if ok: 198 self.label_double.setText(str(doubleNum)) 199 200 #输入对话框 取列表项 201 def setItem(self): 202 items = ["Spring", "Summer", "Fall", "Winter"] 203 item, ok = QInputDialog.getItem(self, "QInputDialog.getItem()","Season:", items, 0, False) 204 if ok and item: 205 self.label_item.setText(item) 206 207 #输入对话框 取文本 208 def setText(self): 209 text, ok = QInputDialog.getText(self, "QInputDialog.getText()", "User name:", QLineEdit.Normal, QDir.home().dirName()) 210 if ok and text: 211 self.label_text.setText(text) 212 213 #输入对话框 取多行文本 214 def setMultiLineText(self): 215 text, ok = QInputDialog.getMultiLineText(self, "QInputDialog.getMultiLineText()", "Address:", "John Doe\nFreedom Street") 216 if ok and text: 217 self.label_multiLineText.setText(text) 218 219 #颜色对话框 取颜色 220 def setColor(self): 221 #options = QColorDialog.ColorDialogOptions(QFlag.QFlag(colorDialogOptionsWidget.value())) 222 color = QColorDialog.getColor(Qt.green, self, "Select Color") 223 224 if color.isValid(): 225 self.label_color.setText(color.name()) 226 self.label_color.setPalette(QPalette(color)) 227 self.label_color.setAutoFillBackground(True) 228 229 #字体对话框 取字体 230 def setFont(self): 231 #options = QFontDialog.FontDialogOptions(QFlag(fontDialogOptionsWidget.value())) 232 #font, ok = QFontDialog.getFont(ok, QFont(self.label_font.text()), self, "Select Font",options) 233 font, ok = QFontDialog.getFont() 234 if ok: 235 self.label_font.setText(font.key()) 236 self.label_font.setFont(font) 237 238 #目录对话框 取目录 239 def setExistingDirectory(self): 240 #options = QFileDialog.Options(QFlag(fileDialogOptionsWidget->value())) 241 #options |= QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly 242 directory = QFileDialog.getExistingDirectory(self, 243 "QFileDialog.getExistingDirectory()", 244 self.label_directory.text()) 245 if directory: 246 self.label_directory.setText(directory) 247 248 #打开文件对话框 取文件名 249 def setOpenFileName(self): 250 #options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 251 #selectedFilter 252 fileName, filetype = QFileDialog.getOpenFileName(self, 253 "QFileDialog.getOpenFileName()", 254 self.label_openFileName.text(), 255 "All Files (*);;Text Files (*.txt)") 256 if fileName: 257 self.label_openFileName.setText(fileName) 258 259 #打开文件对话框 取一组文件名 260 def setOpenFileNames(self): 261 #options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 262 #selectedFilter 263 openFilesPath = "D:/documents/pyMarksix/draw/" 264 files, ok = QFileDialog.getOpenFileNames(self, 265 "QFileDialog.getOpenFileNames()", 266 openFilesPath, 267 "All Files (*);;Text Files (*.txt)") 268 269 if len(files): 270 self.label_openFileNames.setText(", ".join(files)) 271 272 #保存文件对话框 取文件名 273 def setSaveFileName(self): 274 #options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 275 #selectedFilter 276 fileName, ok = QFileDialog.getSaveFileName(self, 277 "QFileDialog.getSaveFileName()", 278 self.label_saveFileName.text(), 279 "All Files (*);;Text Files (*.txt)") 280 if fileName: 281 self.label_saveFileName.setText(fileName) 282 283 def criticalMessage(self): 284 #reply = QMessageBox.StandardButton() 285 MESSAGE = "批评!" 286 reply = QMessageBox.critical(self, 287 "QMessageBox.critical()", 288 MESSAGE, 289 QMessageBox.Abort | QMessageBox.Retry | QMessageBox.Ignore) 290 if reply == QMessageBox.Abort: 291 self.label_critical.setText("Abort") 292 elif reply == QMessageBox.Retry: 293 self.label_critical.setText("Retry") 294 else: 295 self.label_critical.setText("Ignore") 296 297 def informationMessage(self): 298 MESSAGE = "信息" 299 reply = QMessageBox.information(self, "QMessageBox.information()", MESSAGE) 300 if reply == QMessageBox.Ok: 301 self.label_information.setText("OK") 302 else: 303 self.label_information.setText("Escape") 304 305 def questionMessage(self): 306 MESSAGE = "疑问" 307 reply = QMessageBox.question(self, "QMessageBox.question()", 308 MESSAGE, 309 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 310 if reply == QMessageBox.Yes: 311 self.label_question.setText("Yes") 312 elif reply == QMessageBox.No: 313 self.label_question.setText("No") 314 else: 315 self.label_question.setText("Cancel") 316 317 def warningMessage(self): 318 MESSAGE = "警告文本" 319 msgBox = QMessageBox(QMessageBox.Warning, 320 "QMessageBox.warning()", 321 MESSAGE, 322 QMessageBox.Retry | QMessageBox.Discard | QMessageBox.Cancel, 323 self) 324 msgBox.setDetailedText("详细信息。。。") 325 #msgBox.addButton("Save &Again", QMessageBox.AcceptRole) 326 #msgBox.addButton("&Continue", QMessageBox.RejectRole) 327 if msgBox.exec() == QMessageBox.AcceptRole: 328 self.label_warning.setText("Retry") 329 else: 330 self.label_warning.setText("Abort") 331 332 def errorMessage(self): 333 self.errorMessageDialog.showMessage( 334 "This dialog shows and remembers error messages. " 335 "If the checkbox is checked (as it is by default), " 336 "the shown message will be shown again, " 337 "but if the user unchecks the box the message " 338 "will not appear again if QErrorMessage.showMessage() " 339 "is called with the same message.") 340 self.label_error.setText("If the box is unchecked, the message " 341 "won't appear again.") 342 343 app=QApplication(sys.argv) 344 form=StandardDialog() 345 form.show() 346 app.exec_()