Example of pyqt5 loading picture and text files

Time:2020-2-9

First let’s look at a set of effects

After selecting picture text settings

During selection

Core code interpretation

#This function is used to open the computer's Explorer and select photos
  def loadFile(self):
    print("load--file")
    #QFileDialog is the type of system dialog. The first parameter is context, the second parameter is the name of the pop-up box, the third parameter is the path to start opening, and the fourth parameter is the required format
    Fname, = qfiledialog.getopenfilename (self, 'select picture', 'C: \','image files (*. JPG *. GIF *. PNG)))
    self.label.setPixmap(QPixmap(fname))


#This function is to open the text selector and load the text
   def load_text(self):
    print("load--text")
    #Initialize this instance and set some basic properties
    dlg = QFileDialog()
    dlg.setFileMode(QFileDialog.AnyFile)
    dlg.setFilter(QDir.Files)
    #When the selector is off
    if dlg.exec_():
      #Get the selected text
      filenames = dlg.selectedFiles()
      #Read the text content and set it to TextEdit
      f = open(filenames[0], 'r')
      with f:
        data = f.read()
        self.content.setText(data)

Complete code

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

class filedialogdemo(QWidget):

  def __init__(self, parent=None):
    super(filedialogdemo, self).__init__(parent)
    layout = QVBoxLayout()

    self.btn = QPushButton()
    self.btn.clicked.connect(self.loadFile)
    Self.btn.settext ("get photos from file")
    layout.addWidget(self.btn)

    self.label = QLabel()
    layout.addWidget(self.label)

    self.btn_2 = QPushButton()
    self.btn_2.clicked.connect(self.load_text)
    Self.btn? 2.settext ("load computer text file")
    layout.addWidget(self.btn_2)

    self.content = QTextEdit()
    layout.addWidget(self.content)
    Self.setwindowtitle ("test")

    self.setLayout(layout)

  def loadFile(self):
    print("load--file")
    Fname, = qfiledialog.getopenfilename (self, 'select picture', 'C: \','image files (*. JPG *. GIF *. PNG)))
    self.label.setPixmap(QPixmap(fname))

  def load_text(self):
    print("load--text")
    dlg = QFileDialog()
    dlg.setFileMode(QFileDialog.AnyFile)
    dlg.setFilter(QDir.Files)
    if dlg.exec_():
      filenames = dlg.selectedFiles()
      f = open(filenames[0], 'r')
      with f:
        data = f.read()
        self.content.setText(data)

if __name__ == '__main__':
  app = QApplication(sys.argv)
  fileload = filedialogdemo()
  fileload.show()
  sys.exit(app.exec_())

The above example of pyqt5 loading image and text file is all the content shared by Xiaobian. I hope to give you a reference, and I hope you can support developepaer more.

Recommended Today

The future of CSS has come

Author: Chen Da fish head github: KRISACHAN Preface Recently, I heard that typescript 3.7 added support for optional chaining, and then I wanted to upgrade TS version of Ying template, a fish head scaffold. Then I found this message on the command line: ‘postcss cssnext’ has been replaced by ‘postcss preset env’. For details, please […]