日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PyQt4编程之模态与非模态对话框(一)

發布時間:2025/3/19 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyQt4编程之模态与非模态对话框(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

模態對話框(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 Pointdialog = NumberFormatDlg(self.format,self)if dialog.exec_():self.format = dialog.numberFormat()self.refreshTable()class NumberFormatDlg(QDialog): #the second pointdef __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() # noticegrid = 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.formatdef accept(self): #override 'accept()' methodclass ThousandsError(Exception): #inherit the class Exceptiondef __init__(self,message):Exception.__init__(self)self.message=messageclass DecimalError(Exception):def __init__(self,message):Exception.__init__(self)self.message=messagePunctuation = 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()returnexcept DecimalError, e:QMessageBox.warning(self,"D",unicode(e.message))self.decimalMarkerEdit.selectAll()self.decimalMarkerEdit.setFocus()returnself.format['thousandsseparator'] = thousandsself.format['decimalmarker'] = decimalself.format['decimalplaces'] =\self.decimalPlacesSpinBox.value()self.format["rednegatives"] =\self.redNegativesCheckBox.isChecked() #the CheckBox has 'isChecked()' which can get the vaule of the CheckBoxQDialog.accept(self)app = QApplication(sys.argv) widget = Widget() widget.show()app.exec_()
好,然后咱就按照套路分析一下。
框架講解 框架就好比一篇文章的行文思路,是寫這種代碼所必須的。 第一部分:setNumberFormat1這個函數就是來彈出格式設置對話框,并對主窗口里面的數據格式更新。可以把它看成主窗口和對話框之間的橋梁。
第二部分:第二部分就是對話框這個類的實現了。 首先是init函數,設置控件,排出布局,連接按鈕的信號和槽,前面的文章已經分析過類似的,所以也不需要多講。
然后就是accept函數了,我們這里對它進行了重載。為什么要進行重載呢??進行重載一般是因為原來的函數不能實現我們想要的功能。 我們這里想要的功能是當我們點擊“Ok”這個button的時候,對‘對話框’的各項的值進行驗證,如果符合,則返回True給exec_(),否則回到對話框。而原來的accept函數只是簡單地給exec_()返回一個True,除此之外什么都不干。(驗證按驗證的對象及其之間的關系分為窗口部件級驗證和窗體級驗證,按照時間先后分為預防式驗證和提交后驗證,這里明顯是窗口部件級驗證和提交后驗證)

實現分析
1.代碼實現部分需要注意的是"self.format =?? format.copy()",在智能對話框里面,這一句是self.format = format. 用copy的原因是我們想傳格式字典的副本,這樣當改變對話框內的格式字典的時候不會影響原來的初始字典。
2.在accept的開頭,我們創建了兩個異常類,然后用try和except捕獲異常。

下一篇結合非模態實例來講解模態與非模態的區別。



與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的PyQt4编程之模态与非模态对话框(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。