zoukankan      html  css  js  c++  java
  • QMainWindow

    QMainWindow主窗口为用户提供一个应用程序框架,它有自己的布局,可以在布局中添加控件。在主窗口中可以添加控件,比如将工具栏、菜单栏和状态栏等添加到布局管理器中。

    窗口类型介绍

    QMainWindow、QWidget、QDialog三个类都是用来创建窗口的,可以直接使用,也可以继承后再使用。

    QMainWindow窗口可以包含菜单栏、工具栏、状态栏、标题栏等,是最常见的窗口形式,也可以说是GUI程序的主窗口。

    QDialog是对话框窗口的基类。对话框主要用来执行短期任务,或者与用户进行互动,它可以是模态的,也可以是非模态的。QDialog没有菜单栏、工具栏、状态栏等。

    如果是主窗口,就使用QMainWindow类;如果是对话框,就使用QDialog类;如果不确定,或者有可能作为顶层窗口,也有可能嵌入到其他窗口中,那么就使用QWidget类。

    创建主窗口

    如果一个窗口包含一个或多个窗口,那么这个窗口就是父窗口,被包含的窗口就是子窗口。没有父窗口的窗口是顶层窗口,QMainWindow就是一个顶层窗口,它可以包含很多界面元素,如菜单栏、工具栏、状态栏、子窗口等。

    在PyQt5中,在主窗口(QMainWindow)中会有一个控件(QWidget)占位符来占着中心窗口,可以使用setCentralWidget()来设置中心窗口。

    QMainWindow继承自QWidget类,拥有它的所有派生方法和属性。

    QMainWindow类中比较重要的方法:

      addToolBar()            添加工具栏

      centralWidget()        返回窗口中心的一个控件,未设置时返回NULL

      menuBar()                返回主窗口的菜单栏

      setCentralWidget()   设置窗口中心的控件

      setStatusBar()          设置状态栏

      statusBar()               获得状态栏对象后,调用状态栏对象的showMessage(message, int timeout = 0)方法,显示状态栏信息。其中一个参数是要显示的状态栏信息;第二个参数是信息停留的时间,单位是毫秒,默认是0,表示一直显示状态栏信息

    注意  QMainWindow不能设置布局(使用setLayout()方法),因为它有自己的布局

    案例1  创建主窗口

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication
    from PyQt5.QtGui import QIcon
    
    
    class MainWindow(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.resize(400, 200)
            self.status = self.statusBar()  # 创建状态栏
            self.status.showMessage("这是状态栏提示", 5000)  # showMessage()方法,显示提示信息,显示时间是5s
            self.setWindowTitle("PyQt5 MainWindow 例子")
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        # app.setWindowIcon("./images/cartooon1.ico")
        form = MainWindow()
        form.show()
        sys.exit(app.exec_())

    案例2  主窗口居中显示

    import sys
    from PyQt5.QtWidgets import QDesktopWidget, QApplication, QMainWindow
    
    
    class WinForm(QMainWindow):
    
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.setWindowTitle("主窗口放在屏幕中间的例子")
            self.resize(370, 250)
            self.center()
    
        def center(self):
            screen = QDesktopWidget().screenGeometry()  # 计算显示屏幕的大小
            size = self.geometry()  # 获取QWidget窗口的大小
            self.move((screen.width() - size.width())/2, (screen.height() - size.height())/2)  # 将窗口移动到屏幕中间
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = WinForm()
        win.show()
        sys.exit(app.exec_())

    案例3  关闭主窗口

    from PyQt5.QtWidgets import QMainWindow, QHBoxLayout, QPushButton, QApplication, QWidget
    import sys
    
    
    class WinForm(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setWindowTitle("关闭主窗口的例子")
            self.button1 = QPushButton("关闭主窗口")
            self.button1.clicked.connect(self.onButtonClick)
    
            layout = QHBoxLayout()
            layout.addWidget(self.button1)
    
            main_frame = QWidget()
            main_frame.setLayout(layout)
            self.setCentralWidget(main_frame)
    
        def onButtonClick(self):
            sender = self.sender()  # sender是发送信号的对象,此处发送信号的对象是button1按钮
            print(sender.text() + " 被按下了")
            qApp = QApplication.instance()
            qApp.quit()
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        form = WinForm()
        form.show()
        sys.exit(app.exec_())
  • 相关阅读:
    Mysql中use filesort的误区
    Windows双系统
    Java visualvm
    软件设计师06-数据结构
    安装VMware14可能出现的问题
    计算机硬件系统
    Web漏洞扫描
    crunch制作字典
    kali之HexorBase数据库破解
    memcahce 介绍以及安装以及扩展的安装
  • 原文地址:https://www.cnblogs.com/lynsha/p/13397378.html
Copyright © 2011-2022 走看看