PyQt5基本控件使用:消息弹出、用户输入、文件/目录选择对话框
date
May 27, 2020
Last edited time
Jun 27, 2021 06:29 AM
status
Published
slug
pyqt_basic_1
tags
PyQt
summary
PyQt入门
type
Post
Field
Plat
本文主要介绍 PyQt 界面实现中常用的消息弹出对话框、提供用户输入的输入框、打开文件获取文件 / 目录路径的文件对话框。学习这三种控件前,先想一下它们使用的主要场景:
1、消息弹出对话框。程序遇到问题需要退出需要弹出错误提示框 、程序执行可能造成的风险需要弹出警告窗口提示用户是否进一步执行等等。
2、用户输入框。比如常见的让用户选择执行的程序分支、yes/no 等等。
3、文件对话框。获取本地文件或者文件夹的完整路径甚至是直接打开文件显示文件内容。
本文主要针对这三种控件的主要场景进行介绍。
QMessageBox 是一种通用的弹出式对话框,用于显示消息,允许用户通过单击不同的标准按钮对消息进行反馈。弹出式对话框有很多类型,如提示、警告、错误、询问、关于等对话框。这些不同类型的 QMessageBox 对话框只是显示时图标不同,其他功能一样。
QMessageBox 类中常用方法
information(QWdiget parent,title,text,buttons,defaultButton):弹出消息对话框。
question(QWidget parent,title,text,buttons,defaultButton):弹出问答对话框
warning(QWidget parent,title,text,buttons,defaultButton):弹出警告对话框
critical(QWidget parent,title,text,buttons,defaultButton):弹出严重错误对话框
about(QWidget parent,title,text):弹出关于对话
参数解释如下:
parent:指定的父窗口控件。
title:表示对话框标题。
text:表示对话框文本。
buttons:表示多个标准按钮,默认为 ok 按钮。
defaultButton 表示默认选中的标准按钮,默认选中第一个标准按钮。
其他方法如下:
setTitle():设置标题
setText():设置正文消息
setIcon():设置弹出对话框的图片
QMessageBox 的标准按钮类型
QMessage.Ok 同意操作、QMessage.Cancel 取消操作、QMessage.Yes 同意操作、QMessage.No 取消操作、QMessage.Abort 终止操作、QMessage.Retry 重试操作、QMessage.Ignore 忽略操作
5 种常用的消息对话框及其显示效果
提前说明:from PyQt5.QtWidgets import QMessageBox 导入直接使用
(1)消息对话框,用来告诉用户关于提示信息
QMessageBox.information(self, ‘信息提示对话框’,‘前方右拐到达目的地’,QMessageBox.Yes | QMessageBox.No)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F73a29040-4175-44d9-b17b-8cd90dd6567d%2FUntitled.png?table=block&id=acec7719-e5b8-455c-9942-baee10f814d5&cache=v2)
(2)提问对话框,用来告诉用户关于提问消息。
QMessageBox.question(self, “提问对话框”, “你要继续搞测试吗?”, QMessageBox.Yes | QMessageBox.No)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F9de9fbe1-915f-41a2-b9f0-58e235886634%2FUntitled.png?table=block&id=c3cf3c9e-7d09-4245-b013-a97febaa000c&cache=v2)
特别注意 Tips: 对于提问对话框,需要根据用户选择 Yes 或者 No 进行下一步判断,可以获取按钮点击的返回值进行判断,选择 NO 的返回值为 65536,选择 Yes 的返回值为其他。。示例如下:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fea209888-ae4d-4479-9b17-7cd56fddd570%2FUntitled.png?table=block&id=7afdb100-2a4d-49b8-bcfa-ff7bff9113c4&cache=v2)
(3)警告对话框,用来告诉用户关于不寻常的错误消息。
QMessageBox.warning(self, “警告对话框”, “继续执行会导致系统重启,你确定要继续?”, QMessageBox.Yes | QMessageBox.No)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fecd8fd9d-0d48-4919-991e-28b013708546%2FUntitled.png?table=block&id=9d5883d6-ef59-4a82-a511-7f3a54fa0f0a&cache=v2)
(4)严重错误对话框,用来告诉用户关于程序执行失败的严重的错误消息。
QMessageBox.critical(self, “严重错误对话框”, “数组越界,程序异常退出”, QMessageBox.Yes | QMessageBox.No)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F0bd661f7-4612-499a-8a1e-f6fa3e76dd16%2FUntitled.png?table=block&id=80db1808-c1d0-4836-b391-dfb6145929d9&cache=v2)
(5)关于对话框
QMessageBox.about(self, “关于对话框”, “你的 Windows 系统是 DOS1.0”)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F849588c6-069e-4b81-ae5b-2fc7bc14d4c9%2FUntitled.png?table=block&id=a44444d0-01a9-4a5d-a8d8-30990f370e58&cache=v2)
上述程序完整代码如下:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F59fc2782-0d37-4668-886f-9fa4f61b5535%2FUntitled.png?table=block&id=5d31296c-b780-4b2d-a7ed-7b13cb1499c4&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F5a050972-3083-4061-83f8-ad7b65089962%2FUntitled.png?table=block&id=4c4c2878-694f-4adb-beb0-f92fc2339cfb&cache=v2)
QInputDialog 控件是一个标准对话框,用于获取用户输入信息,QInputDialog 控件可以提供数字、字符串输入或提供下拉列表选择。
针对 QInputDialog 对话框控件的使用,我们主要考虑 2 个问题:1、如何在弹出对话框供用户输入,2、如何获取用户输入。
QInputDialog 常用方法:
getint():从输入控件中获得标准整数输入
getDouble():从输入控件中获得标准浮点数输入
getText():从输入控件中获得标准字符串的输入
getItem() :从输入控件中获得列表里的选项输入
完整代码如下(代码可直接复制运行):
运行结果如下(会陆续弹出三个输入对话框):
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F1ff354ff-487a-4e35-bc67-6c325649d2a9%2FUntitled.png?table=block&id=377cba6d-127a-4da0-80ac-42a373ab8617&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F4f9a317d-39ce-4581-b1dc-7f3ee7eee406%2FUntitled.png?table=block&id=cf544eca-765c-4779-9fc6-b25a160fee74&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F60abfa5b-b992-44be-9f9e-496cdef57d5e%2FUntitled.png?table=block&id=08d7d8d7-cd80-4f84-9549-9dfa0374dee4&cache=v2)
关键代码介绍:
QInputDialog.getInt(self, ‘Integer input dialog’, ‘输入数字’) -> 输入整数对话框
QInputDialog.getText(self, ‘Text Input Dialog’, ‘输入姓名:’) -> 输入字符串对话框
QInputDialog.getItem(self, “select input dialog”, ‘语言列表’, items, 0, False) -> 下拉列表选择对话框
QFileDialog 是用于打开和保存文件的标准对话框。使用 QFileDialog 控件主要考虑 2 个场景:使用该控件提供用户选择目录或文件,并保存选择目录或文件的路径。简单说就是实现类似 word/Notepad++ 文件打开功能。如下
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fa923e599-3cb2-4500-a7f8-403a9ee9d118%2FUntitled.png?table=block&id=9113b87a-0ebf-4063-9639-c4840291f1dd&cache=v2)
针对上述场景,QFileDialog 控件实现的主要方法:
QFileDialog.getOpenFileName():获取单个文件路径
QFileDialog.getOpenFileNames():获取多个文件路径
QFileDialog.getExistingDirectory():获取文件夹路径
完整代码如下:
运行结果如下(会陆续弹出选择文件夹、选择单个文件、选择多个文件):
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F69b81cde-60f3-41f3-bb2f-466c1888fe21%2FUntitled.png?table=block&id=284ec0a7-eff1-4a23-be87-60713569991d&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F021b9def-67d4-4da3-bb65-2ea2a5c3e899%2FUntitled.png?table=block&id=e734d89e-0e0b-4529-8618-88805642560d&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Ff854bcc6-788e-44cf-985f-99a395581d41%2FUntitled.png?table=block&id=14051165-9f28-4326-9ceb-39d489ad2eff&cache=v2)
关键代码介绍
QFileDialog.getOpenFileName(self,“选取单个文件”,“C:/”,“All Files ();;Text Files (.txt)”) -> 获取单个指定文件的绝对路径
getOpenFileName() 参数说明:
第 1 个参数:用于指定父组件
第 2 个参数:对话框标题
第 3 个参数:对话框显示时默认打开的目录。“.” 表示当前程序所在目录,“/” 表示当前盘下的根目录。
第 4 个参数:对话框中文件扩展名过滤器。All Files ();;Text Files (.txt) 表示可以选择所有文件类型或者只显示. txt 后缀的文件类型。
QFileDialog.getExistingDirectory(self,“选取指定文件夹”,“C:/”) -> 获取指定文件夹的绝对路径
QFileDialog.getOpenFileNames(self,“选取多个文件”,“C:/”,“All Files ();;Text Files (.txt)”) -> 获取多个指定文件的绝对路径
本文介绍了消息弹出对话框、用户输入对话框以及文件打开对话框的基本使用方法。内容覆盖了这三类控件的基本使用场景。可以开始动手尝试了。。