PyQt4
菜单栏和工具栏
主窗体
QtGui.QMainWindow
类提供了一个主应用窗体。它提供了一个通用色应用框架,可以放置状态栏、工具栏和菜单栏。
状态栏
、、、
def initUI(self):
self.statusBar().showMessage('Ready')
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Statusbar')
self.show()
、、、
statusBar()
第一次调用时,创建一个状态栏。再次调用时,返回状态栏对象。showMessage()
方法是在状态栏上显示一个文本信息。
菜单栏
、、、
def initUI(self):
exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Menubar')
self.show()
、、、
exitAction.triggered.connect(QtGui.qApp.quit)
当我们选择了这个特定的行为时,就触发了这个行为-关闭整个应用。
工具栏
、、、
def initUI(self):
exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(QtGui.qApp.quit)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAction)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Toolbar')
self.show()
、、、
两者一起使用
、、、
def initUI(self):
textEdit = QtGui.QTextEdit()
self.setCentralWidget(textEdit)
exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.close)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
toolbar = self.addToolBar('Exit')
toolbar.addAction(exitAction)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('Main window')
self.show()
、、、
布局管理
绝对位置
几个限制:
1. 当我们画出窗口时,窗口的大小和位置不会改变???
2. 在不同的平台下,外观可能不同
3. 改变字体大小,可能破坏布局
4. 如果我们要改变我们的布局,我们必须重画布局,这是麻烦和花时间的
、、、
def initUI(self):
lbl1 = QtGui.QLabel('ZetCode', self)
lbl1.move(15, 10)
lbl2 = QtGui.QLabel('tutorials', self)
lbl2.move(35, 40)
lbl3 = QtGui.QLabel('for programmers', self)
lbl3.move(55, 70)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Absolute')
self.show()
、、、
使用move()
方法来放置控件,也就是这个例子中的文本控件。
盒子布局
QtGui.QHBoxLayout
和QtGui.QVBoxLayout
是基本的布局类。能够垂直和水平放置控件。
假如我们放置两个按钮在后下角。这样的布局,我们需呀使用一个水平盒子和一个垂直盒子。为了创造必要的空间,我们需要增加一个弹性空间。
、、、
def initUI(self):
okButton = QtGui.QPushButton("OK")
cancelButton = QtGui.QPushButton("Cancel")
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Buttons')
self.show()
、、、
应用中我们使用了QtGui.HBoxLayout
和QtGui.QVBoxLayout
,并且创建了两个按钮。我们创建了一个水平的盒子布局并增加了一个弹性空间(addStretch(1)
)和两个按钮。弹性空间将两个按钮推到了屏幕的右侧。同理创建了一个垂直盒子布局并增加了弹性空间。最后我们得到了窗体。
网格布局
最常见的布局就是网格布局。QtGui.QGridLayout
、、、
def initUI(self):
grid = QtGui.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 = QtGui.QPushButton(name)
grid.addWidget(button, *position)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
、、、
在这个例子中。我们在网格布局中创建了很多按钮。
另一个评论的例子
、、、
def initUI(self):
title = QtGui.QLabel('Title')
author = QtGui.QLabel('Author')
review = QtGui.QLabel('Review')
titleEdit = QtGui.QLineEdit()
authorEdit = QtGui.QLineEdit()
reviewEdit = QtGui.QTextEdit()
grid = QtGui.QGridLayout()
grid.setSpacing(10)
grid.addWidget(title, 1, 0)
grid.addWidget(titleEdit, 1, 1)
grid.addWidget(author, 2, 0)
grid.addWidget(authorEdit, 2, 1)
grid.addWidget(review, 3, 0)
grid.addWidget(reviewEdit, 3, 1, 5, 1)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Review')
self.show()
、、、
grid.addWidget(reviewEdit, 3, 1, 5, 1)
,如果使用网格系统,我们可以设置跨行数和跨列数。在本例中。我们使reviewEdit
跨5行。
转载请注明作者“MieAn100”及其出处
CSDN博客(http://blog.csdn.net/MieAn100)