pyqt5 从本地选择图片 并显示在label上的实例
1.主要用到 QFileDialog 方法打开本地文件
2.界面
打开前:
打开后:
3. 代码
import sys from PyQt5 import QtWidgets, QtCore, QtGui from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * class picture(QWidget): def __init__(self): super(picture, self).__init__() self.resize(600, 400) self.setWindowTitle("label显示图片") self.label = QLabel(self) self.label.setText(" 显示图片") self.label.setFixedSize(300, 200) self.label.move(160, 160) self.label.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋体;}" ) btn = QPushButton(self) btn.setText("打开图片") btn.move(10, 30) btn.clicked.connect(self.openimage) def openimage(self): imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)") jpg = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height()) self.label.setPixmap(jpg) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) my = picture() my.show() sys.exit(app.exec_())
相关推荐
菜鸟 2020-09-14
xingyuzhe 2020-06-09
wishli 2020-06-08
yevvzi 2020-06-08
MaureenChen 2020-05-25
HMHYY 2020-05-19
阿斌Elements 2020-05-15
85443563 2020-04-30
PioneerFan 2020-05-11
cyydjt 2020-05-10
ELEMENTS爱乐小超 2020-04-23
行吟阁 2020-04-18
CurrentJ 2020-04-14
小信 2020-04-09
TyrionZK 2020-02-26
swiftwwj 2020-02-23
ITxiaobaibai 2020-02-19