仅作为操作记录,大佬请跳过。
思路
在python的gui用label
控件
self.label.setPixmap(jpg)
直接上代码
1、界面设计,(详细步骤参考博主文章传送门)
只放入一个label控件
保存——关闭——自动生成对应py文件(无需设置事件函数,)
2、新建main.py,放入代码
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from showpicdirect import Ui_Form
class myputpic(QtWidgets.QWidget,Ui_Form):
def __init__(self):
super(myputpic,self).__init__()
self.setupUi(self)
# imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
# jpg = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())
jpg = QtGui.QPixmap(r'E:\picdirect\青岛.jpg').scaled(self.label.width(), self.label.height())
self.label.setPixmap(jpg)
if __name__=='__main__':
app=QtWidgets.QApplication(sys.argv)
a=myputpic()
a.show()
sys.exit(app.exec_())
展示
直接运行即可
——————————————————————————————————
参考
感谢大佬博主文章 传送门
大佬博主的代码,复制放在一个py文件里,就可以直接运行——进入文件夹选图
大佬博主代码
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_())