博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PyQt5对话框学习
阅读量:3898 次
发布时间:2019-05-23

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

QInputDialog

用户输入使用QInputDialog输入字符,使用QLineEdit显示

import sysfrom PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,                             QInputDialog, QApplication)class Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.btn = QPushButton('Dialog', self)        self.btn.move(20, 20)        self.btn.clicked.connect(self.showDialog)        self.le = QLineEdit(self)        self.le.move(130, 22)        self.setGeometry(300, 300, 290, 150)        self.setWindowTitle('Input dialog')        self.show()    def showDialog(self):        text, ok = QInputDialog.getText(self, 'Input Dialog',                                        'Enter your name:')        if ok:            self.le.setText(str(text))if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

QColorDialog
QColorDialog显示一个用于选择颜色值的对话框。

import sysfrom PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,     QColorDialog, QApplication)from PyQt5.QtGui import QColor  class Example(QWidget):        def __init__(self):        super().__init__()                self.initUI()                    def initUI(self):               col = QColor(0, 0, 0)          self.btn = QPushButton('Dialog', self)        self.btn.move(20, 20)         self.btn.clicked.connect(self.showDialog)         self.frm = 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 = QColorDialog.getColor()         if col.isValid():            self.frm.setStyleSheet("QWidget { background-color: %s }"                % col.name())                    if __name__ == '__main__':        app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

QFontDialog

QFontDialog对话框用以选择字体

import sysfrom PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,                             QSizePolicy, QLabel, QFontDialog, QApplication)class Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        vbox = QVBoxLayout()        btn = QPushButton('Dialog', self)        #布置在左上角        btn.setSizePolicy(QSizePolicy.Fixed,                          QSizePolicy.Fixed)        btn.move(20, 20)        vbox.addWidget(btn)        btn.clicked.connect(self.showDialog)        self.lbl = 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 = QFontDialog.getFont()        if ok:            self.lbl.setFont(font)if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

QFileDialog
QFileDialog是一个让用户选择文件和目录的对话框

import sysfrom PyQt5.QtWidgets import (QMainWindow, QTextEdit,     QAction, QFileDialog, QApplication)from PyQt5.QtGui import QIcon  class Example(QMainWindow):        def __init__(self):        super().__init__()                self.initUI()                    def initUI(self):               self.textEdit = QTextEdit()        self.setCentralWidget(self.textEdit)        self.statusBar()         openFile = QAction(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 = QFileDialog.getOpenFileName(self, 'Open file', '/home')         if fname[0]:            f = open(fname[0], 'r')             with f:                data = f.read()                self.textEdit.setText(data)                if __name__ == '__main__':        app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

转载地址:http://qaben.baihongyu.com/

你可能感兴趣的文章
(转)C++中extern “C”含义深层探索
查看>>
【日常小记】linux中强大且常用命令:find、grep
查看>>
Linux多线程编程(不限Linux)
查看>>
C/C++内存泄漏及检测
查看>>
C中的继承和多态
查看>>
linux修改ssh端口和禁止root远程登陆设置
查看>>
What really happens when you navigate to a URL
查看>>
偶遇with ties
查看>>
linux 编译指定库、头文件的路径问题
查看>>
使用gdb调试运行时的程序小技巧
查看>>
linux后端服务程序之信号处理
查看>>
Padding也要小心
查看>>
linux异步IO编程实例分析
查看>>
小组开发环境搭建: apache+ftp+cvs+samba
查看>>
Learning C with gdb
查看>>
不可不知的json库
查看>>
JSON格式解析和libjson使用简介
查看>>
关于Json格式的理解
查看>>
c语言解析json数据
查看>>
一个C实现的记日志的函数库
查看>>