• 基本布局的管理


    代码:

     1 #coding: utf-8
     2 from PyQt4.QtGui import *
     3 from PyQt4.QtCore import *
     4 import sys
     5 
     6 QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
     7 
     8 class LayoutDialog(QDialog):
     9     def __init__(self, parent=None):
    10         super(LayoutDialog, self).__init__(parent)
    11         self.setWindowTitle(self.tr("用户信息"))
    12 
    13         label1 = QLabel(self.tr("用户名:"))
    14         label2 = QLabel(self.tr("姓名:"))
    15         label3 = QLabel(self.tr("性别:"))
    16         label4 = QLabel(self.tr("年龄:"))
    17         label5 = QLabel(self.tr("部门:"))
    18         otherLabel = QLabel(self.tr("备注:"))
    19         #设置控件的风格,setFrameStyle()是QFrame 的方法,参数以或的方式设定控件的面板风格,由形式(QFrame.Shape)和阴影(QFrame.Shadow)两项配合设定。
           #其中,形状有 NoFrame,Panel,Box,HLine,VLine 以及WinPanel6 种,阴影有 Plain,Raised 和 Sunken3 种
    20 otherLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken) 21 userLineEdit = QLineEdit() 22 nameLineEdit = QLineEdit() 23 sexComboBox = QComboBox() 24 sexComboBox.insertItem(0, self.tr("")) 25 sexComboBox.insertItem(1, self.tr("")) 26 departmentTextEdit = QTextEdit() 27 ageLineEdit = QLineEdit() 28 29 labelCol = 0 30 contentCol = 1 31 #定义一个 QGridLayout 对象 leftLayout,由于此布局管理器并不是主布局管理器,因此不用指定父窗口,最后由主布局管理器统一指定 32 leftLayout = QGridLayout() 33 leftLayout.addWidget(label1, 0, labelCol) 34 leftLayout.addWidget(userLineEdit, 0, contentCol) 35 leftLayout.addWidget(label2, 1, labelCol) 36 leftLayout.addWidget(nameLineEdit, 1, contentCol) 37 leftLayout.addWidget(label3, 2, labelCol) 38 leftLayout.addWidget(sexComboBox, 2, contentCol) 39 leftLayout.addWidget(label4, 3, labelCol) 40 leftLayout.addWidget(departmentTextEdit, 3, contentCol) 41 leftLayout.addWidget(label5, 4, labelCol) 42 leftLayout.addWidget(ageLineEdit, 4, contentCol) 43 leftLayout.addWidget(otherLabel, 5, labelCol, 1, 2) 44 leftLayout.setColumnStretch(0, 1) 45 leftLayout.setColumnStretch(1, 3) 46 #现对话框右上侧的头像选择区的布局, 此处采用一个 QHBoxLayout 类进行布局管理。QHBoxLayout 默认采取自左向右的方式顺序排列插入的控件 47 #也可通过调用setDirection()方法设定排列的顺序,例如:hLayout.setDirection(QBoxLayout.RightToLeft) 48 label6 = QLabel(self.tr("头像:")) 49 iconLabel = QLabel() 50 icon = QPixmap("image2.jpg") 51 iconLabel.setPixmap(icon) 52 iconLabel.resize(icon.width(), icon.height()) 53 iconPushButton = QPushButton(self.tr("改变")) 54 hLayout = QHBoxLayout() 55 hLayout.setSpacing(20) #设定各个控件之间的间距为 20 56 hLayout.addWidget(label6) 57 hLayout.addWidget(iconLabel) 58 hLayout.addWidget(iconPushButton) 59 60 label7 = QLabel(self.tr("个人说明:")) 61 descTextEdit = QTextEdit() 62 63 rightLayout = QVBoxLayout() 64 rightLayout.setMargin(10) 65 rightLayout.addLayout(hLayout) 66 rightLayout.addWidget(label7) 67 rightLayout.addWidget(descTextEdit) 68 69 OKPushButton = QPushButton(self.tr("确定")) 70 cancelPushButton = QPushButton(self.tr("取消")) 71 bottomLayout = QHBoxLayout() 72 bottomLayout.addStretch() #按钮之前插入一个占位符,使两个按钮能靠右对齐。并且在整个对话框的大小发生改变时, 73 # 保证按钮的大小不发生变化。合理使用 addStretch()能让界面的布局效果增色不少 74 bottomLayout.addWidget(OKPushButton) 75 bottomLayout.addWidget(cancelPushButton) 76 #主布局, 用一个 QGridLayout 实现,并在定义主布局时指定父窗口 self, 也可调用self.setLayout(mainLayout)实现 77 mainLayout = QGridLayout(self) 78 mainLayout.setMargin(15) 79 mainLayout.setSpacing(10) 80 mainLayout.addLayout(leftLayout, 0, 0) 81 mainLayout.addLayout(rightLayout, 0, 1) 82 mainLayout.addLayout(bottomLayout, 1, 0, 1, 2) 83 mainLayout.setSizeConstraint(QLayout.SetFixedSize) 84 #设定对话框的控件总是最优化显示,并且用户无法改变对话框的大小,所谓最优化显示,即控件都按其 sizeHint()的大小显示 85 app = QApplication(sys.argv) 86 dialog = LayoutDialog() 87 dialog.show() 88 app.exec_()

  • 相关阅读:
    jmeter取样器
    【递归】普通递归关系
    7月,开始奋斗吧!
    BZOJ 1503 郁闷的出纳员
    bzoj 3262: 陌上花开
    BZOJ 2286 消耗战
    莫队
    bzoj1483: [HNOI2009]梦幻布丁
    字符串算法模板
    高级数据结构模板
  • 原文地址:https://www.cnblogs.com/nju2014/p/4530335.html
Copyright © 2020-2023  润新知