= array(); } $post['classname'] = 'post'; } function comment_format(&$post) { global $conf, $uid, $gid, $forumlist; if (empty($post)) return; $forum = $post['fid'] ? forum_read($post['fid']) : ''; $thread = well_thread_read_cache($post['tid']); if ($thread) { //$post['fid'] = $thread['fid']; $post['closed'] = $thread['closed']; $post['subject'] = $thread['subject']; $post['url'] = $thread['url']; } else { $post['closed'] = 0; $post['subject'] = lang('thread_not_exists'); $post['url'] = ''; } $post['create_date_fmt'] = humandate($post['create_date']); //$post['message'] = stripslashes(htmlspecialchars_decode($post['message'])); $user = user_read_cache($post['uid']); $post['username'] = array_value($user, 'username'); $post['user_avatar_url'] = array_value($user, 'avatar_url'); $post['user'] = $user ? user_safe_info($user) : user_guest(); isset($post['floor']) || $post['floor'] = 0; // 权限判断 $post['allowupdate'] = 2 == array_value($forum, 'comment', 0) && ($uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowupdate')); $post['allowdelete'] = group_access($gid, 'allowuserdelete') && $uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowdelete'); $post['user_url'] = url('user-' . $post['uid'] . ($post['uid'] ? '' : '-' . $post['pid'])); if ($post['files'] > 0) { list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($post['pid']); // 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法 if (2 == $conf['attach_on']) { foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换成图床 $post['message'] = FALSE !== strpos($post['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $post['message']) : $post['message']; } } $post['filelist'] = $filelist; } else { $post['filelist'] = array(); } $post['classname'] = 'post'; } function comment_format_message(&$val) { global $conf; if (empty($val)) return; // 使用云储存 if (1 == $conf['attach_on'] && 1 == $val['attach_on']) { $val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']); } elseif (2 == $conf['attach_on'] && 2 == $val['attach_on']) { // 使用图床 list($attachlist, $imagelist, $filelist) = well_attach_find_by_tid($val['tid']); foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换成图床 $val['message'] = FALSE !== strpos($val['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $val['message']) : $val['message']; } } else { $val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']); } //$val['message'] = stripslashes(htmlspecialchars_decode($val['message'])); } // 把内容中使用了云储存的附件链接替换掉 function comment_message_replace_url($pid, $message) { global $conf; if (0 == $conf['attach_on']) { $message = FALSE !== strpos($message, '="../upload/') ? str_replace('="../upload/', '="upload/', $message) : $message; $message = FALSE !== strpos($message, '="/upload/') ? str_replace('="/upload/', '="upload/', $message) : $message; } elseif (1 == $conf['attach_on']) { // 使用云储存 $message = str_replace('="' . $conf['cloud_url'] . 'upload/', '="upload/', $message); } elseif (2 == $conf['attach_on']) { // 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法 list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($pid); foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换回相对链接 $message = $attach['image_url'] && FALSE !== strpos($message, $attach['image_url']) ? str_replace($attach['image_url'], $url, $message) : $message; } } return $message; } function comment_filter($val) { unset($val['userip']); return $val; } function comment_highlight_keyword($str, $k) { $r = str_ireplace($k, '' . $k . '', $str); return $r; } // //
function comment_message_format(&$s) { if (xn_strlen($s) < 100) return; $s = preg_replace('#.*?
#is', '', $s); $s = str_ireplace(array('
', '
', '
', '

', '', '', '', '' . ''), "\r\n", $s); $s = str_ireplace(array(' '), " ", $s); $s = strip_tags($s); $s = preg_replace('#[\r\n]+#', "\n", $s); $s = xn_substr(trim($s), 0, 100); $s = str_replace("\n", '
', $s); } // 对内容进行引用 function comment_quote($quotepid) { $quotepost = comment_read($quotepid); if (empty($quotepost)) return ''; $uid = $quotepost['uid']; $s = $quotepost['message']; $s = comment_brief($s, 100); $userhref = url('user-' . $uid); $user = user_read_cache($uid); $r = '
' . $user['username'] . ' ' . $s . '
'; return $r; } // 获取内容的简介 0: html, 1: txt; 2: markdown; 3: ubb function comment_brief($s, $len = 100) { $s = strip_tags($s); $s = htmlspecialchars($s); $more = xn_strlen($s) > $len ? ' ... ' : ''; $s = xn_substr($s, 0, $len) . $more; return $s; } ?>Eric6与pyqt5学习笔记 3【水平,垂直,网格,窗体四大类布局】
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Eric6与pyqt5学习笔记 3【水平,垂直,网格,窗体四大类布局】

网站源码admin6浏览0评论

Eric6与pyqt5学习笔记 3【水平,垂直,网格,窗体四大类布局】

Eric6与pyqt5学习笔记 3【水平,垂直,网格,窗体四大类布局】

    ummm,这段博主时间比较忙,准备下周的比赛还有一个app的开发(即将放到github,欢迎提出意见~),所以学习了好几天才更新第三篇笔记~

    上篇学习笔记提到了从最基本的setGeometry()设置窗口,也提到了居中,利用QDesktopWidget类来解决这个问题,但是,按钮大小和位置也还是固定设置的,这里,带大家继续学习,如何布局~

1.布局常用类

 

常用到的布局类有QHBoxLayout,QVBoxLayout,QGridLayout ,QFormLayout4种,分别水平排列布局,垂直排列布局,表格排列布局,以及。布局中最常用的方法有addWidget()和addLayout(),addWidget()方法用于在布局中插入控件,addLayout()用于在布局中插入子布局。

水平布局管理器(QHBoxLayout)可以把添加的控件以水平的顺序依次排开;
垂直布局管理器(QVBoxLayout)可以把添加的控件以垂直的顺序依次排开;
网格布局管理器(QGridLayout)可以以网格的形式,把添加的控件以一定矩阵排列;
窗体布局管理器(QFormLayout)可以以两列的形式排列所添加的控件

 

a).水平(垂直)布局管理:

 

QHBoxLayout,按照从左到右的顺序进行添加控件。
QVBoxLayout,按照从上到下的顺序进行添加控件。

常用方法:
addLayout(…)
QBoxLayout.addLayout(QLayout)
在box的底部添加布局,其中默认的拉伸因子为0

addWidget(…)
QBoxLayout.addWidget(QWidget)
为布局中添加控件,stretch(拉伸因子)只适用与QBoxLayout,widget和box会随着stretch的变大而增大;alignment指定对齐的方式

addStretch(...)

设置拉伸因子

addSpacing(…)
QBoxLayout.addSpacing(int)
通过该方法增加额外的space。

 

b).网格布局管理器:

将窗体分隔成行和列的网格来进行排列(可以认为是二维的数据排布)。通常可以使用函数addWidget()或者addLayout()来将被管理的widget或者layout添加到窗格中。也可以通过重载的函数addWidget()或者addLayout()的行和列跨度实现占据多个窗格。

添加组件
addWidget(…)
QGridLayout.addWidget(QWidget)
QGridLayout.addWidget(QWidget * widget, int row, int column, Qt.Alignment alignment = 0 )
QWidget:为所添加的组件
row:为组件要添加的行数,默认从0开始
column:为组件要添加到的列数,默认从0开始
alignment:对齐的方式

QGridLayout.addWidget(QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt.Alignment alignment = 0)
当添加的组件跨越很多行或者列的时候,使用该方法。
QWidget:为所添加的组件
fromRow:为组件起始的行数
fromColumn:为组件起始的列数
rowSpan:为组件跨越的行数
columnSpan:为组件跨越的列数
alignment:对齐的方式

addLayout(…)
QGridLayout.addLayout(QLayout, int, int, Qt.Alignment alignment=0)
QGridLayout.addLayout(QLayout, int, int, int, int, Qt.Alignment alignment=0)

其中参数说明同addWidget.

 

c).窗体布局管理器:

 

在窗口中按照两列的形式排列控件

addRow(…)
QFormLayout.addRow(QWidget, QWidget)
QFormLayout.addRow(QWidget, QLayout)
QFormLayout.addRow(str, QWidget)
QFormLayout.addRow(str, QLayout)
QFormLayout.addRow(QWidget)
QFormLayout.addRow(QLayout)

 

2.几种窗口布局的说明:

 

a).水平(垂直)布局

 

