二、PyQt5实现Python界面设计_QLayout(界面分布器)

目录

一、介绍

 二、实例

(1)QBoxLayout(QHBoxLayout)水平分布管理器

 (2)QBoxLayout(QVBoxLayout)垂直分布管理器

 (3)QBoxLayout的嵌套使用

 (4)QGridLayout网格布局器

(5)QFormLayout表单分布器


一、介绍

在窗体设计中,位置的摆放是一个最基本且关键的步骤,它直接影响到整个窗体的美观,

在PyQt5中有两种方式可以进行控件的位置摆放,一种是绝对定位,一种十分布管理器。

绝对定位的好处在于,可以根据坐标准放置在需要的位置上,一般使用Move()函数,其缺点就是无法随着窗体大小的变化而变化

而布局管理器的优点则体现在控件可以跟随窗口大小的变化做自动的适应,自动的更新界面控件的大小

 二、实例

在布局管理器中,一般会使用到两个方法,一个是addWidget()用来添加控件的,一个是addLayout()用来嵌套分布管理器的。

(1)QBoxLayout(QHBoxLayout)水平分布管理器

import sys
from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QToolTip,QPushButton,QWidget
from PyQt5.QtGui import QFont

class TooltipForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        QToolTip.setFont(QFont('SansSerif',12))#设置说明字体
        self.setToolTip('今天是<b>星期二</b>')#设置窗口说明
        self.setGeometry(300,30,200,300)#设置窗口位置及大小
        self.setWindowTitle('设置控件提示消息')#设置窗口标题

        self.button1 = QPushButton('Button1')#实例一个按钮控件
        self.button1.setToolTip('这是一个按钮')#设置按钮说明
        self.button2=QPushButton('Button2')#实例一个按钮控件
        
        layout = QHBoxLayout()#实例一个水平分布器
        #将按钮分别放入水平分布器
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        
        #QMainWindow中在setUi时自动为用户创建了一个菜单栏、工具栏、中心窗口和状态栏。
        #而QWidget是没有这几点的。
        #QWidget运行后就只有一个“页面”
        #而QMainWindow运行后生成了一个“窗口”。
        
        mainFrame = QWidget()#实例一个QWidget工作区创空
        mainFrame.setLayout(layout)#将分布器嵌入到工作区内
        self.setCentralWidget(mainFrame)#将工作区嵌入窗体中去

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = TooltipForm()
    main.show()
    app.exec_()
    del app

 (2)QBoxLayout(QVBoxLayout)垂直分布管理器

import sys
from PyQt5.QtWidgets import QVBoxLayout,QHBoxLayout,QMainWindow,QApplication,QToolTip,QPushButton,QWidget
from PyQt5.QtGui import QFont

class TooltipForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        QToolTip.setFont(QFont('SansSerif',12))#设置说明字体
        self.setToolTip('今天是<b>星期二</b>')#设置窗口说明
        self.setGeometry(300,30,200,300)#设置窗口位置及大小
        self.setWindowTitle('设置控件提示消息')#设置窗口标题

        self.button1 = QPushButton('Button1')#定义一个按钮控件
        self.button1.setToolTip('这是一个按钮')#设置按钮说明
        self.button2=QPushButton('Button2')#定义按钮控件
        
        layout = QVBoxLayout()#定义一个垂直分布器
        #添加按钮到分布器内
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        
        mainFrame = QWidget()#定义一个嵌入窗体
        mainFrame.setLayout(layout)#将分布器嵌入到嵌入窗体内
        self.setCentralWidget(mainFrame)#将嵌入窗体嵌入到主窗体内

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = TooltipForm()
    main.show()
    app.exec_()
    del app

 (3)QBoxLayout的嵌套使用

import sys
from PyQt5.QtWidgets import QVBoxLayout,QHBoxLayout,QMainWindow,QApplication,QToolTip,QPushButton,QWidget
from PyQt5.QtGui import QFont

class TooltipForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        QToolTip.setFont(QFont('SansSerif',12))#设置说明字体
        self.setGeometry(300,30,200,300)#设置窗体位置及大小
        self.setWindowTitle('嵌套布局')#设置窗体标题

        #定义四个按钮控件对象
        self.button1 = QPushButton('Button1')
        self.button2=QPushButton('Button2')
        self.button3=QPushButton('Button3')
        self.button4=QPushButton('Button4')
        
        #定义一个水平分布器与两个垂直分布器
        layout = QHBoxLayout()
        layout1= QVBoxLayout()
        layout2= QVBoxLayout()
        #将两个垂直分布器放入水平分布器内
        layout.addLayout(layout1)
        layout.addLayout(layout2)
        #将按钮控件分别放入两个垂直分布器内
        layout1.addWidget(self.button1)
        layout1.addWidget(self.button2)
        layout2.addWidget(self.button3)
        layout2.addWidget(self.button4)
        
        mainFrame = QWidget()#定义一个嵌入窗体
        mainFrame.setLayout(layout)#将分布器嵌入到嵌入窗体内
        self.setCentralWidget(mainFrame)#将嵌入窗体嵌入到窗体内

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = TooltipForm()
    main.show()
    app.exec_()
    del app

 (4)QGridLayout网格布局器

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont

class TooltipForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        QToolTip.setFont(QFont('SansSerif',12))
        self.setWindowTitle('设置网格布局')
#       self.setGeometry(300,30,200,300)
        button1 = QPushButton('Button1',self)
        button2 = QPushButton('Button2',self)
        button3 = QPushButton('Button3',self)
        button4 = QPushButton('Button4',self)
        button5 = QPushButton('Button5',self)
        button6 = QPushButton('Button6',self)
        
        layout = QGridLayout()
        layout.addWidget(button1,0,0,1,1)#第0行第0列开始,占1行1列
        layout.addWidget(button2,0,1,1,2)#第0行第1列开始,占1行2列
        layout.addWidget(button3,1,0,1,1)#第1行第0列开始,占1行1列
        layout.addWidget(button4,1,1,1,2)#第1行第1列开始,占1行2列
        layout.addWidget(button5,2,1,1,1)#第2行第1列开始,占1行1列
        layout.addWidget(button6,2,2,1,1)#第2行第2列开始,占1行1列
       
        mainFrame = QWidget()
        mainFrame.setLayout(layout)
        self.setCentralWidget(mainFrame)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = TooltipForm()
    main.show()
    app.exec_()
    del app

 但当拉动窗体使其变长时间,则会发现,比例间距有点过大

使用表单分布器则不会出现这类问题

(5)QFormLayout表单分布器

from PyQt5.QtWidgets import *

import sys

