随笔 - 41, 文章 - 8, 评论 - 8, 引用 - 0
数据加载中……

[Python][PyQt4]PyQt4 Widget II

PyQt4 Widget II

这里我们将继续介绍 PyQt4 组件。我们将涉及 QtGui.QPixmapQtGui.QLineEditQtGui.QSplitterQtGui.QComboBox

QtGui.QPixmap

QtGui.QPixmap 是可以处理图片的组件之一。它对显示图片进行了优化。在我们的例子中,我们会用 QtGui.QPixmap 来显示图片。

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we dispay an image
on the window.
author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""
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

QtGui.QLineEdit 是一个允许输入和编辑一行纯文本。这个组件中可以撤销/恢复,剪切/粘贴以及拖拉。

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
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
last edited: August 2011
"""
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() 方法中,我们把输入的文本放到了标签中。通过调用 adjustSize() 方法,我们把标签设置到文本的长度。

QtGui.QSplitter

QtGui.QSplitter 可以让用户控制子组件的大小,通过拖动子组件的大小。在我们的例子中,我们的三个 QtGui.QFrame 将由两个 splitter 分割。

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
This example shows
how to use QtGui.QSplitter widget.
author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""
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)

我们使用了有样式的框架,主要用于看到边框。

splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)

我们创建了一个 QtGui.QSplitter 组件,并添加到两个框架。

splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter1)

我们也可以把一个 splitter 添加到 splitter 中。

QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))

我们使用一个简洁的样式。在有些样式中,框架是不可见的。

QtGui.QComboBox

QtGui.QComboBox 允许用户从一组选项中选取一个。

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
This example shows
how to use QtGui.QComboBox widget.
author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""
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.QComboBoxQtGui.QLable 。这里有五个选项。它们是 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()

在这个方法中,我们把选中的选项的文本放到标签中。我们还调整标签的大小。


在本部分,我们涉及了另外四个 PyQt4 组件。

posted on 2012-02-12 10:13 mirguest 阅读(1822) 评论(0)  编辑 收藏 引用 所属分类: Python


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理