PyQt5学习笔记三----组件---对话框类控件

15 篇文章 0 订阅

QDialog对话框:QDialog是对话框的父类,子类大概有:QMessageBox、QFileDialog、QFontDialog、QInputDialog等等。

常用方法

setWindowTitle()

设置对话框标题

setWindowModality()

设置窗口模态

Qt.NonMadal: 非模态,可以和程序的其他窗口交互

Qt.WindowModal: 窗口模态,程序在未处理完当前对话框时,将阻止和对话框的父窗口进行交互

Qt.ApplicationModal: 应用程序模态,阻止和任何其他窗口进行交互

#对话框QDialog实例
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class DialogDemo(QMainWindow):
    def __init__(self,parent=None):
        super(DialogDemo,self).__init__(parent)

        self.setWindowTitle("Dialog")
        self.resize(350,300)
        self.btn=QPushButton(self)
        self.btn.setText("弹出对话框")
        self.btn.move(50,50)
        self.btn.clicked.connect(self.showdialog)
    def showdialog(self):
        dialog=QDialog()
        btn=QPushButton("ok",dialog)
        btn.move(50,50)
        dialog.setWindowTitle("Dialog")
        dialog.setWindowModality(Qt.ApplicationModal)
        dialog.exec_()
if __name__=="__main__":
    app=QApplication(sys.argv)
    demo=DialogDemo()
    demo.show()
    sys.exit(app.exec_())

QMessageBox:是一种通用式对话框,用于显示消息。允许用户通过单机不同的标准按钮对消息进行反馈,每个标准按钮都有一个预定义的文本角色和十六进制叔。QMessageBox类提供许多常用的弹出式对话框,如提示、警告、错误、询问、关于等等。

常用方法:

information(QWidget parent,title,text,buttons,defaultButton)

弹出消息对话框,参数解释如下:

parent:指定的父窗口控件

title:对话框标题

text:对话框文本

buttons:多个标准按钮,默认为ok

defaultButton:默认第一个标准按钮

question(QWidget parent,title,text,buttons,defaultButton)

弹出问答对话框

warning(Widget parent,title,text,buttons,defaultButton)

弹出警告对话框

ctitical(QWidget parent,title,text,buttons,defaultButton)

弹出严重错误对话框

about(QWidget parent,title,text)

弹出关于对话框

setTitle()

设置标题

setText()

设置消息正文

setIcon()

设置对话框图片

标准按钮类型

QMessage.Ok

同意

QMessage.Cancel

取消

QMessage.Yes

同意

QMessage.No

取消

QMessage.Abort

终止

QMessage.Retry

重试

QMessage.Ignore

忽略