class QLineEditEchoMode(QWidget) :
    def __init__(self):
        super(QLineEditEchoMode,self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('设置表单布局')
        self.setGeometry(300,30,200,300)
        button1 = QPushButton('Button1',self)
        button2 = QPushButton('Button2',self)
        button3 = QPushButton('Button3',self)
        button4 = QPushButton('Button4',self)
        button5 = QPushButton('Button5',self)
        button6 = QPushButton('Button6',self)

        formLayout = QFormLayout()
        
        formLayout.addRow(button1,button2)
        formLayout.addRow(button3,button4)
        formLayout.addRow(button5,button6)
        
        self.setLayout(formLayout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLineEditEchoMode()
    main.show()
    app.exec_()
    del app

 

 四类分布器均可以做嵌套使用

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Python 中使用 PyQt5 实现多层界面设计可以使用 QStackedWidget 组件。 QStackedWidget 组件是一个容组件,可以在其中嵌入多个子界面,并通过设置当前层来显示不同的子界面。 使用方法如下: 1. 创建 QStackedWidget 对象 2. 创建多个子界面(QWidget 对象) 3. 将子界面添加到 QStackedWidget 中 4. 设置当前层来显示不同的子界面 下面是一个简单的例子: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QStackedWidget, QVBoxLayout class MainWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # 创建 QStackedWidget 对象 stacked_widget = QStackedWidget() # 创建多个子界面 page1 = QWidget() page2 = QWidget() page3 = QWidget() # 将子界面添加到 QStackedWidget 中 stacked_widget.addWidget(page1) stacked_widget.addWidget(page2) stacked_widget.addWidget(page3) # 设置当前层 stacked_widget.setCurrentIndex(0) # 在窗口中添加 QStackedWidget 并显示 layout = QVBoxLayout() layout.addWidget(stacked_widget) self.setLayout(layout) self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec_()) ``` 在这个例子中,我们创建了三个子界面(page1、page2、page3)并将它们添加到 QStackedWidget 中,然后设置当前层为第一层,就可以在窗口中显示第一 ### 回答2: 利用PythonPyQt5可以实现多层界面设计,具体步骤如下: 首先,我们需要导入PyQt5库,并创建一个主窗口类。 ```python from PyQt5.QtWidgets import QApplication, QMainWindow class MainWindow(QMainWindow): def __init__(self): super().__init__() # 设置主窗口的标题和大小 self.setWindowTitle("多层界面设计示例") self.setGeometry(100, 100, 300, 200) # 创建一个按钮并添加到主窗口上 button = QPushButton("打开第界面", self) button.move(100, 100) ``` 然后,我们需要创建第界面。在第一层界面的按钮点击事件中,可以打开第界面。 ```python from PyQt5.QtWidgets import QDialog, QLabel class SecondWindow(QDialog): def __init__(self): super().__init__() # 设置第界面的标题和大小 self.setWindowTitle("第界面") self.setGeometry(200, 200, 300, 200) # 创建一个标签并添加到第界面上 label = QLabel("这是第界面", self) label.move(100, 100) ``` 最后,我们需要在主程序中实例化主窗口类,并展示界面。 ```python if __name__ == "__main__": import sys # 创建一个应用程序对象 app = QApplication(sys.argv) # 实例化主窗口类 main_window = MainWindow() # 在点击按钮时打开第界面 button.clicked.connect(lambda: open_second_window()) def open_second_window(): second_window = SecondWindow() second_window.exec() # 显示主窗口 main_window.show() # 进入主循环 sys.exit(app.exec()) ``` 这样,就实现了通过点击主窗口上的按钮,打开第界面的多层界面设计。在主窗口和第界面中,我们可以根据需要添加更多的功能和控件。 ### 回答3: 利用PythonPyQt5可以实现多层界面设计PyQt5是一个Python的GUI框架,可以用于创建各种类型的界面。多层界面设计通常包括主界面和子界面的切换,可以通过使用PyQt5的QStackedWidget(堆叠窗口小部件)来实现。 首先,我们需要导入PyQt5库,使用以下代码实现: ```python from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * ``` 然后,创建一个继承自QMainWindow的主窗口类,设置主界面的布局和样式。在主界面中添加按钮或其他控件,用于触发显示子界面的事件。 接下来,创建继承自QWidget的子窗口类,用于显示子界面的内容。在子界面中添加所需的控件和布局,根据需求进行设计。 在主窗口类中,我们可以为按钮或其他控件添加信号和槽函数,用于切换显示子界面。在槽函数中,我们可以使用QStackedWidget的setCurrentIndex方法来切换显示的界面。例如: ```python self.stacked_widget.setCurrentIndex(1) # 显示第个子界面 ``` 添加完所有的子界面后,我们可以将它们添加到QStackedWidget中,使用addWiget方法来进行添加。例如: ```python self.stacked_widget.addWidget(ChildWindow1()) # 添加第一个子界面 self.stacked_widget.addWidget(ChildWindow2()) # 添加第个子界面 ``` 最后,我们需要在主窗口的布局中添加QStackedWidget,并设置好布局和样式。 通过以上步骤,利用PythonPyQt5,我们可以实现多层界面设计。当用户在主界面点击按钮或其他控件时,可以切换显示子界面,以实现不同功能或模块的展示和操作。同时,我们还可以通过设置子界面的布局和样式来满足设计要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值