博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZetCode PyQt4 tutorial Dialogs
阅读量:6534 次
发布时间:2019-06-24

本文共 6525 字,大约阅读时间需要 21 分钟。

#!/usr/bin/python# -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorial In this example, we receive data froma QtGui.QInputDialog dialog. author: Jan Bodnarwebsite: zetcode.com last edited: October 2011"""import sysfrom PyQt4 import QtGuiclass Example(QtGui.QWidget):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(self):              self.btn = QtGui.QPushButton('Dialog', self)        self.btn.move(20, 20)        self.btn.clicked.connect(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):                # This line displays the input dialog. The first string is a dialog title, the second one is a message within the dialog. The dialog returns the entered text and a boolean value. If we click the Ok button, the boolean value is true.        text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',             'Enter your name:')                # The text that we have received from the dialog is set to the line edit widget.        if ok:            self.le.setText(str(text))        def main():        app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()--------------------------------------------------------------------------------#!/usr/bin/python# -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorial In this example, we select a colour valuefrom the QtGui.QColorDialog and change the backgroundcolour of a QtGui.QFrame widget. author: Jan Bodnarwebsite: zetcode.com last edited: October 2011"""import sysfrom PyQt4 import QtGuiclass Example(QtGui.QWidget):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(self):              # This is an initial colour of the QtGui.QFrame background.        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):              # This line will pop up the QtGui.QColorDialog.        col = QtGui.QColorDialog.getColor()        # We check if the colour is valid. If we click on the Cancel button, no valid colour is returned. If the colour is valid, we change the background colour using style sheets.        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()--------------------------------------------------------------------------------#!/usr/bin/python# -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorial In this example, we select a font nameand change the font of a label. author: Jan Bodnarwebsite: zetcode.com last edited: October 2011"""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):        # Here we pop up the font dialog. The getFont() method returns the font name and the ok parameter. It is equal to True if the user clicked OK; otherwise it is False.        font, ok = QtGui.QFontDialog.getFont()        # If we clicked ok, the font of the label would be changed.        if ok:            self.lbl.setFont(font)        def main():        app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()--------------------------------------------------------------------------------#!/usr/bin/python# -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorial In this example, we select a file with aQtGui.QFileDialog and display its contentsin a QtGui.QTextEdit.author: Jan Bodnarwebsite: zetcode.com last edited: October 2011"""import sysfrom PyQt4 import QtGui#The example is based on the QtGui.QMainWindow widget because we centrally set the text edit widget.class 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):        # We pop up the QtGui.QFileDialog. The first string in the getOpenFileName() method is the caption. The second string specifies the dialog working directory. By default, the file filter is set to All files (*).        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',                 '/home')                # The selected file name is read and the contents of the file are set to the text edit widget.        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()

 

转载于:https://www.cnblogs.com/zengjfgit/p/4851649.html

你可能感兴趣的文章
Ajax跨域访问XML数据的另一种方式——使用YQL查询语句
查看>>
[原创]让您的服务器不再有被挂马的烦恼---文件安全卫士
查看>>
流水线和PC指针
查看>>
Fiddler设置抓取https请求
查看>>
div布局小技巧
查看>>
OCP 12c最新考试原题及答案(071-4)
查看>>
MHA故障切换和在线手工切换原理
查看>>
JAVA并发,同步锁性能测试
查看>>
Python版本切换和Pip安装
查看>>
SilverLigth学习笔记--控制 Silverlight控件样式(转)
查看>>
poj3262
查看>>
第四十天笔记
查看>>
4、动态代理
查看>>
Loj #6073.「2017 山东一轮集训 Day5」距离
查看>>
我的TCP/IP学习笔记
查看>>
shell--字符串的截取变量子串串
查看>>
Cas_个人理解
查看>>
UISearchController
查看>>
梦断代码阅读笔记02
查看>>
轮毂电机光电增量编码器的ABZ信号详解
查看>>