(本系列中所有代码在windows7 64位[]/Python 3.4.3 32bit/PyQt GPL v5.5 for Python v3.4 (x32)/eric6-6.0.8下测试通过.)
原本地址:http://zetcode.com/gui/pyqt5/
================================================================================
在此我们会继续介绍PyQt5的部件.我们会介绍QPixmap、QLineEdit、QSplitter和QComboBox.
QPixmap
一个QPixmap是一个用于处理图像的部件.它更适合在屏幕上显示图片.在下面的例子中,我们会用QPixmap来在窗口上显示一个图片.
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we dispay an image on the window. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QLabel, QApplication) from PyQt5.QtGui import QPixmap class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): hbox = QHBoxLayout(self) pixmap = QPixmap("redrock.png") lbl = QLabel(self) lbl.setPixmap(pixmap) hbox.addWidget(lbl) self.setLayout(hbox) self.move(300, 200) self.setWindowTitle('Red Rock') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
在上面的例子中,我们在窗口上显示了一张图片.
pixmap = QPixmap("redrock.png")我们创建一个QPixmap对象,他用文件名作为参数.
lbl = QLabel(self) lbl.setPixmap(pixmap)我们把Pixmap放到QLabel部件里.
QLineEdit
一个QLineEdit是一个允许输入和编辑一行纯文件的部件.部件支持撤销和重做、剪切和剪贴、拖拽功能.
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example shows text which is entered in a QLineEdit in a QLabel widget. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QApplication) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.lbl = QLabel(self) qle = 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('QLineEdit') self.show() def onChanged(self, text): self.lbl.setText(text) self.lbl.adjustSize() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
这个例子我们显示了一个行编辑器和一个标签.我们在行编辑器里输入的文本会立刻在标签中显示.
qle = QLineEdit(self)创建一个QLineEdit部件.
qle.textChanged[str].connect(self.onChanged)如果在行编辑器里的文本有变动,我们就调用onChanged()方法.
def onChanged(self, text): self.lbl.setText(text) self.lbl.adjustSize()
在onChanged()方法里,我们设置被输入的文本到标签部件.我们通过调用adjustSize()方法来调整标签的大小到文本的长度.
图片:行编辑器
QSplitter
一个QSplitter可以让用户通过拖拽子部件的边界来控制子部件的大小.在下面的例子中,我们显示三个QFrame部件用两个分割器调整.
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example shows how to use QSplitter widget. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame, QSplitter, QStyleFactory, QApplication) from PyQt5.QtCore import Qt class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): hbox = QHBoxLayout(self) topleft = QFrame(self) topleft.setFrameShape(QFrame.StyledPanel) topright = QFrame(self) topright.setFrameShape(QFrame.StyledPanel) bottom = QFrame(self) bottom.setFrameShape(QFrame.StyledPanel) splitter1 = QSplitter(Qt.Horizontal) splitter1.addWidget(topleft) splitter1.addWidget(topright) splitter2 = QSplitter(Qt.Vertical) splitter2.addWidget(splitter1) splitter2.addWidget(bottom) hbox.addWidget(splitter2) self.setLayout(hbox) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('QSplitter') self.show() def onChanged(self, text): self.lbl.setText(text) self.lbl.adjustSize() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
在这个例子中,有三个框架和两个分割器.请注意,在某些主题下,分割器可能并不会很友好的显示.
topleft = QFrame(self) topleft.setFrameShape(QFrame.StyledPanel)
我们用一个经典的框架以便可以看清QFrame部件的边界.
splitter1 = QSplitter(Qt.Horizontal) splitter1.addWidget(topleft) splitter1.addWidget(topright)我们创建一个QSplitter部件,把两个框架加到它里面.
splitter2 = QSplitter(Qt.Vertical) splitter2.addWidget(splitter1)我们还可以把一个分割器加到另一个分割器部件里.
图片:分割器
QComboBox
QComboBox是一个允许用户从一个选项列表里选择的部件.
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example shows how to use a QComboBox widget. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import (QWidget, QLabel, QComboBox, QApplication) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.lbl = QLabel("Ubuntu", self) combo = QComboBox(self) combo.addItem("Ubuntu") combo.addItem("Mandriva") combo.addItem("Fedora") combo.addItem("Arch") 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('QComboBox') self.show() def onActivated(self, text): self.lbl.setText(text) self.lbl.adjustSize() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
例子显示了一个QComboBox和一个标签.下拉列表框是一个有着5个选项的列表.它们是Linux发行版的名字.标签部件显示了从下拉列表框选择的选项.
combo = QComboBox(self) combo.addItem("Ubuntu") combo.addItem("Mandriva") combo.addItem("Fedora") combo.addItem("Arch") combo.addItem("Gentoo")我们创建一个有着5个选项的QComboBox部件.
combo.activated[str].connect(self.onActivated)基于选择,我们调用onActivated()方法.
def onActivated(self, text): self.lbl.setText(text) self.lbl.adjustSize()在方法里,我们设置被选择的文本到标签部件.我们会调整标签的大小.
图片:下拉列表框