PyQT5之窗口
窗口类型:
QMainWindow:可以包含菜单栏、工具栏、状态栏、标题栏
QWidget:不确定窗口的用途,就使用QWidget
QDialog: 是对话窗口的基类,没有菜单栏、工具栏、状态栏
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QMainWindow, QApplication, QPushButton, QHBoxLayout
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import QIcon
import sys
import cv2
class FirstMainWin(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# 设置主窗口的标题
self.setWindowTitle('第一个主窗口应用,并居中')
# 设置窗口的尺寸
self.resize(400, 300)
self.status = self.statusBar() # 设置状态栏
self.status.showMessage("只存在5秒的消息", 5000)
self.btn1 = QPushButton("退出应用程序")
# 将信号与槽关联
self.btn1.clicked.connect(self.on_click_button)
layout = QHBoxLayout()
layout.addWidget(self.btn1)
main_frame = QWidget()
main_frame.resize(300, 240)
self.print_size(main_frame)
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
def print_size(self, main_frame):
print(main_frame.x(), main_frame.y(), main_frame.width(), main_frame.height())
print(main_frame.geometry().x(), main_frame.geometry().y(),
main_frame.geometry().width(), main_frame.geometry().height())
print(main_frame.frameGeometry().x(), main_frame.frameGeometry().y(),
main_frame.frameGeometry().width(), main_frame.frameGeometry().height())
def on_click_button(self):
sender = self.sender()
print(sender.text() + "按钮被按下")
app = QApplication.instance()
# 对出应用程序
app.quit()
def center(self):
# 获取屏幕坐标系
screen = QDesktopWidget().screenGemotry()
# 获取窗口坐标系
size = self.geometry()
new_left = (screen.width() - size.width()) / 2
new_top = (screen.height() - size.height()) / 2
self.move(new_left, new_top)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('./image/icon/特性.png'))
main_win = FirstMainWin()
main_win.show()
sys.exit(app.exec_())