像素图
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel
from PyQt5.QtGui import QPixmap, QPainter
from PyQt5.QtCore import Qt
class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
self.pixmap = QPixmap('1.jpg')
lbl = QLabel(self)
lbl.setPixmap(self.pixmap)
hbox.addWidget(lbl)
self.setLayout(hbox)
self.move(300, 300)
self.resize(1000, 1000)
self.setWindowTitle('pixmap')
self.show()
def paintEvent(self, QPaintEvent):
painter = QPainter(self)
painter.translate(600, 100)
painter.rotate(150)
painter.scale(2, 2)
painter.drawPixmap(0, 0, 300, 200, self.pixmap, 80, 100, 300, 100)
painter.translate(100, 100)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())
单行文本框
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QLabel
class Myform(QWidget):
def __init__(self):
super(Myform, self).__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, 300, 200)
self.setWindowTitle('lineedit')
self.show()
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())
剪切器
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QSplitter, QFrame, QHBoxLayout, QStyleFactory
from PyQt5.QtCore import Qt
class Myform(QWidget):
def __init__(self):
super(Myform, self).__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.Vertical)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
splitter2 = QSplitter(Qt.Horizontal)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
hbox.addWidget(splitter2)
self.setLayout(hbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('splitter')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())
下拉选单
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox
class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()
self.initUI()
def initUI(self):
self.lbl = QLabel("Ubuntu", self)
combo = QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandrive")
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('combobox')
self.show()
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())