PyQT:选择文件并对其使用函数

2024-09-30 20:17:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

# -*- coding: utf-8-*-
import sys, convBB
from PyQt4 import QtGui, QtCore


class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QVBoxLayout(self)
        self.resize(300,300)
        self.setWindowTitle('Convert_BB_test')
        self.setWindowIcon(QtGui.QIcon('icon.png'))

        self.buttonSelect = QtGui.QPushButton('Select Files', self)
        layout.addWidget(self.buttonSelect)
        self.buttonSelect.clicked.connect(self.handleButton)

        self.buttonConvert = QtGui.QPushButton('Convert', self)
        layout.addWidget(self.buttonConvert)
        self.buttonConvert.clicked.connect(self.convBB.convert(self.handleButton()))

    def handleButton(self):
        title = self.buttonSelect.text()
        file_list = QtGui.QFileDialog.getOpenFileNames(self, title)
        for path in file_list:
            print (path)
        #convBB.convert(list)
        return file_list

#if __name__ == '__main__':


app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

我从handleButton函数接收文件列表,并希望在此列表中使用函数convBB.convert(list)(来自我的convBB.py):

^{pr2}$

并拥有:

^{3}$

如何使用“选择”按钮选择文件,然后使用“转换”按钮转换它们?


Tags: importselfconvertdefsyswindowlistfile
1条回答
网友
1楼 · 发布于 2024-09-30 20:17:11

既然要用buttonCovert处理list,那么

  1. self.fileList = []行添加到__init__Window
  2. 在返回之前,在handleButton方法中添加self.fileList = file_list
  3. 使用self.buttonConvert.clicked.connect(lambda: convBB.convert(self.fileList))代替self.buttonConvert.clicked.connect(self.convBB.convert(self.handleButton()))

相关问题 更多 >