weixin_46605602
weixin_46605602
2020-11-04 21:31
采纳率: 50%
浏览 107

pyqt5 在图片下面显示图片路径怎么做

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import *

class test(QWidget):

    def __init__(self):
        super(test, self).__init__()

        self.resize(600, 600)
        self.setWindowTitle("图片导入")

        self.label = QLabel(self)
        #self.label.setText("导入图片")
        self.label.setFixedSize(350, 350)
        self.label.move(130, 200)


        self.label.setStyleSheet("QLabel{background:white;}"                               #设置样式
                                 "QLabel{color:rgb(300,300,300,120);font-size:10px;}"
                                 )

        # 按钮
        btn = QPushButton(self)
        btn.setText("导入图片")
        btn.move(230, 50)
        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)


        #self.label.setText('文件路径:\n' + os.path.dirname((__file__)))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my = test()
    my.show()
    sys.exit(app.exec_())

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • vanlove123
    vanlove123 2020-11-06 15:08
    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)
    
        self.label.setText('文件路径:\n' + imgName)

     

    使用:imgName就行

    点赞 评论

相关推荐