博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PyQt5布局管理
阅读量:3898 次
发布时间:2019-05-23

本文共 3921 字,大约阅读时间需要 13 分钟。

PyQt5布局有两种方式,绝对定位和布局类

import sysfrom PyQt5.QtWidgets import QWidget, QLabel, QApplicationclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        lbl1 = QLabel('Zetcode', self)        lbl1.move(15, 10)        lbl2 = QLabel('tutorials', self)        lbl2.move(15, 70)		#绝对位置(35, 10)        lbl3 = QLabel('for programmers', self)        lbl3.move(70, 70)		#设置窗口位置和大小        self.setGeometry(300, 300, 250, 150)        self.setWindowTitle('Absolute Positioning')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    execute = Example()    sys.exit(app.exec_())

在这里插入图片描述

使用QHBoxLayout和QVBoxLayout,来分别创建横向布局和纵向布局

import sysfrom PyQt5.QtWidgets import QWidget, QLabel, QApplicationimport sysfrom PyQt5.QtWidgets import (QWidget, QPushButton,                             QHBoxLayout, QVBoxLayout, QApplication)class Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        okButton = QPushButton("OK1")        cancelButton = QPushButton("Cancel")        okButton1 = QPushButton("OK2")        cancelButton1 = QPushButton("Cance2")        hbox1 = QHBoxLayout()        #添加横向伸展因子,靠右显示        hbox1.addStretch(1)        hbox1.addWidget(okButton)        hbox1.addWidget(cancelButton)        hbox2 = QHBoxLayout()        #hbox2.addStretch(1)        hbox2.addWidget(okButton1)        hbox2.addWidget(cancelButton1)        vbox = QVBoxLayout()       #添加纵向伸展因子,靠底部显示        vbox.addStretch(1)        vbox.addLayout(hbox1)        vbox.addLayout(hbox2)        self.setLayout(vbox)        self.setGeometry(300, 300, 300, 150)        self.setWindowTitle('Buttons')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

使用QGridLayout类创建一个网格布局

import sysfrom PyQt5.QtWidgets import (QWidget, QGridLayout,                             QPushButton, QApplication)class Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        #创建一个网格        grid = QGridLayout()        self.setLayout(grid)        #按钮的标签        names = ['Cls', 'Bck', '', 'Close',                 '7', '8', '9', '/',                 '4', '5', '6', '*',                 '1', '2', '3', '-',                 '0', '.', '=', '+']        positions = [(i, j) for i in range(5) for j in range(4)]        for position, name in zip(positions, names):            if name == '':                #标签为空时,不创建按钮                continue            button = QPushButton(name)            grid.addWidget(button, *position)        self.move(300, 150)        self.setWindowTitle('Calculator')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

文本编辑框

import sysfrom PyQt5.QtWidgets import (QWidget, QGridLayout,                             QPushButton, QApplication,QTextEdit)class Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        #创建一个网格        grid = QGridLayout()        self.setLayout(grid)        text_edit = QTextEdit()        #部件的行和列跨,这里的跨度为1行,4列        grid.addWidget(text_edit,0,0,1,4)        #组件的间隔        #grid.setSpacing(5)        #按钮的标签        names = ['Cls', 'Bck', '', 'Close',                 '7', '8', '9', '/',                 '4', '5', '6', '*',                 '1', '2', '3', '-',                 '0', '.', '=', '+']        positions = [(i, j) for i in range(1,6) for j in range(4)]        for position, name in zip(positions, names):            if name == '':                #标签为空时,不创建按钮                continue            button = QPushButton(name)            grid.addWidget(button, *position)        self.move(300, 150)        self.setWindowTitle('Calculator')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

转载地址:http://oaben.baihongyu.com/

你可能感兴趣的文章
记录几点在开发中遇到的问题 2015-7-28 (会更新)
查看>>
网银在线的异步操作代码示意图
查看>>
火狐Firefox浏览器安装Selenium_IDE的步骤以及其使用规则
查看>>
记录运行代码的时间长短
查看>>
关于yii2的一些知识的学习笔述
查看>>
用纯php实现MVC框架,文件目录模仿yii2
查看>>
新开发的体重管理项目----用纯php模仿yii2框架建立的
查看>>
JavaScript面向对象编程指南 的笔记
查看>>
在 2016 年做 PHP 开发是一种什么样的体验?(一)
查看>>
PHP获取客户端的IP
查看>>
从头开始学习yii2---1.搭建yii2开发环境
查看>>
从头开始学习yii2---3.语言包的配置
查看>>
yii2-表单验证的一些规则
查看>>
索引相关问题
查看>>
php面试可能会被问道的技术题汇总
查看>>
php面试题1-线程和进程的区别(顺带提下协程)
查看>>
php面试题2-用到过的传输协议
查看>>
php面试题3-yii2和yii的不一样的地方
查看>>
IOS 一些好的框架和 技术大牛的博客
查看>>
Java 和 Object-c的区别
查看>>