언어/Python

Python qt label link picture 라벨 링크 이미지 사진

darkdevilness 2017. 10. 24. 00:06
728x90



qtLabel.py

준비물 : 사진 한장 과  아래 코드를 작성한 파일 1개 ,  qt5 version 과  PIL 이 설치되어 있어야 함

아래 동작은  qt 에서 라벨에 이미지를 추가하고  이미지를 클릭하면  해당 이미지 또는 link 가 되어 있는  특정한 곳으로 이동하는 예제 입니다. 

사진의크기는  재설정이 가능합니다

추가 : setPIXmap 은 그냥 Icon 형태로  이미지가 loading 하기 때문에  어떠한 행동도 하지 않습니다.  사진만  보고자하는 용도로 사용하면됩니다. 

여기서  Key Point : ( 여러번 테스트 하지면 충분히 이해 할 것입니다.  모르면 연락주세요 )

setPIXmap  사용 방법,    

setText()에서  text를 사용해 link 사용하는 방법 ,  이미지를 사용해 Link하는 방법 

setOpenExternalLinks() 를 사용하거나, 내부 함수를 사용하는 방법

출처 : 이곳 저곳에서 조합함.

'''

Created on 2017. 10. 23.


@author: HOME

'''


import sys

from PyQt5 import QtCore, QtWidgets

from PyQt5.QtGui import QIcon,QPixmap

from PyQt5.QtWidgets import *

from PIL import Image


class MyWindow(QMainWindow):

    

    def __init__(self):

        super().__init__()

        self.setupUI()

    BUTTON_IMAGE = 'K-006.jpg'

    def setupUI(self):

        self.setGeometry(800, 400, 400, 300)


        textLabel = QLabel("Message: ", self)

        textLabel.move(20, 20)


        self.label = QLabel("", self)

        self.label.move(80, 20)

        self.label.resize(150, 30)


        btn1 = QPushButton("Click", self)

        btn1.move(20, 60)

        btn1.clicked.connect(self.btn1_clicked)


        btn2 = QPushButton("Clear", self)

        btn2.move(140, 60)

        btn2.clicked.connect(self.btn2_clicked)

    

        

        self.ImageButton = QLabel(self)

        self.ImageButton.move(0,100)

        self.ImageButton.resize(300, 150)

        self.ImageButton.setPixmap(QPixmap(self.BUTTON_IMAGE).scaled(300,150))

        self.ImageButton.linkActivated.connect(self.image_clicked)

        self.ImageButton.linkHovered.connect(self.image_hovered)

        self.ImageButton.setText("<a href=\"K-006.jpg\"><img src=\"K-006.jpg\" width=\"300\" height=\"150\" /></a>")

        #self.ImageButton.setText("<a href=\"http://www.google.com\">'Click this link to go to Google'</a>")

        #self.ImageButton.setOpenExternalLinks(True)

        

    def btn1_clicked(self):

        self.label.setText("버튼이 클릭되었습니다.")


    def btn2_clicked(self):

        self.label.clear()

    def image_hovered(self):

        print('Hoverd_Image')

    def image_clicked(self):

        print('Image_clicked')

        im=Image.open(self.BUTTON_IMAGE)

        im.show()

        

if __name__ == "__main__":

    app = QApplication(sys.argv)

    mywindow = MyWindow()

    mywindow.show()

    app.exec_()

    

728x90