PyQt4 tutorial读书笔记(5)-- Dialog

来源:互联网 发布:军休所知乎 编辑:程序博客网 时间:2024/06/05 03:54

转载自:http://zetcode.com/gui/pyqt4/dialogs/

QInputDialog

def showDialog(self):        text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',             'Enter your name:')        if ok:            self.le.setText(str(text))

第一个参数是对话框标题,第二个是对话框显示信息。这个对话框将返回一个输入的文本和一个布尔值。如果我们点击OK按钮,布尔值就是true。并且把返回的输入文本赋值。

QtGui.QColorDialog

QtGui.QColorDialog提供了一个用于选择颜色值的对话框控件。

from PyQt4 import QtGuiclass Example(QtGui.QWidget):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):              col = QtGui.QColor(0, 0, 0)         self.btn = QtGui.QPushButton('Dialog', self)        self.btn.move(20, 20)        self.btn.clicked.connect(self.showDialog)        self.frm = QtGui.QFrame(self)        self.frm.setStyleSheet("QWidget { background-color: %s }"             % col.name())        self.frm.setGeometry(130, 22, 100, 100)                    self.setGeometry(300, 300, 250, 180)        self.setWindowTitle('Color dialog')        self.show()    def showDialog(self):        col = QtGui.QColorDialog.getColor()        if col.isValid():            self.frm.setStyleSheet("QWidget { background-color: %s }"                % col.name())def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

QtGui.QFontDialog

QtGui.QFontDialog 提供了一个用于选择font的对话框

import sysfrom PyQt4 import QtGuiclass Example(QtGui.QWidget):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):              vbox = QtGui.QVBoxLayout()        btn = QtGui.QPushButton('Dialog', self)        btn.setSizePolicy(QtGui.QSizePolicy.Fixed,            QtGui.QSizePolicy.Fixed)        btn.move(20, 20)        vbox.addWidget(btn)        btn.clicked.connect(self.showDialog)        self.lbl = QtGui.QLabel('Knowledge only matters', self)        self.lbl.move(130, 20)        vbox.addWidget(self.lbl)        self.setLayout(vbox)                  self.setGeometry(300, 300, 250, 180)        self.setWindowTitle('Font dialog')        self.show()    def showDialog(self):        font, ok = QtGui.QFontDialog.getFont()        if ok:            self.lbl.setFont(font)def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

QtGui.QFileDialog

import sysfrom PyQt4 import QtGuiclass Example(QtGui.QMainWindow):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):              self.textEdit = QtGui.QTextEdit()        self.setCentralWidget(self.textEdit)        self.statusBar()        openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)        openFile.setShortcut('Ctrl+O')        openFile.setStatusTip('Open new File')        openFile.triggered.connect(self.showDialog)        menubar = self.menuBar()        fileMenu = menubar.addMenu('&File')        fileMenu.addAction(openFile)               self.setGeometry(300, 300, 350, 300)        self.setWindowTitle('File dialog')        self.show()    def showDialog(self):        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',                 '/home')        f = open(fname, 'r')        with f:                    data = f.read()            self.textEdit.setText(data) def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()
0 0
原创粉丝点击