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

发布时间:2023-01-17 10:06:07 作者:iii
来源:亿速云 阅读:142

本篇内容介绍了“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显示通用消息框

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

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

推荐阅读:
  1. mac下如何使用vscode来调试python代码
  2. go语言和python有哪些区别

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python qdialog

上一篇:如何修改linux系统终端字体

下一篇:PyQt5如何使用QMessageBox显示不同的对话框

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》