PyQt5的学习之路(八)

时间:2022-04-22 08:35:14

像素图

#! /usr/bin/python
# -*- coding: utf-8 -*-

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) # 将(600, 100)设为新图的原点
# 后四位参素是从图片的(80, 100)位置截取横300,纵100像素的图片,并根据前四位参数对像素图的大小位置设置
painter.rotate(150) # 顺时针旋转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_())

单行文本框

#! /usr/bin/python
# -*- coding: utf-8 -*-

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_())

剪切器

#! /usr/bin/python
# -*- coding: utf-8 -*-

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_())

下拉选单

#! /usr/bin/python
# -*- coding: utf-8 -*-

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_())