PyQt5在使用继承QMainWindow时,布局管理器的问题

2024-03-09 08:18

本文主要是介绍PyQt5在使用继承QMainWindow时,布局管理器的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用继承QMainWindow时,在里面创建使用布局管理器的时候会有些问题
代码示例:

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QButtonGroup, QFrame, QToolButton, QStackedLayout,\QWidget, QStatusBar, QBoxLayout, QLabel, QDesktopWidget, QMessageBox
from PyQt5.QtGui import QDesktopServicesimport sysclass Demo(QWidget):def __init__(self):super().__init__()self.setWindowTitle("测试")# 窗口大小self.resize(1400,800)# self.setFixedSize(1500, 600)  # 设置窗口为固定尺寸, 此时窗口不可调整大小# self.setMinimumSize(1800, 1000)  # 设置窗口最大尺寸# self.setMaximumSize(900, 300)  # 设置窗口最小尺寸     # self.setWindowFlag(Qt.WindowStaysOnTopHint)   # 设置窗口顶层显示# self.setWindowFlags(Qt.FramelessWindowHint)  # 设置无边框窗口样式,不显示最上面的标题栏self.content_font = QFont("微软雅黑", 12, QFont.Medium)  # 定义字体样式self.center()self.__setup_ui__()# 控制窗口显示在屏幕中心的方法def center(self):# 获得窗口qr = self.frameGeometry()# 获得屏幕中心点cp = QDesktopWidget().availableGeometry().center()# 显示到屏幕中心qr.moveCenter(cp)self.move(qr.topLeft())# 关闭窗口的时候,触发了QCloseEvent,需要重写closeEvent()事件处理程序,这样就可以弹出是否退出的确认窗口def closeEvent(self, event):reply = QMessageBox.question(self, "退出程序",   # 提示框标题"确定退出xxxx程序吗?",  # 消息对话框中显示的文本QMessageBox.Yes | QMessageBox.No,  # 指定按钮的组合 Yes和NoQMessageBox.No  # 默认的按钮焦点,这里默认是No按钮)# 判断按钮的选择if reply == QMessageBox.Yes:event.accept()else:event.ignore()def __setup_ui__(self):# 工具栏self.frame_tool = QFrame(self)self.frame_tool.setObjectName("frame_tool")self.frame_tool.setGeometry(0, 0, self.width(), 25)self.frame_tool.setStyleSheet("border-color: rgb(0, 0, 0);")self.frame_tool.setFrameShape(QFrame.Panel)self.frame_tool.setFrameShadow(QFrame.Raised)# 1.1 界面1按钮self.window1_btn = QToolButton(self.frame_tool)self.window1_btn.setCheckable(True)self.window1_btn.setText("window1")self.window1_btn.setObjectName("menu_btn")self.window1_btn.resize(100, 25)self.window1_btn.clicked.connect(self.click_window1)self.window1_btn.setAutoRaise(True)  # 去掉工具按钮的边框线如果是QPushButton按钮的话,就是用setFlat(True)这个方法,用法相同# 1.2 界面2按钮self.window2_btn = QToolButton(self.frame_tool)self.window2_btn.setCheckable(True)self.window2_btn.setText("window2")self.window2_btn.setObjectName("menu_btn")self.window2_btn.resize(100, 25)self.window2_btn.move(self.window1_btn.width(), 0)self.window2_btn.clicked.connect(self.click_window2)self.window2_btn.setAutoRaise(True)self.btn_group = QButtonGroup(self.frame_tool)self.btn_group.addButton(self.window1_btn, 1)self.btn_group.addButton(self.window2_btn, 2)# 1.3 帮助下拉菜单栏# 创建帮助工具按钮help_btn = QToolButton(self.frame_tool)help_btn.setText("帮助")help_btn.setObjectName("menu_btn")help_btn.resize(100, 25)help_btn.move(self.window2_btn.x() + self.window2_btn.width(), 0)help_btn.setAutoRaise(True)help_btn.setPopupMode(QToolButton.InstantPopup)# 创建关于菜单help_menu = QMenu("帮助", self.frame_tool)feedback_action = QAction(QIcon("xxx.png"), "反馈", help_menu)feedback_action.triggered.connect(self.click_feedback)about_action = QAction(QIcon("xxx.png"), "关于", help_menu)about_action.triggered.connect(self.click_about)# 把两个QAction放入help_menuhelp_menu.addAction(feedback_action)help_menu.addAction(about_action)# 把help_menu放入help_btnhelp_btn.setMenu(help_menu)# 2. 工作区域self.main_frame = QFrame(self)self.main_frame.setGeometry(0, 25, self.width(), self.height() - self.frame_tool.height())# self.main_frame.setStyleSheet("background-color: rgb(65, 95, 255)")# 创建堆叠布局self.stacked_layout = QStackedLayout(self.main_frame)# 第一个布局self.main_frame1 = QMainWindow()self.frame1_bar = QStatusBar()self.frame1_bar.setObjectName("frame1_bar")self.main_frame1.setStatusBar(self.frame1_bar)self.frame1_bar.showMessage("欢迎进入frame1")rom_frame = QFrame(self.main_frame1)rom_frame.setGeometry(0, 0 , self.width(), self.main_frame.height() - 25)rom_frame.setFrameShape(QFrame.Panel)rom_frame.setFrameShadow(QFrame.Raised)# 超链接self.super_link = QLabel(rom_frame)self.super_link.setText("""超链接: <a href="https://blog.csdn.net/s_daqing">点击打开查看</a>""")self.super_link.setGeometry(20, 30, 300, 25)self.super_link.setFont(self.content_font)  # 使用字体样式self.super_link.setOpenExternalLinks(True)  # 使其成为超链接self.super_link.setTextInteractionFlags(Qt.TextBrowserInteraction) # 双击可以复制文本self.start_btn = QPushButton("开 始", rom_frame)self.start_btn.setGeometry(self.width() * 0.7, self.height() * 0.8, 100, 40)# self.start_btn.clicked.connect(self.start_btn_click)self.quit_btn = QPushButton("退 出", rom_frame)self.quit_btn.setGeometry(self.width() * 0.85, self.height() * 0.8, 100, 40)# self.quit_btn.clicked.connect(QCoreApplication.instance().quit)  # 点击退出可以直接退出self.quit_btn.clicked.connect(self.close)  # 点击退出按钮的退出槽函数rom_frame1 = QFrame()rom_frame1.setFrameShape(QFrame.Panel)rom_frame1.setFrameShadow(QFrame.Raised)rom_frame2 = QFrame()rom_frame2.setFrameShape(QFrame.Panel)rom_frame2.setFrameShadow(QFrame.Raised)# 创建布局管理器self.layout1 = QBoxLayout(QBoxLayout.TopToBottom)# 给管理器对象设置父控件rom_frame.setLayout(self.layout1)self.main_frame1.setCentralWidget(rom_frame)# 把子控件添加到布局管理器中self.layout1.addWidget(rom_frame1, 1)self.layout1.addWidget(rom_frame2, 1)self.layout1.setContentsMargins(0, 0, 0, 0)  # 设置布局的左上右下外边距self.layout1.setSpacing(0)  # 设置子控件的内边距frame1_bar_frame = QFrame(self.main_frame1)frame1_bar_frame.setGeometry(0, self.main_frame.height(), self.width(), 25)# 第二个布局self.main_frame2 = QMainWindow()self.frame2_bar = QStatusBar()self.frame2_bar.setObjectName("frame2_bar")self.main_frame2.setStatusBar(self.frame2_bar)self.frame2_bar.showMessage("欢迎进入frame2")custom_frame = QFrame(self.main_frame2)custom_frame.setGeometry(0, 0 , self.width(), self.main_frame.height() - 25)custom_frame.setFrameShape(QFrame.Panel)custom_frame.setFrameShadow(QFrame.Raised)custom_frame1 = QFrame()custom_frame1.setFrameShape(QFrame.Panel)custom_frame1.setFrameShadow(QFrame.Raised)custom_frame2 = QFrame()custom_frame2.setFrameShape(QFrame.Panel)custom_frame2.setFrameShadow(QFrame.Raised)custom_frame3 = QFrame()custom_frame3.setFrameShape(QFrame.Panel)custom_frame3.setFrameShadow(QFrame.Raised)# 创建布局管理器self.layout2 = QBoxLayout(QBoxLayout.TopToBottom)# 给管理器对象设置父控件custom_frame.setLayout(self.layout2)"""使用了父类为QMainWindow的话,在里面使用布局类,QGridLayout, QHBoxLayout ,QVBoxLayout 等等时,发现不好用,加上下面这句代码就可以了,QMainWindow对象.setCentralWidget(这里填布局管理器的父控件对象)"""self.main_frame2.setCentralWidget(custom_frame)# 把子控件添加到布局管理器中self.layout2.addWidget(custom_frame1, 1)self.layout2.addWidget(custom_frame2, 1)self.layout2.addWidget(custom_frame3, 1)self.layout2.setContentsMargins(0, 0, 0, 0)  # 设置布局的左上右下外边距self.layout2.setSpacing(0)  # 设置子控件的内边距frame2_bar_frame = QFrame(self.main_frame2)frame2_bar_frame.setGeometry(0, self.main_frame.height(), self.width(), 25)# 把两个布局放进去self.stacked_layout.addWidget(self.main_frame1)self.stacked_layout.addWidget(self.main_frame2)def click_window1(self):if self.stacked_layout.currentIndex() != 0:self.stacked_layout.setCurrentIndex(0)self.frame1_bar.showMessage("欢迎进入frame1")def click_window2(self):if self.stacked_layout.currentIndex() != 1:self.stacked_layout.setCurrentIndex(1)self.frame2_bar.showMessage("欢迎进入frame2")QDesktopServices.openUrl(QUrl("https://www.csdn.net/")) # 点击window2按钮后,执行这个槽函数的时候,会在浏览器自动打开这个网址def click_feedback(self, event):QMessageBox.about(self, "反馈", "使用过程中如有疑问,请联系:xxxx.163.com\r\n\r\n版本:V1.0.1")def click_about(self, event):QMessageBox.about(self, "关于", "使用文档,请参考:xxxxxx")if __name__ == "__main__":app = QApplication(sys.argv)window = Demo()window.show()sys.exit(app.exec_())

展示

在这里插入图片描述

这篇关于PyQt5在使用继承QMainWindow时,布局管理器的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/790067

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没