# -*- coding:utf-8 -*- 
'''@Author:      GETF@Email:       GETF_own@163@DateTime:    2017-11-01 16:07:44@Description: Description 
'''# Form implementation generated from reading ui file 'F:\pythonexe\hello\hello.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!import sys
from PyQt5.QtWidgets import QWidget, QPushButton,QHBoxLayout, QVBoxLayout, QApplication
from PyQt5.QtWidgets import QDesktopWidget
import os
from PyQt5.QtGui import QIconclass Hello(QWidget):def __init__(self):super().__init__()self.initUI()def center(self):qr = self.frameGeometry()cp = QDesktopWidget().availableGeometry().center()qr.moveCenter(cp)self.move(qr.topLeft()) def initUI(self):okButton = QPushButton("确定")cancelButton = QPushButton("取消")test1Button = QPushButton("确")test2Button = QPushButton("取")hbox = QHBoxLayout()#水平布局1hbox.addStretch(1)#设置下一个布局拉伸因子为1hbox.addWidget(okButton)hbox.addStretch(1)hbox.addWidget(cancelButton)hbox2 = QHBoxLayout()#水平布局2hbox2.addStretch(2)hbox2.addWidget(test1Button)hbox2.addWidget(test2Button)#没设置拉伸因子,默认为0vbox = QVBoxLayout()#竖直布局vbox.addStretch(1)vbox.addLayout(hbox)#在底部增加boxvbox.addStretch(2)vbox.addLayout(hbox2)#在底部增加boxself.setLayout(vbox)    self.center()#移至屏幕中央self.resize(300, 300)#设置窗口大小dir_path = os.path.abspath(os.path.dirname(__file__))+'\image\\favicon.ico'self.setWindowIcon(QIcon(dir_path))self.setWindowTitle('Hello')self.show()if __name__ == '__main__':app = QApplication(sys.argv)ex = Hello()sys.exit(app.exec_())


 

 

 

