PySide学习笔记第九章-对话框

来源:互联网 发布:安化集团网络办公系统 编辑:程序博客网 时间:2024/05/20 07:53

PySide中的对话框

对话框是用来和应用交流的,比如输入数据,更改数据,改变应用设置,对话框是用户和电脑程序的一种重要的交流方式

 一、QtGui.QInputDialog 提供了一个简便的对话框可以让用户输入数值(数值可以是:字符串,数值,也可以是一个list中的一项)。

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialIn this example, we receive data froma QtGui.QInputDialog dialog.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGuiclass Example(QtGui.QWidget):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):        self.btn = QtGui.QPushButton('Dialog', self) # 创建一个button,然后设定其位置        self.btn.move(20, 20)        self.btn.clicked.connect(self.showDialog) # button点击的信号和self.showDialog的槽函数联系起来        self.le = QtGui.QLineEdit(self) # 创建一个行编辑器,然后设定其位置        self.le.move(130, 22)        self.setGeometry(300, 300, 290, 150)        self.setWindowTitle('Input dialog')        self.show()    def showDialog(self): # 定义槽函数        text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', # 创建一个输入对话框,参数分别为   self:当前 QtGui.QWidget                                              # ,'Input Dialog':title,'Input Dialog':对话框里的信息,                                              # 该函数返回两个值,返回输入的文本和一个boolean(点击OK,返回true)            'Enter your name:')        if ok: # 如果点击OK            self.le.setText(str(text))def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()
二、QtGui.QColorDialog提供了一个对话框来选择颜色

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialIn this example, we select a colour valuefrom the QtGui.QColorDialog and change the backgroundcolour of a QtGui.QFrame widget.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide 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.showDialog函数槽联系起来        self.frm = QtGui.QFrame(self) # 创建一个颜色框,初始背景颜色为黑色(0,0,0)        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() # 弹出选择颜色的对话框,并返回你所选定的颜色(当你点击Ok)        if col.isValid(): # 判断颜色是否有效,即你是否选定颜色后并点击了Ok            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 用来选择字体,代码逻辑同上


# -*- coding: utf-8 -*-"""ZetCode PySide tutorialIn this example, we select a font nameand change the font of a label.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide 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 PySide 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('play.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()

 

1 0
原创粉丝点击