Pyqt5 realizes the method of drawing ui, list window and scrolling window to display pictures

Time:2019-9-10

1: ListWidget displays all files under the file in a scroll window:

self.listWidget = QtWidgets.QListWidget(self.gridLayoutWidget)
    self.listWidget.setMaximumSize(QtCore.QSize(16777215, 400))
    self.listWidget.setObjectName("listWidget")
 The above code is automatically generated by drawing it with qtDesigner 
    Self. listWidget. setVertical ScrollBarPolicy (QtCore. Qt. ScrollBarAlways On  is displayed here in a scroll window)
    Self. pushButton_10. clicked. connect (TabWidget. viewList)# Associated Events
    def viewList(self):
      for i in os.listdir("./photo"):
        self.listWidget.addItem("./photo"+"/"+i)

2: Click on the item in the list to return the item name and display the picture

Self. listWidget. current ItemChanged. connect (TabWidget. image)  This is the name of the item that will be returned by clicking on item: PS I use the TabWidget drawn by qtDesigner.

def image():
  print(self.listWidget.currentItem().text())
  imagefile=self.listWidget.currentItem().text()
  png = QtGui.QPixmap(imagefile).scaled(self.label.width(), self.label.height())
  self.label.setPixmap(png)
  # Display pictures on lable

3: Display all images in the file in the scroll window

This section first draws a scroll area with qtDesigner. The advantage of using scroll Area is that scroll bars are automatically generated.
def suoluetu(self):
    names=[]    
    positions = [(i, j) for i in range(4) for j in range(30)]
    for i in os.listdir(r"./photo"):
      names.append("./photo"+"/"+i)
    self.filewidget = QWidget()
    self.filewidget.setMinimumSize(350, 1800)
    for position, names in zip(positions, names):
      lab = QLabel(self.filewidget)
      lab.setFixedSize(100,150)
      pix=QtGui.QPixmap(names)
      lab.setPixmap(pix)
      Lab. move (100 * position [0] + 50, 150 * position [1] + 70) This part was written by myself.
    self.scrollArea.setWidget(self.filewidget)
    The original gridLayout image is also good, but you can't scroll the picture.
    Later, this method was adopted.
    Unfortunately, the picture in the window can not be clicked, so the corresponding picture can not be analyzed.
    So I think we should still use listWidget to display pictures.

The above pyqt5 implementation of drawing ui, list window, scrolling window display picture method is the editor to share all the content, I hope to give you a reference, but also hope that you support developpaer.

Recommended Today

Server Core Knowledge

Catalog 1. Computer: a tool to assist human brain 2. Composition of computer hardware 3. power supply 4.CPU 5. memory 6. Classification of computer use 7. Introduction to common Internet servers 8. Server classification 1. Computer: a tool to assist human brain Nowadays people touch computers almost all the time! Whether it’s a desktop computer, […]