【PyQt5-Qt Designer】Qt 的标准对话框总结
PyQt5 各种弹出对话框的总结
忙碌了两天才总结完,深刻体会到 “编程在实践中才能领悟更深”,后续有了更多的 理解继续来补充。。。
效果如下:
参考:
https://www.cnblogs.com/hhh5460/p/4271475.html
1 # -*- coding: utf-8 -*- 2 from PyQt5.QtGui import * 3 from PyQt5.QtCore import * 4 from PyQt5.QtWidgets import * 5 import sys 6 7 8 class DialogOptionsWidget(QWidget): 9 def __init__(self, parent=None): 10 super(DialogOptionsWidget, self).__init__(parent) 11 12 def addCheckBox(self, text, value): 13 pass 14 15 def addSpacer(): 16 pass 17 18 def value(): 19 pass 20 21 class StandardDialog(QDialog): 22 def __init__(self, parent=None): 23 super(StandardDialog, self).__init__(parent) 24 25 self.setWindowTitle("Standard Dialog") 26 27 frameStyle = QFrame.Sunken | QFrame.Panel 28 29 mainLayout = QVBoxLayout(self) 30 toolbox = QToolBox() 31 mainLayout.addWidget(toolbox) 32 33 self.errorMessageDialog = QErrorMessage(self) 34 35 pushButton_integer = QPushButton("QInputDialog.get&Int()") 36 pushButton_double = QPushButton("QInputDialog.get&Double()") 37 pushButton_item = QPushButton("QInputDialog.getIte&m()") 38 pushButton_text = QPushButton("QInputDialog.get&Text()") 39 pushButton_multiLineText = QPushButton("QInputDialog.get&MultiLineText()") 40 pushButton_color = QPushButton("QColorDialog.get&Color()") 41 pushButton_font = QPushButton("QFontDialog.get&Font()") 42 pushButton_directory = QPushButton("QFileDialog.getE&xistingDirectory()") 43 pushButton_openFileName = QPushButton("QFileDialog.get&OpenFileName()") 44 pushButton_openFileNames = QPushButton("QFileDialog.&getOpenFileNames()") 45 pushButton_saveFileName = QPushButton("QFileDialog.get&SaveFileName()") 46 pushButton_critical = QPushButton("QMessageBox.critica&l()") 47 pushButton_information = QPushButton("QMessageBox.i&nformation()") 48 pushButton_question = QPushButton("QQMessageBox.&question()") 49 pushButton_warning = QPushButton("QMessageBox.&warning()") 50 pushButton_error = QPushButton("QErrorMessage.showM&essage()") 51 52 self.label_integer = QLabel() 53 self.label_double = QLabel() 54 self.label_item = QLabel() 55 self.label_text = QLabel() 56 self.label_multiLineText = QLabel() 57 self.label_color = QLabel() 58 self.label_font = QLabel() 59 self.label_directory = QLabel() 60 self.label_openFileName = QLabel() 61 self.label_openFileNames = QLabel() 62 self.label_saveFileName = QLabel() 63 self.label_critical = QLabel() 64 self.label_information = QLabel() 65 self.label_question = QLabel() 66 self.label_warning = QLabel() 67 self.label_error = QLabel() 68 69 self.label_integer.setFrameStyle(frameStyle) 70 self.label_double.setFrameStyle(frameStyle) 71 self.label_item.setFrameStyle(frameStyle) 72 self.label_text.setFrameStyle(frameStyle) 73 self.label_multiLineText.setFrameStyle(frameStyle) 74 self.label_color.setFrameStyle(frameStyle) 75 self.label_font.setFrameStyle(frameStyle) 76 self.label_directory.setFrameStyle(frameStyle) 77 self.label_openFileName.setFrameStyle(frameStyle) 78 self.label_openFileNames.setFrameStyle(frameStyle) 79 self.label_saveFileName.setFrameStyle(frameStyle) 80 self.label_critical.setFrameStyle(frameStyle) 81 self.label_information.setFrameStyle(frameStyle) 82 self.label_question.setFrameStyle(frameStyle) 83 self.label_warning.setFrameStyle(frameStyle) 84 self.label_error.setFrameStyle(frameStyle) 85 86 page = QWidget() 87 layout = QGridLayout(page) 88 layout.setColumnStretch(1, 1) 89 layout.setColumnMinimumWidth(1, 250) 90 layout.addWidget(pushButton_integer, 0, 0) 91 layout.addWidget(self.label_integer, 0, 1) 92 layout.addWidget(pushButton_double, 1, 0) 93 layout.addWidget(self.label_double, 1, 1) 94 layout.addWidget(pushButton_item, 2, 0) 95 layout.addWidget(self.label_item, 2, 1) 96 layout.addWidget(pushButton_text, 3, 0) 97 layout.addWidget(self.label_text, 3, 1) 98 layout.addWidget(pushButton_multiLineText, 4, 0) 99 layout.addWidget(self.label_multiLineText, 4, 1) 100 layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 5, 0) 101 toolbox.addItem(page, "Input Dialog") 102 103 page = QWidget() 104 layout = QGridLayout(page) 105 layout.setColumnStretch(1, 1) 106 # layout.setColumnMinimumWidth(1,250) 107 layout.addWidget(pushButton_color, 0, 0) 108 layout.addWidget(self.label_color, 0, 1) 109 colorDialogOptionsWidget = DialogOptionsWidget() 110 colorDialogOptionsWidget.addCheckBox("Do not use native dialog", QColorDialog.DontUseNativeDialog) 111 colorDialogOptionsWidget.addCheckBox("Show alpha channel", QColorDialog.ShowAlphaChannel) 112 colorDialogOptionsWidget.addCheckBox("No buttons", QColorDialog.NoButtons) 113 layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 1, 0) 114 layout.addWidget(colorDialogOptionsWidget, 2, 0, 1, 2) 115 toolbox.addItem(page, "Color Dialog") 116 117 page = QWidget() 118 layout = QGridLayout(page) 119 layout.setColumnStretch(1, 1) 120 layout.addWidget(pushButton_font, 0, 0) 121 layout.addWidget(self.label_font, 0, 1) 122 fontDialogOptionsWidget = DialogOptionsWidget() 123 fontDialogOptionsWidget.addCheckBox("Do not use native dialog", QFontDialog.DontUseNativeDialog) 124 fontDialogOptionsWidget.addCheckBox("No buttons", QFontDialog.NoButtons) 125 layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 1, 0) 126 layout.addWidget(fontDialogOptionsWidget, 2, 0, 1, 2) 127 toolbox.addItem(page, "Font Dialog") 128 129 page = QWidget() 130 layout = QGridLayout(page) 131 layout.setColumnStretch(1, 1) 132 layout.addWidget(pushButton_directory, 0, 0) 133 layout.addWidget(self.label_directory, 0, 1) 134 layout.addWidget(pushButton_openFileName, 1, 0) 135 layout.addWidget(self.label_openFileName, 1, 1) 136 layout.addWidget(pushButton_openFileNames, 2, 0) 137 layout.addWidget(self.label_openFileNames, 2, 1) 138 layout.addWidget(pushButton_saveFileName, 3, 0) 139 layout.addWidget(self.label_saveFileName, 3, 1) 140 fileDialogOptionsWidget = DialogOptionsWidget() 141 fileDialogOptionsWidget.addCheckBox("Do not use native dialog", QFileDialog.DontUseNativeDialog) 142 fileDialogOptionsWidget.addCheckBox("Show directories only", QFileDialog.ShowDirsOnly) 143 fileDialogOptionsWidget.addCheckBox("Do not resolve symlinks", QFileDialog.DontResolveSymlinks) 144 fileDialogOptionsWidget.addCheckBox("Do not confirm overwrite", QFileDialog.DontConfirmOverwrite) 145 fileDialogOptionsWidget.addCheckBox("Do not use sheet", QFileDialog.DontUseSheet) 146 fileDialogOptionsWidget.addCheckBox("Readonly", QFileDialog.ReadOnly) 147 fileDialogOptionsWidget.addCheckBox("Hide name filter details", QFileDialog.HideNameFilterDetails) 148 layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 4, 0) 149 layout.addWidget(fileDialogOptionsWidget, 5, 0, 1, 2) 150 toolbox.addItem(page, "File Dialogs") 151 152 page = QWidget() 153 layout = QGridLayout(page) 154 layout.setColumnStretch(1, 1) 155 layout.addWidget(pushButton_critical, 0, 0) 156 layout.addWidget(self.label_critical, 0, 1) 157 layout.addWidget(pushButton_information, 1, 0) 158 layout.addWidget(self.label_information, 1, 1) 159 layout.addWidget(pushButton_question, 2, 0) 160 layout.addWidget(self.label_question, 2, 1) 161 layout.addWidget(pushButton_warning, 3, 0) 162 layout.addWidget(self.label_warning, 3, 1) 163 layout.addWidget(pushButton_error, 4, 0) 164 layout.addWidget(self.label_error, 4, 1) 165 layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 5, 0) 166 toolbox.addItem(page, "Message Boxes") 167 168 pushButton_integer.clicked.connect(self.setInteger) 169 pushButton_double.clicked.connect(self.setDouble) 170 pushButton_item.clicked.connect(self.setItem) 171 pushButton_text.clicked.connect(self.setText) 172 pushButton_multiLineText.clicked.connect(self.setMultiLineText) 173 pushButton_color.clicked.connect(self.setColor) 174 pushButton_font.clicked.connect(self.setFont) 175 pushButton_directory.clicked.connect(self.setExistingDirectory) 176 pushButton_openFileName.clicked.connect(self.setOpenFileName) 177 pushButton_openFileNames.clicked.connect(self.setOpenFileNames) 178 pushButton_saveFileName.clicked.connect(self.setSaveFileName) 179 pushButton_critical.clicked.connect(self.criticalMessage) 180 pushButton_information.clicked.connect(self.informationMessage) 181 pushButton_question.clicked.connect(self.questionMessage) 182 pushButton_warning.clicked.connect(self.warningMessage) 183 pushButton_error.clicked.connect(self.errorMessage) 184 185 # 输入对话框 取整数 186 def setInteger(self): 187 intNum, ok = QInputDialog.getInt(self, "QInputDialog.getInteger()", "Percentage:", 25, 0, 100, 1) 188 if ok: 189 self.label_integer.setText(str(intNum)) 190 191 # 输入对话框 取实数 192 def setDouble(self): 193 doubleNum, ok = QInputDialog.getDouble(self, "QInputDialog.getDouble()", "Amount:", 37.56, -10000, 10000, 2) 194 if ok: 195 self.label_double.setText(str(doubleNum)) 196 197 # 输入对话框 取列表项 198 def setItem(self): 199 items = ["Spring", "Summer", "Fall", "Winter"] 200 item, ok = QInputDialog.getItem(self, "QInputDialog.getItem()", "Season:", items, 0, False) 201 if ok and item: 202 self.label_item.setText(item) 203 204 # 输入对话框 取文本 205 def setText(self): 206 text, ok = QInputDialog.getText(self, "QInputDialog.getText()", "User name:", QLineEdit.Normal, 207 QDir.home().dirName()) 208 if ok and text: 209 self.label_text.setText(text) 210 211 # 输入对话框 取多行文本 212 def setMultiLineText(self): 213 text, ok = QInputDialog.getMultiLineText(self, "QInputDialog.getMultiLineText()", "Address:", 214 "John Doe\nFreedom Street") 215 if ok and text: 216 self.label_multiLineText.setText(text) 217 218 # 颜色对话框 取颜色 219 def setColor(self): 220 # options = QColorDialog.ColorDialogOptions(QFlag.QFlag(colorDialogOptionsWidget.value())) 221 color = QColorDialog.getColor(Qt.green, self, "Select Color") 222 223 if color.isValid(): 224 self.label_color.setText(color.name()) 225 self.label_color.setPalette(QPalette(color)) 226 self.label_color.setAutoFillBackground(True) 227 228 # 字体对话框 取字体 229 def setFont(self): 230 # options = QFontDialog.FontDialogOptions(QFlag(fontDialogOptionsWidget.value())) 231 # font, ok = QFontDialog.getFont(ok, QFont(self.label_font.text()), self, "Select Font",options) 232 font, ok = QFontDialog.getFont() 233 if ok: 234 self.label_font.setText(font.key()) 235 self.label_font.setFont(font) 236 237 # 目录对话框 取目录 238 def setExistingDirectory(self): 239 # options = QFileDialog.Options(QFlag(fileDialogOptionsWidget->value())) 240 # options |= QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly 241 directory = QFileDialog.getExistingDirectory(self, 242 "QFileDialog.getExistingDirectory()", 243 self.label_directory.text()) 244 if directory: 245 self.label_directory.setText(directory) 246 247 # 打开文件对话框 取文件名 248 def setOpenFileName(self): 249 # options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 250 # selectedFilter 251 fileName, filetype = QFileDialog.getOpenFileName(self, 252 "QFileDialog.getOpenFileName()", 253 self.label_openFileName.text(), 254 "All Files (*);;Text Files (*.txt)") 255 if fileName: 256 self.label_openFileName.setText(fileName) 257 258 # 打开文件对话框 取一组文件名 259 def setOpenFileNames(self): 260 # options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 261 # selectedFilter 262 openFilesPath = "D:/documents/pyMarksix/draw/" 263 files, ok = QFileDialog.getOpenFileNames(self, 264 "QFileDialog.getOpenFileNames()", 265 openFilesPath, 266 "All Files (*);;Text Files (*.txt)") 267 268 if len(files): 269 self.label_openFileNames.setText(", ".join(files)) 270 271 # 保存文件对话框 取文件名 272 def setSaveFileName(self): 273 # options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 274 # selectedFilter 275 fileName, ok = QFileDialog.getSaveFileName(self, 276 "QFileDialog.getSaveFileName()", 277 self.label_saveFileName.text(), 278 "All Files (*);;Text Files (*.txt)") 279 if fileName: 280 self.label_saveFileName.setText(fileName) 281 282 def criticalMessage(self): 283 # reply = QMessageBox.StandardButton() 284 MESSAGE = "批评!" 285 reply = QMessageBox.critical(self, 286 "QMessageBox.critical()", 287 MESSAGE, 288 QMessageBox.Abort | QMessageBox.Retry | QMessageBox.Ignore) 289 if reply == QMessageBox.Abort: 290 self.label_critical.setText("Abort") 291 elif reply == QMessageBox.Retry: 292 self.label_critical.setText("Retry") 293 else: 294 self.label_critical.setText("Ignore") 295 296 def informationMessage(self): 297 MESSAGE = "信息" 298 reply = QMessageBox.information(self, "QMessageBox.information()", MESSAGE) 299 if reply == QMessageBox.Ok: 300 self.label_information.setText("OK") 301 else: 302 self.label_information.setText("Escape") 303 304 def questionMessage(self): 305 MESSAGE = "疑问" 306 reply = QMessageBox.question(self, "QMessageBox.question()", 307 MESSAGE, 308 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 309 if reply == QMessageBox.Yes: 310 self.label_question.setText("Yes") 311 elif reply == QMessageBox.No: 312 self.label_question.setText("No") 313 else: 314 self.label_question.setText("Cancel") 315 316 def warningMessage(self): 317 MESSAGE = "警告文本" 318 msgBox = QMessageBox(QMessageBox.Warning, 319 "QMessageBox.warning()", 320 MESSAGE, 321 QMessageBox.Retry | QMessageBox.Discard | QMessageBox.Cancel, 322 self) 323 msgBox.setDetailedText("详细信息。。。") 324 # msgBox.addButton("Save &Again", QMessageBox.AcceptRole) 325 # msgBox.addButton("&Continue", QMessageBox.RejectRole) 326 if msgBox.exec() == QMessageBox.AcceptRole: 327 self.label_warning.setText("Retry") 328 else: 329 self.label_warning.setText("Abort") 330 331 def errorMessage(self): 332 self.errorMessageDialog.showMessage( 333 "This dialog shows and remembers error messages. " 334 "If the checkbox is checked (as it is by default), " 335 "the shown message will be shown again, " 336 "but if the user unchecks the box the message " 337 "will not appear again if QErrorMessage.showMessage() " 338 "is called with the same message.") 339 self.label_error.setText("If the box is unchecked, the message " 340 "won't appear again.") 341 342 343 app = QApplication(sys.argv) 344 form = StandardDialog() 345 form.show() 346 app.exec_()
https://blog.csdn.net/jia666666/article/details/81560756
https://www.jb51.net/article/134636.htm
Qt设计师代码:
1 from PyQt5 import QtCore, QtGui, QtWidgets 2 3 class Ui_Dialog(object): 4 def setupUi(self, Dialog): 5 Dialog.setObjectName("Dialog") 6 Dialog.resize(474, 423) 7 Dialog.setSizeGripEnabled(True) 8 self.verticalLayout_2 = QtWidgets.QVBoxLayout(Dialog) 9 self.verticalLayout_2.setObjectName("verticalLayout_2") 10 self.toolBox = QtWidgets.QToolBox(Dialog) 11 self.toolBox.setObjectName("toolBox") 12 self.page = QtWidgets.QWidget() 13 self.page.setGeometry(QtCore.QRect(0, 0, 456, 275)) 14 self.page.setObjectName("page") 15 self.verticalLayout = QtWidgets.QVBoxLayout(self.page) 16 self.verticalLayout.setObjectName("verticalLayout") 17 self.frame = QtWidgets.QFrame(self.page) 18 self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel) 19 self.frame.setFrameShadow(QtWidgets.QFrame.Raised) 20 self.frame.setObjectName("frame") 21 self.gridLayout = QtWidgets.QGridLayout(self.frame) 22 self.gridLayout.setObjectName("gridLayout") 23 self.pushButton_3 = QtWidgets.QPushButton(self.frame) 24 self.pushButton_3.setObjectName("pushButton_3") 25 self.gridLayout.addWidget(self.pushButton_3, 2, 0, 1, 1) 26 self.label = QtWidgets.QLabel(self.frame) 27 self.label.setFrameShape(QtWidgets.QFrame.Panel) 28 self.label.setFrameShadow(QtWidgets.QFrame.Sunken) 29 self.label.setText("") 30 self.label.setObjectName("label") 31 self.gridLayout.addWidget(self.label, 0, 2, 1, 1) 32 self.pushButton_2 = QtWidgets.QPushButton(self.frame) 33 self.pushButton_2.setObjectName("pushButton_2") 34 self.gridLayout.addWidget(self.pushButton_2, 1, 0, 1, 1) 35 self.pushButton = QtWidgets.QPushButton(self.frame) 36 self.pushButton.setObjectName("pushButton") 37 self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1) 38 self.pushButton_4 = QtWidgets.QPushButton(self.frame) 39 self.pushButton_4.setObjectName("pushButton_4") 40 self.gridLayout.addWidget(self.pushButton_4, 3, 0, 1, 1) 41 self.pushButton_5 = QtWidgets.QPushButton(self.frame) 42 self.pushButton_5.setObjectName("pushButton_5") 43 self.gridLayout.addWidget(self.pushButton_5, 4, 0, 1, 1) 44 self.label_2 = QtWidgets.QLabel(self.frame) 45 self.label_2.setFrameShape(QtWidgets.QFrame.Panel) 46 self.label_2.setFrameShadow(QtWidgets.QFrame.Sunken) 47 self.label_2.setText("") 48 self.label_2.setObjectName("label_2") 49 self.gridLayout.addWidget(self.label_2, 1, 2, 1, 1) 50 self.label_4 = QtWidgets.QLabel(self.frame) 51 self.label_4.setFrameShape(QtWidgets.QFrame.Panel) 52 self.label_4.setFrameShadow(QtWidgets.QFrame.Sunken) 53 self.label_4.setText("") 54 self.label_4.setObjectName("label_4") 55 self.gridLayout.addWidget(self.label_4, 3, 2, 1, 1) 56 self.label_5 = QtWidgets.QLabel(self.frame) 57 self.label_5.setFrameShape(QtWidgets.QFrame.Panel) 58 self.label_5.setFrameShadow(QtWidgets.QFrame.Sunken) 59 self.label_5.setText("") 60 self.label_5.setObjectName("label_5") 61 self.gridLayout.addWidget(self.label_5, 4, 2, 1, 1) 62 self.label_3 = QtWidgets.QLabel(self.frame) 63 self.label_3.setFrameShape(QtWidgets.QFrame.Panel) 64 self.label_3.setFrameShadow(QtWidgets.QFrame.Sunken) 65 self.label_3.setText("") 66 self.label_3.setObjectName("label_3") 67 self.gridLayout.addWidget(self.label_3, 2, 2, 1, 1) 68 self.verticalLayout.addWidget(self.frame) 69 self.toolBox.addItem(self.page, "") 70 self.page_2 = QtWidgets.QWidget() 71 self.page_2.setGeometry(QtCore.QRect(0, 0, 456, 275)) 72 self.page_2.setObjectName("page_2") 73 self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.page_2) 74 self.verticalLayout_3.setObjectName("verticalLayout_3") 75 self.frame_2 = QtWidgets.QFrame(self.page_2) 76 self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel) 77 self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised) 78 self.frame_2.setObjectName("frame_2") 79 self.gridLayout_2 = QtWidgets.QGridLayout(self.frame_2) 80 self.gridLayout_2.setObjectName("gridLayout_2") 81 self.pushButton_6 = QtWidgets.QPushButton(self.frame_2) 82 self.pushButton_6.setObjectName("pushButton_6") 83 self.gridLayout_2.addWidget(self.pushButton_6, 0, 0, 1, 1) 84 self.label_6 = QtWidgets.QLabel(self.frame_2) 85 self.label_6.setFrameShape(QtWidgets.QFrame.Panel) 86 self.label_6.setFrameShadow(QtWidgets.QFrame.Sunken) 87 self.label_6.setText("") 88 self.label_6.setObjectName("label_6") 89 self.gridLayout_2.addWidget(self.label_6, 0, 1, 1, 1) 90 self.verticalLayout_3.addWidget(self.frame_2) 91 self.toolBox.addItem(self.page_2, "") 92 self.page_3 = QtWidgets.QWidget() 93 self.page_3.setObjectName("page_3") 94 self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.page_3) 95 self.verticalLayout_4.setObjectName("verticalLayout_4") 96 self.frame_3 = QtWidgets.QFrame(self.page_3) 97 self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel) 98 self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised) 99 self.frame_3.setObjectName("frame_3") 100 self.gridLayout_3 = QtWidgets.QGridLayout(self.frame_3) 101 self.gridLayout_3.setObjectName("gridLayout_3") 102 self.label_7 = QtWidgets.QLabel(self.frame_3) 103 self.label_7.setFrameShape(QtWidgets.QFrame.Panel) 104 self.label_7.setFrameShadow(QtWidgets.QFrame.Sunken) 105 self.label_7.setText("") 106 self.label_7.setObjectName("label_7") 107 self.gridLayout_3.addWidget(self.label_7, 0, 1, 1, 1) 108 self.pushButton_7 = QtWidgets.QPushButton(self.frame_3) 109 self.pushButton_7.setObjectName("pushButton_7") 110 self.gridLayout_3.addWidget(self.pushButton_7, 0, 0, 1, 1) 111 self.verticalLayout_4.addWidget(self.frame_3) 112 self.toolBox.addItem(self.page_3, "") 113 self.page_4 = QtWidgets.QWidget() 114 self.page_4.setObjectName("page_4") 115 self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.page_4) 116 self.verticalLayout_5.setObjectName("verticalLayout_5") 117 self.frame_4 = QtWidgets.QFrame(self.page_4) 118 self.frame_4.setFrameShape(QtWidgets.QFrame.StyledPanel) 119 self.frame_4.setFrameShadow(QtWidgets.QFrame.Raised) 120 self.frame_4.setObjectName("frame_4") 121 self.gridLayout_4 = QtWidgets.QGridLayout(self.frame_4) 122 self.gridLayout_4.setObjectName("gridLayout_4") 123 self.label_8 = QtWidgets.QLabel(self.frame_4) 124 self.label_8.setFrameShape(QtWidgets.QFrame.Panel) 125 self.label_8.setFrameShadow(QtWidgets.QFrame.Sunken) 126 self.label_8.setText("") 127 self.label_8.setObjectName("label_8") 128 self.gridLayout_4.addWidget(self.label_8, 0, 1, 1, 1) 129 self.pushButton_8 = QtWidgets.QPushButton(self.frame_4) 130 self.pushButton_8.setObjectName("pushButton_8") 131 self.gridLayout_4.addWidget(self.pushButton_8, 0, 0, 1, 1) 132 self.pushButton_9 = QtWidgets.QPushButton(self.frame_4) 133 self.pushButton_9.setObjectName("pushButton_9") 134 self.gridLayout_4.addWidget(self.pushButton_9, 1, 0, 1, 1) 135 self.label_10 = QtWidgets.QLabel(self.frame_4) 136 self.label_10.setFrameShape(QtWidgets.QFrame.Panel) 137 self.label_10.setFrameShadow(QtWidgets.QFrame.Sunken) 138 self.label_10.setText("") 139 self.label_10.setObjectName("label_10") 140 self.gridLayout_4.addWidget(self.label_10, 2, 1, 1, 1) 141 self.label_9 = QtWidgets.QLabel(self.frame_4) 142 self.label_9.setFrameShape(QtWidgets.QFrame.Panel) 143 self.label_9.setFrameShadow(QtWidgets.QFrame.Sunken) 144 self.label_9.setText("") 145 self.label_9.setObjectName("label_9") 146 self.gridLayout_4.addWidget(self.label_9, 1, 1, 1, 1) 147 self.pushButton_10 = QtWidgets.QPushButton(self.frame_4) 148 self.pushButton_10.setObjectName("pushButton_10") 149 self.gridLayout_4.addWidget(self.pushButton_10, 2, 0, 1, 1) 150 self.pushButton_11 = QtWidgets.QPushButton(self.frame_4) 151 self.pushButton_11.setObjectName("pushButton_11") 152 self.gridLayout_4.addWidget(self.pushButton_11, 3, 0, 1, 1) 153 self.label_11 = QtWidgets.QLabel(self.frame_4) 154 self.label_11.setFrameShape(QtWidgets.QFrame.Panel) 155 self.label_11.setFrameShadow(QtWidgets.QFrame.Sunken) 156 self.label_11.setText("") 157 self.label_11.setObjectName("label_11") 158 self.gridLayout_4.addWidget(self.label_11, 3, 1, 1, 1) 159 self.verticalLayout_5.addWidget(self.frame_4) 160 self.toolBox.addItem(self.page_4, "") 161 self.page_5 = QtWidgets.QWidget() 162 self.page_5.setObjectName("page_5") 163 self.verticalLayout_6 = QtWidgets.QVBoxLayout(self.page_5) 164 self.verticalLayout_6.setObjectName("verticalLayout_6") 165 self.frame_5 = QtWidgets.QFrame(self.page_5) 166 self.frame_5.setFrameShape(QtWidgets.QFrame.StyledPanel) 167 self.frame_5.setFrameShadow(QtWidgets.QFrame.Raised) 168 self.frame_5.setObjectName("frame_5") 169 self.gridLayout_5 = QtWidgets.QGridLayout(self.frame_5) 170 self.gridLayout_5.setObjectName("gridLayout_5") 171 self.pushButton_12 = QtWidgets.QPushButton(self.frame_5) 172 self.pushButton_12.setObjectName("pushButton_12") 173 self.gridLayout_5.addWidget(self.pushButton_12, 0, 0, 1, 1) 174 self.label_12 = QtWidgets.QLabel(self.frame_5) 175 self.label_12.setFrameShape(QtWidgets.QFrame.Panel) 176 self.label_12.setFrameShadow(QtWidgets.QFrame.Sunken) 177 self.label_12.setText("") 178 self.label_12.setObjectName("label_12") 179 self.gridLayout_5.addWidget(self.label_12, 0, 1, 1, 1) 180 self.pushButton_13 = QtWidgets.QPushButton(self.frame_5) 181 self.pushButton_13.setObjectName("pushButton_13") 182 self.gridLayout_5.addWidget(self.pushButton_13, 1, 0, 1, 1) 183 self.label_13 = QtWidgets.QLabel(self.frame_5) 184 self.label_13.setFrameShape(QtWidgets.QFrame.Panel) 185 self.label_13.setFrameShadow(QtWidgets.QFrame.Sunken) 186 self.label_13.setText("") 187 self.label_13.setObjectName("label_13") 188 self.gridLayout_5.addWidget(self.label_13, 1, 1, 1, 1) 189 self.pushButton_16 = QtWidgets.QPushButton(self.frame_5) 190 self.pushButton_16.setObjectName("pushButton_16") 191 self.gridLayout_5.addWidget(self.pushButton_16, 4, 0, 1, 1) 192 self.pushButton_15 = QtWidgets.QPushButton(self.frame_5) 193 self.pushButton_15.setObjectName("pushButton_15") 194 self.gridLayout_5.addWidget(self.pushButton_15, 3, 0, 1, 1) 195 self.pushButton_14 = QtWidgets.QPushButton(self.frame_5) 196 self.pushButton_14.setObjectName("pushButton_14") 197 self.gridLayout_5.addWidget(self.pushButton_14, 2, 0, 1, 1) 198 self.label_14 = QtWidgets.QLabel(self.frame_5) 199 self.label_14.setFrameShape(QtWidgets.QFrame.Panel) 200 self.label_14.setFrameShadow(QtWidgets.QFrame.Sunken) 201 self.label_14.setText("") 202 self.label_14.setObjectName("label_14") 203 self.gridLayout_5.addWidget(self.label_14, 2, 1, 1, 1) 204 self.label_15 = QtWidgets.QLabel(self.frame_5) 205 self.label_15.setFrameShape(QtWidgets.QFrame.Panel) 206 self.label_15.setFrameShadow(QtWidgets.QFrame.Sunken) 207 self.label_15.setText("") 208 self.label_15.setObjectName("label_15") 209 self.gridLayout_5.addWidget(self.label_15, 3, 1, 1, 1) 210 self.label_16 = QtWidgets.QLabel(self.frame_5) 211 self.label_16.setFrameShape(QtWidgets.QFrame.Panel) 212 self.label_16.setFrameShadow(QtWidgets.QFrame.Sunken) 213 self.label_16.setText("") 214 self.label_16.setObjectName("label_16") 215 self.gridLayout_5.addWidget(self.label_16, 4, 1, 1, 1) 216 self.verticalLayout_6.addWidget(self.frame_5) 217 self.toolBox.addItem(self.page_5, "") 218 self.verticalLayout_2.addWidget(self.toolBox) 219 220 self.retranslateUi(Dialog) 221 self.toolBox.setCurrentIndex(4) 222 QtCore.QMetaObject.connectSlotsByName(Dialog) 223 224 def retranslateUi(self, Dialog): 225 _translate = QtCore.QCoreApplication.translate 226 Dialog.setWindowTitle(_translate("Dialog", "Dialog")) 227 self.pushButton_3.setText(_translate("Dialog", "QInputDialog.getIte&m()")) 228 self.pushButton_2.setText(_translate("Dialog", "QInputDialog.get&Double()")) 229 self.pushButton.setText(_translate("Dialog", "QInputDialog.get&Int()")) 230 self.pushButton_4.setText(_translate("Dialog", "QInputDialog.get&Text()")) 231 self.pushButton_5.setText(_translate("Dialog", "QInputDialog.get&MultiLineText()")) 232 self.toolBox.setItemText(self.toolBox.indexOf(self.page), _translate("Dialog", "Input Dialog")) 233 self.pushButton_6.setText(_translate("Dialog", "QColorDialog.get&Color()")) 234 self.toolBox.setItemText(self.toolBox.indexOf(self.page_2), _translate("Dialog", "Color Dialog")) 235 self.pushButton_7.setText(_translate("Dialog", "QFontDialog.get&Font()")) 236 self.toolBox.setItemText(self.toolBox.indexOf(self.page_3), _translate("Dialog", "Font Dialog")) 237 self.pushButton_8.setText(_translate("Dialog", "QFileDialog.getE&xistingDirectory()")) 238 self.pushButton_9.setText(_translate("Dialog", "\"QFileDialog.get&OpenFileName()")) 239 self.pushButton_10.setText(_translate("Dialog", "QFileDialog.&getOpenFileNames()")) 240 self.pushButton_11.setText(_translate("Dialog", "QFileDialog.get&SaveFileName()")) 241 self.toolBox.setItemText(self.toolBox.indexOf(self.page_4), _translate("Dialog", "File Dialogs")) 242 self.pushButton_12.setText(_translate("Dialog", "QMessageBox.critica&l()")) 243 self.pushButton_13.setText(_translate("Dialog", "QMessageBox.i&nformation()")) 244 self.pushButton_16.setText(_translate("Dialog", "QErrorMessage.showM&essage()")) 245 self.pushButton_15.setText(_translate("Dialog", "QMessageBox.&warning()")) 246 self.pushButton_14.setText(_translate("Dialog", "QQMessageBox.&question()")) 247 self.toolBox.setItemText(self.toolBox.indexOf(self.page_5), _translate("Dialog", "Message Boxes")) 248 249 250 if __name__ == "__main__": 251 import sys 252 app = QtWidgets.QApplication(sys.argv) 253 Dialog = QtWidgets.QDialog() 254 ui = Ui_Dialog() 255 ui.setupUi(Dialog) 256 Dialog.show() 257 sys.exit(app.exec_())
1 # -*- coding: utf-8 -*- 2 from PyQt5.QtCore import pyqtSlot,QDir,Qt 3 from PyQt5.QtGui import QPixmap,QFont,QIcon,QPalette 4 from PyQt5.QtWidgets import QDialog, QApplication, QInputDialog,QErrorMessage,QLineEdit,QColorDialog,QFontDialog,QFileDialog,QMessageBox 5 from Ui_dialogs import Ui_Dialog 6 import sys 7 8 9 class Dialog(QDialog, Ui_Dialog): 10 def __init__(self, parent=None): 11 super(Dialog, self).__init__(parent) 12 self.setupUi(self) 13 self.errorMessageDialog = QErrorMessage(self) 14 15 self.slotCustom() 16 17 #自定义一个对话框 18 def slotCustom(self): 19 customMsgBox = QMessageBox(self) 20 customMsgBox.setWindowTitle("Custom message box") 21 lockButton = customMsgBox.addButton(self.tr("锁定"), 22 QMessageBox.ActionRole) 23 unlockButton = customMsgBox.addButton(self.tr("解锁"), 24 QMessageBox.ActionRole) 25 cancelButton = customMsgBox.addButton("cancel", QMessageBox.ActionRole) 26 27 customMsgBox.setText(self.tr("这是一个自定义消息框!")) 28 customMsgBox.exec_() 29 30 button = customMsgBox.clickedButton() 31 if button == lockButton: 32 self.label.setText("Custom MessageBox/Lock") 33 elif button == unlockButton: 34 self.label.setText("Custom MessageBox/Unlock") 35 elif button == cancelButton: 36 self.label.setText("Custom MessageBox/Cancel") 37 38 39 #输入对话框 取整数 40 41 @pyqtSlot() 42 def on_pushButton_clicked(self): 43 intNum , ok = QInputDialog.getInt(self,"QInputDialog.getInteger()", "Percentage:",value=30,min=0,max=150,step=2) #数字输入默认设为30,范围是0,,10,梯度是2 44 if ok: 45 self.label.setText(str(intNum)) 46 # 输入对话框 取实数 47 @pyqtSlot() 48 def on_pushButton_2_clicked(self): 49 doubleNum , ok = QInputDialog.getDouble(self,"QInputDialog.getDouble()", "Amount:", 37.56, -100, 100, 2) #默认输入设为37.56,范围 -100,100,保留2位小数 50 if ok: 51 self.label_2.setText(str(doubleNum)) 52 #输入对话框 取列表项 53 @pyqtSlot() 54 def on_pushButton_3_clicked(self): 55 items = ["Spring", "Summer", "Fall", "Winter"] 56 item, ok = QInputDialog.getItem(self, "QInputDialog.getItem()", "Season:", items, 1, False) #默认是items[1] 57 if ok and item: 58 self.label_3.setText(item) 59 ## 输入对话框 取文本 60 @pyqtSlot() 61 def on_pushButton_4_clicked(self): 62 text, ok = QInputDialog.getText(self, "QInputDialog.getText()", "User name:", QLineEdit.Normal,QDir.home().dirName()) #QLineEdit.Normal 设置文本框输入格式:正常显示字符 63 if ok and text: 64 self.label_4.setText(text) 65 66 # 输入对话框 取多行文本 67 @pyqtSlot() 68 def on_pushButton_5_clicked(self): 69 text, ok = QInputDialog.getMultiLineText(self, "QInputDialog.getMultiLineText()", "Address:", 70 "John Doe\nFreedom Street") 71 if ok and text: 72 self.label_5.setText(text) 73 74 # 颜色对话框QColorDialog 取颜色 75 @pyqtSlot() 76 def on_pushButton_6_clicked(self): 77 # options = QColorDialog.ColorDialogOptions(QFlag.QFlag(colorDialogOptionsWidget.value())) 78 color = QColorDialog.getColor(Qt.green, self, "Select Color") #默认绿色 79 80 if color.isValid(): 81 self.label_6.setText(color.name()) 82 self.label_6.setPalette(QPalette(color)) #引入QPalette 调色板,将选中的颜色设置为背景色 83 self.label_6.setAutoFillBackground(True) 84 85 # 字体对话框 取字体 86 @pyqtSlot() 87 def on_pushButton_7_clicked(self): 88 # options = QFontDialog.FontDialogOptions(QFlag(fontDialogOptionsWidget.value())) 89 # font, ok = QFontDialog.getFont(ok, QFont(self.label_font.text()), self, "Select Font",options) 90 font, ok = QFontDialog.getFont() 91 print() 92 if ok: 93 self.label_7.setText(font.key()) 94 self.label_7.setFont(font) 95 96 # 目录对话框 取目录 97 @pyqtSlot() 98 def on_pushButton_8_clicked(self): 99 # options = QFileDialog.Options(QFlag(fileDialogOptionsWidget->value())) 100 # options |= QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly 101 directory = QFileDialog.getExistingDirectory(self, 102 "QFileDialog.getExistingDirectory()", 103 self.label_8.text()) 104 if directory: 105 self.label_8.setText(directory) 106 107 # 打开文件对话框 取文件名 108 @pyqtSlot() 109 def on_pushButton_9_clicked(self): 110 # options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 111 # selectedFilter 112 fileName, filetype = QFileDialog.getOpenFileName(self, 113 "QFileDialog.getOpenFileName()", 114 self.label_9.text(), 115 "All Files (*);;Text Files (*.txt)") 116 if fileName: 117 self.label_9.setText(fileName) 118 ## 打开文件对话框 取多个文件名 119 @pyqtSlot() 120 def on_pushButton_10_clicked(self): 121 # options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 122 # selectedFilter 123 openFilesPath = "D:/" 124 files, ok = QFileDialog.getOpenFileNames(self, 125 "QFileDialog.getOpenFileNames()", 126 openFilesPath, 127 "All Files (*);;Text Files (*.txt)") 128 129 if len(files): 130 self.label_10.setText(", ".join(files)) 131 132 # 保存文件对话框 取文件名 133 @pyqtSlot() 134 def on_pushButton_11_clicked(self): 135 # options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value())) 136 # selectedFilter 137 fileName, ok = QFileDialog.getSaveFileName(self, 138 "QFileDialog.getSaveFileName()", 139 self.label_11.text(), 140 "All Files (*);;Text Files (*.txt)") 141 if fileName: 142 self.label_11.setText(fileName) 143 144 #警告对话框 145 @pyqtSlot() 146 def on_pushButton_12_clicked(self): 147 # reply = QMessageBox.StandardButton() 148 reply = QMessageBox.critical(self, 149 "QMessageBox.critical()", 150 "批评!", 151 QMessageBox.Abort | QMessageBox.Retry | QMessageBox.Ignore) 152 if reply == QMessageBox.Abort: 153 self.label_12.setText("Abort") 154 elif reply == QMessageBox.Retry: 155 self.label_12.setText("Retry") 156 else: 157 self.label_12.setText("Ignore") 158 159 #提示对话框 160 @pyqtSlot() 161 def on_pushButton_13_clicked(self): 162 reply = QMessageBox.information(self, "QMessageBox.information()", "信息") 163 if reply == QMessageBox.Ok: 164 self.label_13.setText("OK") 165 else: 166 self.label_13.setText("Escape") 167 168 #询问对话框 169 @pyqtSlot() 170 def on_pushButton_14_clicked(self): 171 MESSAGE = "疑问" 172 reply = QMessageBox.question(self, "QMessageBox.question()", 173 MESSAGE, 174 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 175 if reply == QMessageBox.Yes: 176 self.label_14.setText("Yes") 177 elif reply == QMessageBox.No: 178 self.label_14.setText("No") 179 else: 180 self.label_14.setText("Cancel") 181 182 #严重警告对话框 183 @pyqtSlot() 184 def on_pushButton_15_clicked(self): 185 MESSAGE = "警告文本" 186 msgBox = QMessageBox(QMessageBox.Warning, 187 "QMessageBox.warning()", 188 MESSAGE, 189 QMessageBox.Retry | QMessageBox.Discard | QMessageBox.Cancel, 190 self) 191 msgBox.setDetailedText("详细信息。。。") 192 # msgBox.addButton("Save &Again", QMessageBox.AcceptRole) 193 # msgBox.addButton("&Continue", QMessageBox.RejectRole) 194 if msgBox.exec() == QMessageBox.AcceptRole: 195 self.label_15.setText("Retry") 196 else: 197 self.label_15.setText("Abort") 198 199 #错误信息对话框 200 @pyqtSlot() 201 def on_pushButton_16_clicked(self): 202 self.errorMessageDialog.showMessage( 203 "This dialog shows and remembers error messages. " 204 "If the checkbox is checked (as it is by default), " 205 "the shown message will be shown again, " 206 "but if the user unchecks the box the message " 207 "will not appear again if QErrorMessage.showMessage() " 208 "is called with the same message.") 209 self.label_16.setText("If the box is unchecked, the message " 210 "won't appear again.") 211 212 if __name__ == "__main__": 213 app = QApplication(sys.argv) 214 ui = Dialog() 215 ui.show() 216 sys.exit(app.exec_()) 217 218
纯代码完成:
# -*- coding: utf-8 -*- from PyQt5.QtCore import pyqtSlot, QDir, Qt from PyQt5.QtGui import QPixmap, QFont, QIcon, QPalette from PyQt5.QtWidgets import QDialog, QWidget,QApplication,QFrame,QGridLayout,QPushButton,QLabel,QVBoxLayout,\ QHBoxLayout,QInputDialog, QErrorMessage, QLineEdit, QColorDialog, QFontDialog,QFileDialog, QMessageBox,QToolBox import sys class Demo(QWidget): def __init__(self, parent=None): super(Demo, self).__init__(parent) self.initUi() self.layout_init() self.buttonClicked() def initUi(self): self.setGeometry(300,300,400,360) self.setWindowTitle("这是一个QToolBox练习") # self.showFullScreen() def layout_init(self): frameStyle = QFrame.Panel | QFrame.Sunken self.horizontaLayout = QHBoxLayout(self) self.toolBox = QToolBox(self) self.horizontaLayout.addWidget(self.toolBox) self.page_1 = QWidget() self.toolBox.addItem(self.page_1, "Input Dialog输入抽屉框") # self.toolBox.setItemText(0, "Input Dialog输入抽屉框") self.frame_1 = QFrame(self.page_1) self.verticalLayout_1 = QVBoxLayout(self.page_1) #在page_1 中是 垂直布局 self.verticalLayout_1.addWidget(self.frame_1) #在page_1 中有一个frame self.gridLayout = QGridLayout(self.frame_1) self.pushButton_11 = QPushButton(self.frame_1) #在网格布局中添加一个按钮pushButton_1 self.pushButton_11.setText("QInputDialog.getInt()") self.pushButton_12 = QPushButton(self.frame_1) self.pushButton_12.setText("QInputDialog.get&Double()") self.pushButton_13 = QPushButton(self.frame_1) self.pushButton_13.setText("QInputDialog.getIte&m()") self.pushButton_14 = QPushButton(self.frame_1) self.pushButton_14.setText("QInputDialog.get&Text()") self.pushButton_15 = QPushButton(self.frame_1) self.pushButton_15.setText("QInputDialog.get&MultiLineText()") self.label_11 = QLabel(self.frame_1) ##在网格布局中添加一个标签label_1 self.label_11.setFrameStyle(frameStyle) #给标签设置样式 self.label_12 = QLabel(self.frame_1) self.label_12.setFrameStyle(frameStyle) self.label_13 = QLabel(self.frame_1) self.label_13.setFrameStyle(frameStyle) self.label_14 = QLabel(self.frame_1) self.label_14.setFrameStyle(frameStyle) self.label_15 = QLabel(self.frame_1) self.label_15.setFrameStyle(frameStyle) self.gridLayout.addWidget(self.pushButton_11,0,0,1,1) self.gridLayout.addWidget(self.label_11,0,1,1,2) self.gridLayout.addWidget(self.pushButton_12, 1, 0, 1, 1) self.gridLayout.addWidget(self.label_12, 1, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_13, 2, 0, 1, 1) self.gridLayout.addWidget(self.label_13, 2, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_14, 3, 0, 1, 1) self.gridLayout.addWidget(self.label_14, 3, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_15, 4, 0, 1, 1) self.gridLayout.addWidget(self.label_15, 4, 1, 1, 2) self.page_2 = QWidget() self.toolBox.addItem(self.page_2, "颜色&字体抽屉框") # self.toolBox.setItemText(1, "颜色&字体抽屉框") self.frame_2 = QFrame(self.page_2) self.verticalLayout_2 = QVBoxLayout(self.page_2) self.verticalLayout_2.addWidget(self.frame_2) self.gridLayout = QGridLayout(self.frame_2) self.pushButton_21 = QPushButton(self.frame_2) self.pushButton_21.setText("QColorDialog.get&Color()") self.label_21 = QLabel(self.frame_2) self.label_21.setFrameStyle(frameStyle) self.pushButton_22 = QPushButton(self.frame_2) self.pushButton_22.setText("QFontDialog.get&Font()") self.label_22 = QLabel(self.frame_2) self.label_22.setFrameStyle(frameStyle) self.gridLayout.addWidget(self.pushButton_21, 0, 0, 1, 1) self.gridLayout.addWidget(self.label_21, 0, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_22, 1, 0, 1, 1) self.gridLayout.addWidget(self.label_22, 1, 1, 1, 2) self.page_3 = QWidget() self.toolBox.addItem(self.page_3, "文件抽屉框") # self.toolBox.setItemText(2, "文件抽屉框") self.frame = QFrame(self.page_3) self.verticalLayout_3 = QVBoxLayout(self.page_3) self.verticalLayout_3.addWidget(self.frame) self.gridLayout = QGridLayout(self.frame) self.pushButton_31 = QPushButton(self.frame) self.pushButton_31.setText("QFileDialog.getE&xistingDirectory()") self.label_31 = QLabel(self.frame) self.label_31.setFrameStyle(frameStyle) self.pushButton_32 = QPushButton(self.frame) self.pushButton_32.setText("QFileDialog.get&OpenFileName()") self.label_32 = QLabel(self.frame) self.label_32.setFrameStyle(frameStyle) self.pushButton_33 = QPushButton(self.frame) self.pushButton_33.setText("QFileDialog.&getOpenFileNames()") self.label_33 = QLabel(self.frame) self.label_33.setFrameStyle(frameStyle) self.pushButton_34 = QPushButton(self.frame) self.pushButton_34.setText("QFileDialog.get&SaveFileName()") self.label_34 = QLabel(self.frame) self.label_34.setFrameStyle(frameStyle) self.gridLayout.addWidget(self.pushButton_31, 0, 0, 1, 1) self.gridLayout.addWidget(self.label_31, 0, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_32, 1, 0, 1, 1) self.gridLayout.addWidget(self.label_32, 1, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_33, 2, 0, 1, 1) self.gridLayout.addWidget(self.label_33, 2, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_34, 3, 0, 1, 1) self.gridLayout.addWidget(self.label_34, 3, 1, 1, 2) self.page_4 = QWidget() self.toolBox.addItem(self.page_4, "Message信息提示抽屉框") # self.toolBox.setItemText(3, "Message信息提示抽屉框") #第4个框 标题是 "Message信息提示抽屉框" self.frame = QFrame(self.page_4) self.verticalLayout_4 = QVBoxLayout(self.page_4) self.verticalLayout_4.addWidget(self.frame) self.gridLayout = QGridLayout(self.frame) self.pushButton_41 = QPushButton(self.frame) self.pushButton_41.setText("QColorDialog.get&Color()") self.label_41 = QLabel(self.frame) self.label_41.setFrameStyle(frameStyle) self.pushButton_42 = QPushButton(self.frame) self.pushButton_42.setText("QFontDialog.get&Font()") self.label_42 = QLabel(self.frame) self.label_42.setFrameStyle(frameStyle) self.pushButton_43 = QPushButton(self.frame) self.pushButton_43.setText("QFontDialog.get&Font()") self.label_43 = QLabel(self.frame) self.label_43.setFrameStyle(frameStyle) self.pushButton_44 = QPushButton(self.frame) self.pushButton_44.setText("QFontDialog.get&Font()") self.label_44 = QLabel(self.frame) self.label_44.setFrameStyle(frameStyle) self.pushButton_45 = QPushButton(self.frame) self.pushButton_45.setText("QFontDialog.get&Font()") self.label_45 = QLabel(self.frame) self.label_45.setFrameStyle(frameStyle) self.gridLayout.addWidget(self.pushButton_41, 0, 0, 1, 1) self.gridLayout.addWidget(self.label_41, 0, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_42, 1, 0, 1, 1) self.gridLayout.addWidget(self.label_42, 1, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_43, 2, 0, 1, 1) self.gridLayout.addWidget(self.label_43, 2, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_44, 3, 0, 1, 1) self.gridLayout.addWidget(self.label_44, 3, 1, 1, 2) self.gridLayout.addWidget(self.pushButton_45, 4, 0, 1, 1) self.gridLayout.addWidget(self.label_45, 4, 1, 1, 2) def buttonClicked(self): self.pushButton_11.clicked.connect(self.inputDialog_getInt) self.pushButton_12.clicked.connect(self.inputDialog_getDouble) self.pushButton_13.clicked.connect(self.inputDialog_getItem) self.pushButton_14.clicked.connect(self.inputDialog_getText) self.pushButton_15.clicked.connect(self.inputDialog_getMultiLineText) self.pushButton_21.clicked.connect(self.colorDialog_getColor) self.pushButton_22.clicked.connect(self.fontDialog_getFont) def inputDialog_getInt(self): intNum ,ok = QInputDialog.getInt(self,"整数输入框","输入整数:",10,0,100,2) if ok: self.label_11.setText(str(intNum)) def inputDialog_getDouble(self): doubleNum , ok = QInputDialog.getDouble(self,"实数输入框!","输入实数:",3.14,-20.0,20.0,2) if ok: self.label_12.setText(str(doubleNum)) def inputDialog_getItem(self): items = ["China","American","British","Russia","India"] item , ok = QInputDialog.getItem(self,"项目对话框!","选择项目:",items,0) if ok and item: self.label_13.setText(item) def inputDialog_getText(self): text , ok = QInputDialog.getText(self,"单行文本输入对话框!","请输入单行文本:",echo=QLineEdit.Normal ,text="这是默认的文本。。。") if ok and text: self.label_14.setText(text) def inputDialog_getMultiLineText(self): multi_text , ok = QInputDialog.getMultiLineText(self,"多行文本输入对话框!","在此输入多行文本:","这是默认的文本。。。") if ok and multi_text: self.label_15.setText(multi_text) def colorDialog_getColor(self): color = QColorDialog.getColor(Qt.blue,self,"选择颜色!") if color: self.label_21.setText(color.name()) self.label_21.setPalette(QPalette(color)) self.label_21.setAutoFillBackground(True) def fontDialog_getFont(self): font,ok = QFontDialog.getFont() if ok: self.label_22.setText(font.key()) self.label_22.setFont(font) if __name__ == "__main__": app = QApplication(sys.argv) ui = Demo() ui.show() sys.exit(app.exec_())