How to create a button for each element in a list and put it in a scroll-area?

点点圈 提交于 2019-12-02 09:26:08

The Problem is not that you add the same button, but that you sort of replace the Widget in the scrollArea.

A better way would be to create a QHBoxLayout and than add the buttons to the layout.

filenames = []
lay = QtGui.QHboxLayout()
window.scrollArea.setLayout(lay)
def addfiles():
    fileName= QtGui.QFileDialog.getOpenFileName()
    fileDirectory = unicode(fileName)  
    global filenames
    filenames.append(fileDirectory)
    button = QtGui.QPushButton(os.path.basename(fileDirectory))
    lay.addWidget(button)

In a sort of that way it should work. Here is a small working example:

from PyQt4 import QtGui
import sys

filenames = []

class TestGui(QtGui.QWidget):
    """ A Fast test gui show how to create buttons in a ScrollArea"""
    def __init__(self):
        super(TestGui, self).__init__()
        self.lay = QtGui.QHBoxLayout()
        self.sA = QtGui.QScrollArea()
        self.sA_lay = QtGui.QVBoxLayout()
        self.sA.setLayout(self.sA_lay)
        self.closeGui = QtGui.QPushButton("Close")
        self.add_file_button = QtGui.QPushButton("Add File")
        self.lay.addWidget(self.closeGui)
        self.lay.addWidget(self.add_file_button)
        self.lay.addWidget(self.sA)
        self.setLayout(self.lay)
        self.connect_()
        self.show()

    def connect_(self):
        self.add_file_button.clicked.connect(self.__add_file_to_list)
        self.closeGui.clicked.connect(self.close)
        return

    def __add_file_to_list(self):
        fname = QtGui.QFileDialog.getOpenFileName()
        global filenames
        filenames.append(fname)
        button = QtGui.QPushButton(fname)
        self.sA_lay.addWidget(button)
        return


if __name__ == '__main__':
     app = QtGui.QApplication(sys.argv)
     tg = TestGui()
     sys.exit(app.exec_())

The problem is that you're not adding a layout to the scrollLayout, you're setting the scrollArea's widget:

#!/usr/bin/env python
import os, sys
from PyQt4 import QtCore, QtGui


filenames = []


class Window(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.centralwidget = QtGui.QWidget(self)
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.scrollArea = QtGui.QScrollArea(self.centralwidget)
        self.scrollArea.setWidgetResizable(True)

        self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.verticalLayout.addWidget(self.scrollArea)
        self.setCentralWidget(self.centralwidget)

        # create a layout for your scrollarea
        self.formLayout = QtGui.QFormLayout(self.scrollAreaWidgetContents)
        self.addFiles()

    def addFiles(self):
        global filenames
        filenames.append("~/files/newFile.txt")
        button = QtGui.QPushButton(os.path.basename(filenames[-1]))
        self.formLayout.addWidget(button)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!