# -*- coding: utf-8 -*-"""
PyQt5 tutorial
In this example, we dispay an image
on the window.
author: py40.com
last edited: 2017年3月
"""import sys
from PyQt5.QtWidgets import(QWidget, QHBoxLayout,
QLabel, QApplication)from PyQt5.QtGui import QPixmap
classExample(QWidget):def__init__(self):super().__init__()
self.initUI()definitUI(self):
hbox = QHBoxLayout(self)
pixmap = QPixmap("icon.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("icon.png")
创建一个QPixmap 对象,它将传入的文件名作为参数。
lbl = QLabel(self)
lbl.setPixmap(pixmap)
我们将这个pixmap放到QLabel控件中。
文本框 QLineEdit
QLineEdit是用于输入或编辑单行文本的控件。它还有撤销重做、剪切复制和拖拽功能。
# -*- coding: utf-8 -*-"""
PyQt5 tutorial
This example shows text which
is entered in a QLineEdit
in a QLabel widget.
author: py40.com
last edited: 2017年3月
"""import sys
from PyQt5.QtWidgets import(QWidget, QLabel,
QLineEdit, QApplication)classExample(QWidget):def__init__(self):super().__init__()
self.initUI()definitUI(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()defonChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()if __name__ =='__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())