单击按钮后Pyqt5加载İmage

时间:2020-02-01 15:24:02

标签: python pyqt5

我想在单击按钮后在表单上显示图像。但是下面的代码不起作用。我已经定义了一个函数。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QFileDialog, QPushButton
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot
import TurkceOcr as ocr

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'Resimden Texte'
        self.left = 50
        self.top = 50
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create widget

        button = QPushButton('Resim Yükle', self)
        button.setToolTip('This is load picture button')
        button.move(10, 10)
        button.clicked.connect(self.on_click)

        self.label = QLabel(self)
        self.label.move(10,50)


        #self.resize(pixmap.width(), pixmap.height())

        self.show()

    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')
        image = QFileDialog.getOpenFileName(None, 'OpenFile', '', "Image file(*.jpg)")
        imagePath = image[0]
        pixmap = QPixmap(imagePath)
        self.label.setPixmap(pixmap)
        #print(ocr.resimden_yaziya(imagePath))
        print(imagePath)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

QWidget :: adjustSize()

调整小部件的大小以适合其内容。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QFileDialog, QPushButton
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot
#import TurkceOcr as ocr

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'Resimden Texte'
        self.left = 50
        self.top = 50
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create widget

        button = QPushButton('Resim Yükle', self)
        button.setToolTip('This is load picture button')
        button.move(10, 10)
        button.clicked.connect(self.on_click)

        self.label = QLabel(self)
        self.label.move(10,50)


        #self.resize(pixmap.width(), pixmap.height())

        self.show()

    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')
        image = QFileDialog.getOpenFileName(None, 'OpenFile', '', "Image file(*.jpg)")
        imagePath = image[0]
        pixmap = QPixmap(imagePath)
        self.label.setPixmap(pixmap)
        
        self.label.adjustSize()                       # <---
        
        #print(ocr.resimden_yaziya(imagePath))
        print(imagePath)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

enter image description here