这里我们将继续介绍PyQt4 widget。我们将介绍QtGui.QPixmap、QtGui.QLineEdit、QtGui.QSplitter和QtGui.QComboBox。
QtGui.QPixmap
QPixmap是用于处理图像的小部件之一。它被优化为在屏幕上显示图像。在我们的代码示例中,我们将使用QtGui.QPixmap在窗口上显示图像。
#!/usr/bin/python
"""
ZetCode PyQt4 tutorial
In this example, we dispay an image
on the window.
author: Jan Bodnar
website: zetcode.com
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
pixmap = QtGui.QPixmap("redrock.png")
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
hbox.addWidget(lbl)
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('Red Rock')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在我们的示例中,我们在窗口上显示一个图像。
pixmap = QtGui.QPixmap("redrock.png")
我们创建一个QtGui.QPixmap对象。它将文件名作为参数。
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
我们将pixmap放到QtGui.QLabel小部件中。
QtGui.QLineEdit
QLineEdit是一个允许输入和编辑一行纯文本的小部件。有撤消和重做,剪切和粘贴,以及拖放功能可用于小部件。
#!/usr/bin/python
"""
ZetCode PyQt4 tutorial
This example shows text which
is entered in a QtGui.QLineEdit
in a QtGui.QLabel widget.
author: Jan Bodnar
website: zetcode.com
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lbl = QtGui.QLabel(self)
qle = QtGui.QLineEdit(self)
qle.move(60, 100)
self.lbl.move(60, 40)
qle.textChanged[str].connect(self.onChanged)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QtGui.QLineEdit')
self.show()
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
此示例显示了行编辑小部件和标签。我们在行编辑中键入的文本立即显示在标签小部件中。
qle = QtGui.QLineEdit(self)
将创建QtGui.QLineEdit小部件。
qle.textChanged[str].connect(self.onChanged)
如果行编辑小部件中的文本发生了变化,我们调用onChanged方法。
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
在onChanged方法中,我们将键入的文本设置为label小部件。我们调用adjustSize方法将标签的大小调整为文本的长度。
QtGui.QSplitter
QSplitter允许用户通过拖动子部件之间的边界来控制子部件的大小。在我们的示例中,我们展示了三个QtGui.QFrame小部件,它们是用两个拆分器组织的。
#!/usr/bin/python
"""
ZetCode PyQt4 tutorial
This example shows
how to use QtGui.QSplitter widget.
author: Jan Bodnar
website: zetcode.com
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
topleft = QtGui.QFrame(self)
topleft.setFrameShape(QtGui.QFrame.StyledPanel)
topright = QtGui.QFrame(self)
topright.setFrameShape(QtGui.QFrame.StyledPanel)
bottom = QtGui.QFrame(self)
bottom.setFrameShape(QtGui.QFrame.StyledPanel)
splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
hbox.addWidget(splitter2)
self.setLayout(hbox)
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QSplitter')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在我们的示例中,我们有三个框架小部件和两个拆分器。
topleft = QtGui.QFrame(self)
topleft.setFrameShape(QtGui.QFrame.StyledPanel)
我们使用一个样式化的框架来查看QtGui.QFrame小部件之间的边界。
splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
我们创建了一个QtGui.QSplitter小部件,并在其中添加了两个帧。
splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter1)
我们还可以将拆分器添加到另一个拆分器小部件中。
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
我们使用Cleanlooks风格。在某些样式中,框架不可见。
QtGui.QComboBox
QComboBox是一个小部件,允许用户从选项列表中进行选择。
#!/usr/bin/python
"""
ZetCode PyQt4 tutorial
This example shows
how to use QtGui.QComboBox widget.
author: Jan Bodnar
website: zetcode.com
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lbl = QtGui.QLabel("Ubuntu", self)
combo = QtGui.QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Red Hat")
combo.addItem("Gentoo")
combo.move(50, 50)
self.lbl.move(50, 150)
combo.activated[str].connect(self.onActivated)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QComboBox')
self.show()
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
该示例显示了QtGui.QComboBox和QtGui.QLabel。组合框包含五个选项的列表。这些是Linux发行版的名称。标签小部件显示组合框中的选定选项。
combo = QtGui.QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Red Hat")
combo.addItem("Gentoo")
我们创建了一个QtGui.QComboBox小部件,包含五个选项。
combo.activated[str].connect(self.onActivated)
在选择一个项时,我们调用onActivated方法。
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
在该方法中,我们将所选项的文本设置为label小部件。我们调整标签的大小。
在PyQt4教程的这一部分中,我们介绍了其他四个PyQt4小部件。