Home > Mobile >  How to Show a Picture in Qt python
How to Show a Picture in Qt python

Time:05-15

How Can I Show A Picture in PyQT6? My Code:

from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 314)
        self.descriptionbox_2 = QtWidgets.QLabel(Dialog)
        self.descriptionbox_2.setGeometry(QtCore.QRect(200, 70, 141, 61))
        self.descriptionbox_2.setTextFormat(QtCore.Qt.TextFormat.RichText)
        self.descriptionbox_2.setWordWrap(True)
        self.descriptionbox_2.setOpenExternalLinks(True)
        self.descriptionbox_2.setObjectName("descriptionbox_2")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.descriptionbox_2.setText(_translate("Dialog", "<html><head/><body><p><br/></p></body></html>"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec())

I Want To Show A Picture From the web, I tested the img tag but not worked! please help!

CodePudding user response:

This is the code :

import sys
import requests
from PyQt6.QtGui     import QPixmap, QScreen
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QDialog

URL = 'https://avatars.githubusercontent.com/u/76901932?v=4'


class Ui_Dialog(object):
    def setupUi(self,Dialog):
        Dialog.setObjectName("Dialog")
        self.label = QLabel(Dialog)
        self.pixmap = QPixmap()
        self.getAndSetImageFromURL(URL)


    def getAndSetImageFromURL(self,imageURL):
        request = requests.get(imageURL)
        self.pixmap.loadFromData(request.content)
        self.label.setPixmap(self.pixmap)
        Dialog.resize(self.pixmap.width(),self.pixmap.height())



if __name__ == '__main__':
    app =QApplication(sys.argv)
    Dialog =QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec())

and Results:

enter image description here

  • Related