从上述效果图,可以很明显看到拉伸因子的作用,ummm,言语讲不太清楚了,大家自己修改设置布局拉伸因子的数值去感受一下吧

 

 

b).网格布局管理器:

代码放在下面了~

 

# -*- coding:utf-8 -*- 
'''@Author:      GETF@Email:       GETF_own@163@DateTime:    2017-11-01 23:58:45@Description: Description 
'''# Form implementation generated from reading ui file 'F:\pythonexe\hello\hello.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!import sys
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QApplication,QDesktopWidget
import os
from PyQt5.QtGui import QIconclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def center(self):qr = self.frameGeometry()cp = QDesktopWidget().availableGeometry().center()qr.moveCenter(cp)self.move(qr.topLeft()) def initUI(self):grid = QGridLayout()#创建一个表格布局self.setLayout(grid)names = ['清空', '删除', '', '启动','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 == '':continuebutton = QPushButton(name)grid.addWidget(button, *position)#利用zip性质传参'''zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。利用*号操作符,可以将list unzip(解压)。关于*position的测试如下:测试一:names = ['test1', 'test2', '', 'test3','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 == '':continueprint(position)print(*position)print(name)(0, 0)0 0test1(0, 1)0 1test2(0, 3)0 3test3测试二:names = ['test1', 'test2', '', 'test3','7', '8', '9', '/','4', '5', '6', '*','1', '2', '3', '-','0', '.', '=', '+']positions = [(i,j) for i in range(5) for j in range(4)]#五行四列for name in zip(positions, names):#利用一个循环来创建按钮print(name)((0, 0), 'test1')((0, 1), 'test2')((0, 2), '')((0, 3), 'test3')测试三:names = ['test1', 'test2', '', 'test3','7', '8', '9', '/','4', '5', '6', '*','1', '2', '3', '-','0', '.', '=', '+']positions = [(i,j) for i in range(5) for j in range(4)]#五行四列for name in zip(positions):#利用一个循环来创建按钮print(name)print(*name[0])((0, 0),)0 0((0, 1),)0 1((0, 2),)0 2((0, 3),)注释:测试结果只显示部分,表明输出来的格式'''   self.center()self.setWindowTitle('计算器')dir_path = os.path.abspath(os.path.dirname(__file__))+'\image\\favicon.ico'self.setWindowIcon(QIcon(dir_path))self.show()if __name__ == '__main__':app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())


效果图如下:

 

 

哈哈哈,很像一个计算器啊,明天有时间就试试把这个空壳写成一个完整的计算器吧~

之后补代码

 

 

c).窗体布局管理器:

这里综合上面的计算器(界面)和第一个提到的竖直水平窗体完成了一个嵌套窗体,代码如下:

 

 

# -*- coding:utf-8 -*- 
'''@Author:      GETF@Email:       GETF_own@163@DateTime:    2017-11-01 23:58:45@Description: Description 
'''# Form implementation generated from reading ui file 'F:\pythonexe\hello\hello.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!import sys
from PyQt5.QtWidgets import (QWidget, QFormLayout, QPushButton, QApplication,QDesktopWidget,QVBoxLayout,QGridLayout,QGroupBox,QLabel,QLineEdit)
import os
from PyQt5.QtGui import QIconclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def center(self):#居中qr = self.frameGeometry()cp = QDesktopWidget().availableGeometry().center()qr.moveCenter(cp)self.move(qr.topLeft()) def initUI(self):self.createGridGroupBox()self.creatFormGroupBox()vbox = QVBoxLayout()#创建一个竖直布局vbox.addWidget(self.formGroupBox)vbox.addWidget(self.gridGroupBox)self.setLayout(vbox)#上面先创建了一个竖直布局,然后在该布局中塞入两个布局,分别是窗体布局和表格布局#self.createGridGroupBox(),self.creatFormGroupBox()这两个自定义来管理窗体布局和表格布局self.center()self.setWindowTitle('hello')dir_path = os.path.abspath(os.path.dirname(__file__))+'\image\\favicon.ico'self.setWindowIcon(QIcon(dir_path))self.show()def createGridGroupBox(self):self.gridGroupBox = QGroupBox()#这几个类的父类grid = QGridLayout()#创建一个表格布局names = ['清空', '删除', '', '启动','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 == '':continuebutton = QPushButton(name)grid.addWidget(button, *position)#利用zip性质传参self.gridGroupBox.setLayout(grid)#将布局设置过去def creatFormGroupBox(self):self.formGroupBox = QGroupBox()form = QFormLayout()#创建一个窗体布局performanceLabel = QLabel("表达式:")#设置不可修改文字performanceEditor = QLineEdit("1+1")#设置可被修改文字form.addRow(performanceLabel,performanceEditor)self.formGroupBox.setLayout(form)if __name__ == '__main__':app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())


效果图如下:

 

 

 

 

 

~有问题欢迎交流

 

 

 

 

 

 

发布评论

评论列表(0)

  1. 暂无评论