如何为列表中的每个元素创建一个按钮并将其放在滚动区域中? [英] How to create a button for each element in a list and put it in a scroll-area?

查看:165
本文介绍了如何为列表中的每个元素创建一个按钮并将其放在滚动区域中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,该列表每次用户打开文件时都会得到一个元素.每次将此文件追加到列表中时,我都需要使用文件名(列表中的元素)创建一个按钮,并将此按钮放入滚动区域.

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.

推荐答案

问题不在于您添加了相同的按钮,而是您替换了scrollArea中的Widget.

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

比将按钮添加到布局中,更好的方法是创建QHBoxLayout并将其添加到布局中.

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_())

这篇关于如何为列表中的每个元素创建一个按钮并将其放在滚动区域中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