对PyQt5基本窗口控件 QMainWindow的使用详解

作者:zhushoutao 时间:2021-08-27 21:47:22 

QMainWindow基本介绍

QMainWindow主窗口为用户提供了一个应用程序框架,它有自己的布局,可以在布局中添加控件。

窗口类型介绍

PyQt5中,主要使用以下三个类来创建窗口,可以直接使用,也可以继承后再使用

QMainWindow

QWidget

QDialog

QMainWindow

QMainWindow可以包含菜单栏,工具栏,状态栏,标题栏等,是GUI程序的主窗口。

如果我们需要创建主窗口程序,就使用该类。

QDialog

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

如果我们需要的是对话框,就选择该类。

QWidget

该类作为QMainWindow和QWidget的父类,并未细化到主窗口或者对话框,作为通用窗口类,如果不确定具体使用哪种窗口类,就可以使用该类。

创建主窗口

QMainWindow官方文档

如果一个窗口包含一个或者多个窗口,这个窗口就是父窗口,被包含的窗口就是子窗口。没有父窗口的窗口就是顶层窗口,QMainWindow就是顶层窗口,它可以包含很多界面元素。

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

对PyQt5基本窗口控件 QMainWindow的使用详解

重要方法

方法描述
addToolBar()添加工具栏
centralWidget()返回窗口中心的控件,未设置返回NULL
menuBar()返回主窗口的菜单栏
setCentralWidget()设置窗口中心的控件
setStatusBar()设置状态栏
statusBar()获取状态栏对象

Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.

QMainWindow不能设置布局,因为它有自己的布局,不过中心窗口是可以使用布局的。

创建一个主窗口


# _*_ coding:utf-8 _*_
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton)
from PyQt5.QtGui import QIcon

class MainWindow(QMainWindow):
def __init__(self, parent=None):
 super(MainWindow, self).__init__(parent)
 self.init_ui()

def init_ui(self):
 #设置窗口属性
 self.setGeometry(200, 200, 400, 200)
 self.setWindowTitle('创建主窗口')
 self.setWindowIcon(QIcon(r"E:\\1.jpg"))
 #设置状态栏
 self.status = self.statusBar()
 self.status.showMessage('我是状态栏', 5000)

if __name__ == "__main__":
app = QApplication(sys.argv[1:])

window = MainWindow()
window.show()

sys.exit(app.exec_())

对PyQt5基本窗口控件 QMainWindow的使用详解

主窗口居中显示


# _*_ coding:utf-8 _*_
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QDesktopWidget)
from PyQt5.QtGui import QIcon

class MainWindow(QMainWindow):
def __init__(self, parent=None):
 super(MainWindow, self).__init__(parent)
 self.init_ui()

def init_ui(self):
 #设置窗口属性
 self.resize(400, 200)
 self.setWindowTitle('主窗口居中显示')
 self.setWindowIcon(QIcon(r"E:\\1.jpg"))
 #设置状态栏
 self.status = self.statusBar()
 self.status.showMessage('我是状态栏', 5000)
 #居中显示处理
 self.move_center()

def move_center(self):
 screen = QDesktopWidget().screenGeometry()
 form = self.geometry()
 x_move_step = (screen.width() - form.width()) / 2
 y_move_step = (screen.height() - form.height()) / 2
 self.move(x_move_step, y_move_step)

if __name__ == "__main__":
app = QApplication(sys.argv[1:])

window = MainWindow()
window.show()

sys.exit(app.exec_())

对PyQt5基本窗口控件 QMainWindow的使用详解


screen = QDesktopWidget().screenGeometry()

获取当前屏幕的大小


self.geometry()

获取窗口大小

关闭主窗口


# _*_ coding:utf-8 _*_
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout,
       QPushButton, QMainWindow)

class MainWindow(QMainWindow):
def __init__(self):
 super(MainWindow, self).__init__()
 self.init_ui()

def init_ui(self):
 self.setWindowTitle('关闭主窗口')

layout = QVBoxLayout()
 self.close_btn = QPushButton('关闭', self)
 layout.addWidget(self.close_btn)

main_frame = QWidget()
 self.setCentralWidget(main_frame)
 main_frame.setLayout(layout)

self.close_btn.clicked.connect(self.on_button_click)

def on_button_click(self):
 #将信息显示在状态栏中
 sender = self.sender()
 msg = sender.text() + " 被按下了"
 status = self.statusBar()
 status.showMessage(msg, 5000)
 #程序退出
 app = QApplication.instance()
 app.quit()

if __name__ == "__main__":
app = QApplication(sys.argv[1:])

window = MainWindow()
window.show()

sys.exit(app.exec_())

对PyQt5基本窗口控件 QMainWindow的使用详解


self.close_btn.clicked.connect(self.on_button_click)

PyQt中控件之间通信使用信号(Signal)和槽(slot)机制。

定义的方式为 控件.消息.connect(槽)

上例中,就是将button的clicked信号跟槽on_button_click进行了绑定。

来源:https://blog.csdn.net/weixin_43200735/article/details/82696402

标签:PyQt5,窗口,QMainWindow
0
投稿

猜你喜欢

  • Python打开文件、文件读写操作、with方式、文件常用函数实例分析

    2023-07-01 16:31:08
  • python 列表元素左右循环移动 的多种解决方案

    2023-06-15 06:21:45
  • Python中拆分具有多个分隔符的字符串方法实例

    2022-07-01 00:38:20
  • 对python添加模块路径的三种方法总结

    2023-11-26 04:01:35
  • 防止表单重复提交的巧妙方法

    2009-03-09 18:32:00
  • python math模块使用方法介绍

    2021-06-20 23:44:01
  • 详解php中反射的应用

    2023-11-15 01:26:56
  • Python二叉树定义与遍历方法实例分析

    2023-06-26 17:26:56
  • 教你如何在SQL Server数据库中加密数据

    2009-09-10 14:49:00
  • 利用Python实现数值积分的方法

    2023-07-11 10:16:31
  • python 判断txt每行内容中是否包含子串并重新写入保存的实例

    2023-11-28 06:34:56
  • 记录PHP错误日志 display_errors与log_errors的区别

    2023-11-14 09:38:29
  • Python字符串拼接的几种方法整理

    2021-05-08 18:55:47
  • 关于Python作用域自学总结

    2023-11-02 12:19:53
  • sql查询点滴记录

    2012-01-05 19:07:49
  • python实现人脸检测的简单实例

    2023-02-17 16:08:20
  • Selenium+BeautifulSoup+json获取Script标签内的json数据

    2023-06-17 09:30:18
  • 使用PyInstaller将Python程序文件转换为可执行程序文件

    2023-03-28 11:35:34
  • Python使用tkinter实现小时钟效果

    2022-08-14 09:00:18
  • Python中JSON的使用方法(超详细)

    2023-11-15 23:04:19
  • asp之家 网络编程 m.aspxhome.com