How to create a button for each element in a list

2020-05-07 07:21发布

I have a list which gets one element each time user opens a file. I need to create a button with the file's name (element from the list), each time this file is appended to a list, and put this button into a scroll-area.

The problem is that I always have only one button, that just changes its name:

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

I know that the problem is that I add the same object (button) to the scroll-area, but I don't know how to fix it.

2条回答
萌系小妹纸
2楼-- · 2020-05-07 08:00

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)
查看更多
Juvenile、少年°
3楼-- · 2020-05-07 08:11

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_())
查看更多
登录 后发表回答