本文代码来自zetcode.com
QPixmap
QPixmap是一类用来处理图片的控件,针对在屏幕上显示的图片进行了优化
#!/usr/bin/python3
"""
In this example, we display an image
on the window.
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout
from PyQt5.QtGui import QPixmap
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
pixmap = QPixmap("redrock.jpg") # 创建QPixmap对象
lbl = QLabel(self)
lbl.setPixmap(pixmap) # 将pixmap放入QLabel控件中
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_())
如下图:
![redrock](https://i-blog.csdnimg.cn/blog_migrate/50334661bddc326a5e681582945829cc.png)
QLineEdit
QLineEdit控件允许用户编辑文本,进行撤销、重写、复制、剪切、拖拽等操作。
#!/usr/bin/python3
"""
This example shows text which
is entered in a QLineEdit in
a QLabel widget.
"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QLineEdit, QLabel
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.lbl = QLabel(self)
qle = QLineEdit(self) # 创建QLineEdit对象
qle.move(60, 100)
self.lbl.move(60, 40)
qle.textChanged[str].connect(self.onChanged) # 当编辑框里的文本改变,我们调用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_())
在onChanged()方法中,我们将输入的文本放入标签控件中,然后调用adjustSize()方法调整标签大小以适应文本长度。
![qlineedit](https://i-blog.csdnimg.cn/blog_migrate/a7f8a7d709d4ffa4dbfbcae90b35a6d2.png)
QSplitter
QSplitter允许用户通过拖拽窗口边界控制子控件的大小
#!/usr/bin/python3
"""
This example shows
how to use QSplitter widget.
"""
import sys
from PyQt5.QtWidgets import (QWidget, QApplication, QHBoxLayout,
QFrame, QSplitter, QStyleFactory)
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()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
本例中我们有三个框控件和两个分离控件
topleft.setFrameShape(QFrame.SytlePannel)
我们使用QFrame.StylePannel以便看清QFrame控件的边界
![splitterwidget](https://i-blog.csdnimg.cn/blog_migrate/9675b7314aed939910999ecb65c1e7f2.png)
QComboBox
QComboBox控件提供用户选择选项
#!/usr/bin/python3
"""
This example shows how to use
a QComboBox widget.
"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QComboBox
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_())
经过选择后,我们使用onAcitivated()方法,将选择的条目显示在标签上
![combobox](https://i-blog.csdnimg.cn/blog_migrate/36897d7457fc8e1f22c5ea8183ede317.png)