欢迎光临
我们一直在努力

PyQt6如何使用QDialog显示通用消息框

本篇内容介绍了“PyQt6如何使用QDialog显示通用消息框”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

使用QDialog显示通用消息框

直接使用QDialog类,可以及通过对话框进行通用对话框显示,亦可以通过自定义设置自己需要的对话框。

# _*_ coding:utf-8 _*_
 
import sys
 
from PyQt6.QtWidgets import QWidget
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWidgets import QMainWindow
from PyQt6.QtWidgets import QVBoxLayout
from PyQt6.QtWidgets import QHBoxLayout
from PyQt6.QtWidgets import QPushButton
from PyQt6.QtWidgets import QDialog
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import Qt
 
 
class MainWindowView(QMainWindow):
    """主窗体界面"""
 
    def __init__(self):
        """构造函数"""
        super().__init__()
 
        self.setWindowTitle("MainWindow")
        self.setWindowIcon(QIcon(r"./res/folder_pictures.ico"))
        self.resize(300, 200)
        self.setMinimumSize(600, 400)
 
 
        self.center()
        self.initui()
 
        
 
    def initui(self):
        """初始函数"""
 
        self.vboxlayout = QVBoxLayout(self)
        self.main_widget = QWidget()
        self.main_widget.setLayout(self.vboxlayout)
        self.setCentralWidget(self.main_widget)
 
        self.hboxlayout = QHBoxLayout(self)
        self.btn = QPushButton(self)
        self.btn.setText("弹出对话框")
        self.btn.move(100,100)
        self.btn.clicked.connect(self.show_dialog)
 
    def center(self):
        """居中显示"""
        win_rect = self.frameGeometry()  # 获取窗口矩形
        screen_center = self.screen().availableGeometry().center()  # 屏幕中心
        win_rect.moveCenter(screen_center)      # 移动窗口矩形到屏幕中心
        self.move(win_rect.center())         # 移动窗口与窗口矩形重合
 
    def show_dialog(self):
        dialog = QDialog()
        button = QPushButton("确定", dialog)
        button.clicked.connect(dialog.close)
        button.move(50,50)
        dialog.setWindowTitle("QDialog")
        dialog.setWindowModality(Qt.WindowModality.ApplicationModal)
        dialog.exec()
 
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    view = MainWindowView()
    view.show()
    sys.exit(app.exec())

结果:

点击按钮可以弹出对话框,可以添加对应的按钮关联信号进行窗体关闭或控制。

“PyQt6如何使用QDialog显示通用消息框”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注云搜网网站,小编将为大家输出更多高质量的实用文章!

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。