PyQt5可重用文件选择对话框

时间:2017-02-22 03:39:49

标签: python pyqt5

我想使用GUI加载两个文件,然后对这两个文件执行python命令。我想使用可重用的文件选择方法来设置调用它的元素。我看到的每个教程都设置了一个特定的命名元素而不是一个元素。我该如何修复此代码?

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDesktopWidget, QLabel, QPushButton, QInputDialog, QLineEdit, QFileDialog, QAction
from PyQt5.QtGui import QIcon


class dswfGui(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # Build the window widget
        self.setGeometry(300, 300, 550, 550)  # x, y, w, h
        self.setWindowTitle("workflow")
        self.center()

        # Add a label
        label = QLabel("workflow", self)
        label.resize(label.sizeHint())
        label.move(120, 20)

        # Select input file1
        file1_label = QLabel("file 1", self)
        file1_label.resize(file1_label.sizeHint())
        file1_label.move(20,50)
        file1_label.setToolTip('end file 1')
        self.file1 = QLineEdit("Select file", self)
        self.file1.setObjectName("file1")
        self.file1.resize(self.file1.sizeHint())
        self.file1.move(120, 50)
        self.file1.setToolTip('Select file 1')
        f1btn = QPushButton('Select file 1', self)
        f1btn.resize(f1btn.sizeHint())
        f1btn.element = self.file1
        f1btn.move(250, 46)
        f1btn.clicked.connect(self.selectFile)

        # Select input file2
        file2_label = QLabel("file 2", self)
        file2_label.resize(file2_label.sizeHint())
        file2_label.move(20,90)
        file2_label.setToolTip('end file 2')
        self.file2 = QLineEdit("Select file2", self)
        self.file2.setObjectName("file2")
        self.file2.resize(self.file2.sizeHint())
        self.file2.move(120, 90)
        self.file2.setToolTip('Select file 2')
        f2btn = QPushButton('Select file 2', self)
        f2btn.resize(f2btn.sizeHint())
        f2btn.element = self.file1
        f2btn.move(250, 86)
        # this doesn't work because self.selectFile doesn't accept an element or read an element
        f2btn.clicked.connect(self.selectFile)

        self.statusBar()

        # Show window and run
        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def selectFile(self):
        # I want this to be reusable, but it doesn't seem
        # to want to let me pass element in or call self.element
        self.element.setText(QFileDialog.getOpenFileName()[0])
        #this works but is not DRY
        #self.file1.setText(QFileDialog.getOpenFileName()[0])

if __name__ == "__main__":
    app = QApplication(sys.argv)
    gui = dswfGui()
    sys.exit(app.exec_())

我不确定如何让我想要的更清楚。我想在UI中选择两个文件。我想使用一个方法(selectFile)打开QFileDialog并将适当的QLineEdit设置为所选值。我不想为每个QLineEdit都有一个QFileDialog,因为那不是DRY。

1 个答案:

答案 0 :(得分:1)

问题不是很清楚,无论如何,如果我理解正确,你有以下选择:

使用lambda(s):

如果您需要断开单个功能,这可能会变得很糟糕

var blockingCollection = new BlockingCollection<MyType>();

定义两个回调函数

这与您试图避免的内容几乎相同

f1btn.clicked.connect(lambda: self.selectFile(self.file1))
f2btn.clicked.connect(lambda: self.selectFile(self.file2))