pyqt6:QMessageBox 、QInputDialog、QFileDialog弹窗

目录

一、准备步骤

二、代码


 一、准备步骤

先在qtdesigner拖入一个pushButton。然后保存项目命名为test.ui。

 用pyuic生成test.py文件。再创建load_test.py文件,写代码如下:

二、代码

import sys
from test import Ui_Form

import warnings
warnings.filterwarnings('ignore')

from PyQt6.QtWidgets import *


class MyApp(QWidget, Ui_Form):

    def __init__(self):
        super(MyApp, self).__init__()
        self.setupUi(self)

        self.pushButton.clicked.connect(self.say)

    def say(self):
        # region QMessageBox
        QMessageBox.information(self, "提示", "这是一个信息消息。")
        QMessageBox.warning(self, "警告", "这是一个警告消息。")
        QMessageBox.critical(self, "错误", "这是一个错误消息。")
        QMessageBox.about(self, '关于', '这是关于软件的说明。。。')

        choice = QMessageBox.question(self, "问题", "是否继续?", QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
        if choice == QMessageBox.StandardButton.Yes:
            print("用户点击了'是'")
        else:
            print("用户点击了'否'")
        # endregion

        # region QInputDialog
        value, sure = QInputDialog.getInt(self, '输入整数', '值', min=0, max=20)
        if sure:
            print(value)

        value, sure = QInputDialog.getDouble(self, '输入小数', '值', min=0, max=20)
        if sure:
            print(value)

        value, sure = QInputDialog.getText(self, '输入字符串', '值', text='')
        if sure:
            print(value)


        value, sure = QInputDialog.getMultiLineText(self, '输入整数', '值', '')
        if sure:
            print(value)

        seasons = ['春', '夏', '秋', '东']
        value, sure = QInputDialog.getItem(self, '选择季节', '值', seasons, current=0, editable=False)  # 我这里不能写items=seasons,只能seasons,不然会报错,莫名其妙的
        if sure:
            print(value)
        # endregion

        # region QFileDialog
        dir_ = QFileDialog.getExistingDirectory(self, "选取文件夹", "C:/")  # 起始路径
        print(dir_)

        file_, _ = QFileDialog.getOpenFileName(self, "选取文件", "C:/", "All Files (*);;Text Files (*.txt)")  # 文件扩展名用双分号间隔
        print(file_)

        file_, _ = QFileDialog.getSaveFileName(self, "文件保存", "C:/", "All Files (*);;Text Files (*.txt)")
        print(file_)
        # endregion


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyApp()
    w.show()
    sys.exit(app.exec())

感受吧

1. QMessageBox(消息框)类的用法: - QMessageBox 是用于显示各种类型消息框的类,例如信息框、警告框、错误框等。 - 常见的内置函数: - `QMessageBox.information(parent, title, message, buttons, defaultButton)`:显示一个信息框。 - `QMessageBox.warning(parent, title, message, buttons, defaultButton)`:显示一个警告框。 - `QMessageBox.critical(parent, title, message, buttons, defaultButton)`:显示一个错误框。 - `QMessageBox.question(parent, title, message, buttons, defaultButton)`:显示一个询问框。 2. QInputDialog(输入对话框)类的用法: - QInputDialog 用于显示一个输入对话框,用于获取用户输入的文本或数值。 - 常见的内置函数: - `QInputDialog.getText(parent, title, label, echo, text, flags)`:显示一个文本输入对话框。 - `QInputDialog.getInt(parent, title, label, value, min, max, step, flags)`:显示一个整数输入对话框。 - `QInputDialog.getDouble(parent, title, label, value, min, max, decimals, flags)`:显示一个浮点数输入对话框。 3. QFileDialog(文件对话框)类的用法: - QFileDialog 用于显示一个文件对话框,用于选择文件或目录。 - 常见的内置函数: - `QFileDialog.getOpenFileName(parent, caption, directory, filter, initialFilter, options)`:显示一个打开文件对话框。 - `QFileDialog.getSaveFileName(parent, caption, directory, filter, initialFilter, options)`:显示一个保存文件对话框。 - `QFileDialog.getExistingDirectory(parent, caption, directory, options)`:显示一个选择目录的对话框。 重要的 Dialogs 宏定义用法: - Dialogs 中有一些重要宏定义,用于定义对话框的按钮类型、图标类型等。例如: - `QMessageBox.Ok`:确定按钮。 - `QMessageBox.Cancel`:取消按钮。 - `QMessageBox.Yes`:是按钮。 - `QMessageBox.No`:否按钮。 - `QMessageBox.Information`:信息图标。 - `QMessageBox.Warning`:警告图标。 - `QMessageBox.Critical`:严重错误图标。 - … 下面是一个详细的例子,演示了如何使用 QMessageBox、QInputDialog 和 QFileDialog: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QInputDialog, QFileDialog class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Dialogs Example') btn1 = QPushButton('Show Information', self) btn1.move(30, 50) btn1.clicked.connect(self.showInformation) btn2 = QPushButton('Get Text', self) btn2.move(30, 100) btn2.clicked.connect(self.getText) btn3 = QPushButton('Open File', self) btn3.move(30, 150) btn3.clicked.connect(self.openFile) def showInformation(self): QMessageBox.information(self, 'Information', 'This is an information message.') def getText(self): text, ok = QInputDialog.getText(self, 'Text Input', 'Enter your name:') if ok: QMessageBox.information(self, 'Hello', f'Hello, {text}!') def openFile(self): file_name, _ = QFileDialog.getOpenFileName(self, 'Open File', '', 'Text Files (*.txt)') if file_name: with open(file_name, 'r') as file: content = file.read() QMessageBox.information(self, 'File Content', f'The content of the file:\n{content}') app = QApplication(sys.argv) window = Example() window.show() sys.exit(app.exec_()) ``` 在上述例子中,我们创建了一个窗口,并添加了三个按钮。点击按钮会触发相应的操作。 - 点击 "Show Information" 按钮,会弹出一个信息框。 - 点击 "Get Text" 按钮,会弹出一个输入对话框,获取用户输入的文本,并显示一个信息框。 - 点击 "Open File" 按钮,会弹出一个文件选择对话框,选择一个文本文件后,会读取文件内容,并显示一个信息框。 通过这个例子,我们可以了解如何使用 QMessageBox、QInputDialog 和 QFileDialog 类来实现常见的对话框功能,以及如何使用重要的宏定义来定制对话框的按钮和图标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

learninger_lt7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值