模态对话框(Modal Dialogue Box,又叫做模式对话框),是指在用户想要对对话框以外的应用程序进行操作时,必须首先对该对话框进行响应。如单击【确定】或【取消】按钮等将该对话框关闭。------------以上内容摘自360百科
对话框按照模态可以分为模态对话框和非模态对话框,按照智能程度又可以分为简易,标准和智能。大多数模态情况下,对话框包括简易和标准型的。
按照我的理解,简易型和标准型的其实基础架构差不多,只是标准型的“格式更标准”。所以先直接上一个标准型的例子来分析一下。
我们需要做好的对话框效果如下图所示。
就是在主界面点击某一个按钮,然后弹出该对话框对显示数据的格式进行设置。先上代码。
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self):
QWidget.__init__(self)
button = QPushButton(QIcon("web.png"),"click me",self)
button.move(100,100)
button.clicked.connect(self.setNumberFormat1)
self.format = dict(thousandsseparator=',',decimalmarker='.',decimalplaces=2,rednegatives=True)
self.resize(200,300)
def setNumberFormat1(self): #the first Point
dialog = NumberFormatDlg(self.format,self)
if dialog.exec_():
self.format = dialog.numberFormat()
self.refreshTable()
class NumberFormatDlg(QDialog): #the second point
def __init__(self,format,parent=None):
super(NumberFormatDlg,self).__init__(parent)
thousandsLabel = QLabel("&Thousands seperator")
self.thousandsEdit = QLineEdit(format['thousandsseparator'])
thousandsLabel.setBuddy(self.thousandsEdit)
decimalMarkerLabel = QLabel("Decimal &marker")
self.decimalMarkerEdit = QLineEdit(format["decimalmarker"])
decimalMarkerLabel.setBuddy(self.decimalMarkerEdit)
decimalPlacesLabel = QLabel("&Decimal places")
self.decimalPlacesSpinBox = QSpinBox()
decimalPlacesLabel.setBuddy(self.decimalPlacesSpinBox)
self.decimalPlacesSpinBox.setRange(0,6)
self.decimalPlacesSpinBox.setValue(format['decimalplaces'])
self.redNegativesCheckBox = QCheckBox("&Red negative numbers")
self.redNegativesCheckBox.setChecked(format['rednegatives'])
buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
self.format = format.copy() # notice
grid = QGridLayout()
grid.addWidget(thousandsLabel,0,0)
grid.addWidget(self.thousandsEdit,0,1)
grid.addWidget(decimalMarkerLabel,1,0)
grid.addWidget(self.decimalMarkerEdit,1,1)
grid.addWidget(decimalPlacesLabel,2,0)
grid.addWidget(self.decimalPlacesSpinBox,2,1)
grid.addWidget(self.redNegativesCheckBox,3,0,1,2)
grid.addWidget(buttonBox,4,0,1,2)
self.setLayout(grid)
self.connect(buttonBox.button(QDialogButtonBox.Ok),SIGNAL("clicked()"),self,SLOT("accept()")) self.connect(buttonBox,SIGNAL("rejected()"),self,SLOT("reject()"))
self.setWindowTitle("Set Number Format (Modal)")
def numberFormat(self):
return self.format
def accept(self): #override 'accept()' method
class ThousandsError(Exception): #inherit the class Exception
def __init__(self,message):
Exception.__init__(self)
self.message=message
class DecimalError(Exception):
def __init__(self,message):
Exception.__init__(self)
self.message=message
Punctuation = frozenset(" ,;.")
thousands = unicode(self.thousandsEdit.text()) decimal = unicode(self.decimalMarkerEdit.text())
try:
if len(decimal) == 0:
raise DecimalError("The decimal marker may not be empty.")
if len(thousands) > 1:
raise ThousandsError("The thousands separator may only be empty or one character.")
if len(decimal) > 1:
raise DecimalError("The decimal marker must be one character")
if thousands == decimal:
raise ThousandsError("The thousands separator and the decimal marker must be different.")
if thousands and thousands not in Punctuation: #"and not in"
raise ThousandsError("The thousands separator must be a punctuation sumbol.")
except ThousandsError, e:
QMessageBox.warning(self,"Thousands Separator", unicode(e.message)) #QMessageBox's warning can create a new 'warning widget'
self.thousandsEdit.selectAll()
self.thousandsEdit.setFocus()
return
except DecimalError, e:
QMessageBox.warning(self,"D",unicode(e.message))
self.decimalMarkerEdit.selectAll()
self.decimalMarkerEdit.setFocus()
return
self.format['thousandsseparator'] = thousands
self.format['decimalmarker'] = decimal
self.format['decimalplaces'] =\
self.decimalPlacesSpinBox.value()
self.format["rednegatives"] =\
self.redNegativesCheckBox.isChecked() #the CheckBox has 'isChecked()' which can get the vaule of the CheckBox
QDialog.accept(self)
app = QApplication(sys.argv)
widget = Widget()
widget.show()
app.exec_()