五种常用对话框及显示效果

  1. QMessageBox.information(self,"标题","消息对话框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
  2. QmessageBox.question(self,"标题","提问对话框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
  3. QmessageBox.warning(self,"标题","警告对话框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
  4. QmessageBox.critical(self,"标题","严重错误对话框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
  5. QmessageBox.about(self,"标题","关于对话框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
#消息对话框对话框QMessageBox实例
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MyWindow(QWidget):
    def __init__(self,parent=None):
        super(MyWindow, self).__init__(parent)
        self.setWindowTitle("消息对话框")
        self.resize(300,100)
        self.initUI()

    def initUI(self):
        self.myButton=QPushButton(self)
        self.myButton.setText("点击弹出对话框")
        self.myButton.clicked.connect(self.msg)
    def msg(self):
        #使用infomation信息框
        reply=QMessageBox.information(self,"标题","消息正文",QMessageBox.Yes|QMessageBox.No,QMessageBox.No)
        print(reply)
if __name__=="__main__":
    app=QApplication(sys.argv)
    myShow=MyWindow()
    myShow.show()
    sys.exit(app.exec_())

QInputDialog:该控件是一个标准对话框,有一个文本框和两个按钮组成(ok、cancel)

常用方法

getInt()

获得标准整数输入

getDoublie()

获得标准浮点数输入

getText()

获得标准字符串输入

getItem()

获得列表里的选项输入

#消息对话框对话框QMessageBox实例
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class InputDialogDemo(QWidget):
    def __init__(self,parent=None):
        super(InputDialogDemo, self).__init__(parent)
        self.setWindowTitle("QInputDialog")
        self.layout=QFormLayout()
        self.initUI()
    def initUI(self):
        self.btn1=QPushButton("获得列表里的选项")
        self.btn1.clicked.connect(self.getItem)
        self.le1=QLineEdit()
        self.layout.addRow(self.btn1,self.le1)

        self.btn2=QPushButton("获得字符串")
        self.btn2.clicked.connect(self.getIext)
        self.le2=QLineEdit()
        self.layout.addRow(self.btn2,self.le2)

        self.btn3=QPushButton("获得整数")
        self.btn3.clicked.connect(self.getInt)
        self.le3=QLineEdit()
        self.layout.addRow(self.btn3,self.le3)
        self.setLayout(self.layout)

    def getItem(self):
        items=("C","C++","Java","Python")
        #从列表里选择一个选项,点击OK键,最终选到的值即赋值给item
        item,ok=QInputDialog.getItem(self,"select input dialog","语言列表",items,0,False)
        if ok and item:
            self.le1.setText(item)

    def getIext(self):
        text,ok=QInputDialog.getText(self,"Text Input Dialog","输入姓名")
        if ok:
            self.le2.setText(str(text))

    def getInt(self):
        num,ok=QInputDialog.getInt(self,"integer input dialog","输入数字")
        if ok:
            self.le3.setText(str(num))

if __name__=="__main__":
    app=QApplication(sys.argv)
    demo=InputDialogDemo()
    demo.show()
    sys.exit(app.exec_())

QFontDialog:该控件是一个常用的字体选择对话框,可以让用户选择所显示的文本的字号大小、样式、和格式。使用getFont()可以从字体选择对话框中选择文本显示字号、大小、样式、格式。

#消息对话框对话框QFontDialog实例
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class FontDialogDemo(QWidget):
    def __init__(self,parent=None):
        super(FontDialogDemo, self).__init__(parent)
        layout=QVBoxLayout()
        self.fontButton=QPushButton("choose font")
        self.fontButton.clicked.connect(self.getFont)
        layout.addWidget(self.fontButton)

        self.fontLineEdit=QLabel("hello 测试字体例子")
        layout.addWidget(self.fontLineEdit)
        self.setLayout(layout)
        self.setWindowTitle("Font Diaog例子")

    def getFont(self):
        font,ok=QFontDialog.getFont()
        if ok:
            self.fontLineEdit.setFont(font)
if __name__=="__main__":
    app=QApplication(sys.argv)
    demo=FontDialogDemo()
    demo.show()
    sys.exit(app.exec_())

QFileDialog:该控件是用于打开和保存文件的标准对话框。QFileDialog在打开文件时使用了文件过滤器,用于显示指定的扩展名的文件

常用方法

getOpenFileName()

返回用户所选择文件的名称,并打开该文件

getSaveFileName()

使用用户选择的文件名并保存文件

setFileMode()

可选择的文件类型:

QFileDialog.AnyFile 任何文件

QFileDialog.ExistingFile 已存在的文件

QFileDialog.Directory文件目录

QFileDialog.ExistingFiles已存在的多个文件

setFilter()

设置过滤器,只显示过滤器允许的文件类型

#消息对话框对话框QFileDialog实例
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class FileDialogDemo(QWidget):
    def __init__(self,parent=None):
        super(FileDialogDemo, self).__init__(parent)
        layout=QVBoxLayout()
        self.btn=QPushButton("加载图片")
        self.btn.clicked.connect(self.getFile)
        layout.addWidget(self.btn)
        self.le=QLabel("")
        layout.addWidget(self.le)
        self.btn1=QPushButton("加载文本文件")
        self.btn1.clicked.connect(self.getFiles)
        layout.addWidget(self.btn1)
        self.contents=QTextEdit()
        layout.addWidget(self.contents)
        self.setLayout(layout)
        self.setWindowTitle("FileDialog")

    def getFile(self):
        fname,_ =QFileDialog.getOpenFileName(self,"open file","c:\\","Image files (*.jpg *.gif)")
        self.le.setPixmap(QPixmap(fname))

    def getFiles(self):
        dlg=QFileDialog()
        dlg.setFileMode(QFileDialog.AnyFile)
        dlg.setFilter(QDir.Files)
        if(dlg.exec_()):
            filenames=dlg.selectedFiles()
            f=open(filenames[0],'r')
            with f:
                data=f.read()
                self.contents.setText(data)

if __name__=="__main__":
    app=QApplication(sys.argv)
    demo=FileDialogDemo()
    demo.show()
    sys.exit(app.exec_())

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值