PyQt4-入门学习(2)

本文内容为个人学习时的材料整理,有许多书本上的内容,并非个人原创,但在测试时将原文编码有问题的地方进行了些需修改。水平不足,敬请谅解!


1. 对话框

1)简易对话框:对话框的调用者会把对话框中的各窗口部件全部设置为初始值,也可由对话框调用者直接获取各窗口部件的最终值。简易对话框不掌握各窗口部件中用于编辑和显示的数据。

# -*-coding: utf-8-*-
import sys
from PyQt4 import QtGui,QtCore


class PenPropertiesDlg(QtGui.QDialog):
    def __init__(self,parent=None):
        super(PenPropertiesDlg,self).__init__(parent)

        self.setWindowTitle("Pen Properties")

        # 设置组件
        widthLabel = QtGui.QLabel("&Width: ")
        # buddy表示加速键
        self.widthSpinBox = QtGui.QSpinBox()
        widthLabel.setBuddy(self.widthSpinBox)

        self.widthSpinBox.setAlignment(QtCore.Qt.AlignRight |
                                       QtCore.Qt.AlignVCenter)
        self.widthSpinBox.setRange(0,24)

        self.beveledCheckBox = QtGui.QCheckBox('&Beveled edges')

        styleLabel = QtGui.QLabel("&Style: ")

        self.styleComboBox = QtGui.QComboBox()
        styleLabel.setBuddy(self.styleComboBox)
        self.styleComboBox.addItems([
            "Solid","Dashed","Dotted","DashDotted","DashDotDotted"
        ])

        okButton = QtGui.QPushButton("&OK")
        cancelButton = QtGui.QPushButton("Cancel")

        # 进行布局
        buttonLayout = QtGui.QHBoxLayout()
        buttonLayout.addStretch()
        buttonLayout.addWidget(okButton)
        buttonLayout.addWidget(cancelButton)

        layout = QtGui.QGridLayout()
        layout.addWidget(widthLabel, 0, 0)
        layout.addWidget(self.widthSpinBox, 0, 1)
        layout.addWidget(self.beveledCheckBox, 0, 2)
        layout.addWidget(styleLabel, 1, 0)
        layout.addWidget(self.styleComboBox, 1, 1, 1, 2)
        layout.addLayout(buttonLayout, 2, 0, 1, 3)

        self.setLayout(layout)

        self.connect(okButton,QtCore.SIGNAL("clicked()"),
                     self, QtCore.SLOT("accept()"))
        self.connect(cancelButton,QtCore.SIGNAL("clicked()"),
                     self, QtCore.SLOT("reject()"))


    def setPenProperties(self):
        dialog = PenPropertiesDlg(self)
        # dialog.widthSpinBox.setValue(self.width)
        # dialog.beveledCheckBox.setChecked(self.beveled)
        # dialog.styleComboBox.setCurrentIndex(
        #     dialog.styleComboBox.findText(self.style)
        # )
        #

        if dialog.exec_():
            self.width = dialog.widthSpinBox.value()
            self.beveled = dialog.beveledCheckBox.isChecked()
            self.style = dialog.styleComboBox.currentText()
            self.updateData()


app = QtGui.QApplication(sys.argv)
pen = PenPropertiesDlg()
pen.setPenProperties()
sys.exit(app.exec_())

2)标准对话框:根据自己的初始化程序或者方法所给定的设置值来初始化各个窗口部件,且各最终值是由调用的方法或者其实例中的变量决定而不是直接通过对话框的窗口部件来决定的对话框。

# -*-coding:utf-8-*-
import sys
from PyQt4 import QtGui,QtCore

format = dict(thousandsseparator=",",
                decimalmarker=".",
                dicimalplaces=2,
                rednegatives=False)
class NumberFormatDlg(QtGui.QDialog): def __init__(self,format,parent=None): super(NumberFormatDlg,self).__init__(parent) self.format = format # 设置组件 thousandsLabel = QtGui.QLabel("&Thousands separator") self.thousandsEdit = QtGui.QLineEdit() thousandsLabel.setBuddy(self.thousandsEdit) decimalMarkerLabel = QtGui.QLabel("decimal &marker") self.decimalMarkerEdit = QtGui.QLineEdit() decimalMarkerLabel.setBuddy(self.decimalMarkerEdit) decimalPlacesLabel = QtGui.QLabel("&Decimal places") self.decimalPlacesSpinBox = QtGui.QSpinBox() decimalPlacesLabel.setBuddy(self.decimalPlacesSpinBox) self.decimalPlacesSpinBox.setRange(0,6) self.decimalPlacesSpinBox.setValue(1) self.redNegativesCheckBox = QtGui.QCheckBox("&Red negative numbers") self.redNegativesCheckBox.setChecked(False) buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel) # 设置布局 grid = QtGui.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, QtCore.SIGNAL("accepted()"), self, QtCore.SLOT("accept()")) self.connect(buttonBox, QtCore.SIGNAL("rejected()"), self, QtCore.SLOT("reject()")) self.setWindowTitle("Set Number Format (Modal)") def numberFormat(self): return self.format def accept(self): class ThousandsError(Exception): pass class DecimalError(Exception): pass Punctuation = frozenset(" ,;:.") thousands = self.thousandsEdit.text() decimal = 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 separtor and " "the decimal marker must be different.") if thousands and thousands not in Punctuation: raise ThousandsError, ("The thousands separator must be " "a punctuation symbol.") if decimal not in Punctuation: raise DecimalError, ("The decimal marker must be a " "punctuation symbol.") except ThousandsError, e: QtGui.QMessageBox.warning(self, "Thousands Separator Error", e) self.thousandsEdit.selectAll() self.thousandsEdit.setFocusPolicy() return except DecimalError, e: QtGui.QMessageBox.warning(self, "Decimal Marker Error", e) 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() QtGui.QDialog.accept(self) def setNumberFormat(self): dialog = NumberFormatDlg(self.format,self) if dialog.exec_(): self.format = dialog.numberFormat() self.refreshTable()app = QtGui.QApplication(sys.argv)nfd = NumberFormatDlg(format)nfd.setNumberFormat()sys.exit(app.exec_())



3)智能对话框:能够根据传送到其初始化程序中的引用数据或者数据结构来初始化各个窗口部件,这就使其具有了能够根据用户交互来直接更新数据的能力,这种对话框可认为是“智能对话框”


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值