Python - PyQt5如何将文件作为输入?

时间:2018-03-14 16:47:46

标签: python-3.x pyqt5

下面附有截图:
所以基本上我想把文件作为输入(即suffix_list.txt)而不是在函数内指定文件位置,当我点击动作按钮时我需要在QtTextEdit中执行输出
我尝试了一些东西,但我无法得到它。请帮帮我。我是PyQt5的新手。谢谢你 这是我的gui代码:

    # -*- coding: utf-8 -*-    
# Created by: PyQt5 UI code generator 5.10
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(514, 381)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.text_output = QtWidgets.QTextEdit(self.centralwidget)
        self.text_output.setGeometry(QtCore.QRect(80, 10, 351, 271))
        self.text_output.setObjectName("text_output")
        self.btn_Action = QtWidgets.QPushButton(self.centralwidget)
        self.btn_Action.setGeometry(QtCore.QRect(220, 300, 75, 23))
        self.btn_Action.setObjectName("btn_Action")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 514, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpen_File = QtWidgets.QAction(MainWindow)
        self.actionOpen_File.setObjectName("actionOpen_File")
        self.menuFile.addAction(self.actionOpen_File)
        self.menubar.addAction(self.menuFile.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.btn_Action.setText(_translate("MainWindow", "Action"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.actionOpen_File.setText(_translate("MainWindow", "Open File"))


    def file_open(self):
        name, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', options=QtWidgets.QFileDialog.DontUseNativeDialog)
        file = open(name, 'r')
        with file:
            text = file.read()
            self.textEdit.setText(text)

   def suffix_remove(self): 
        suffix_list = []
        dictionary = {}
        lists = ['athletic','kitchenette','helpful','terrify']
        with open('suffix_list.txt') as f:
            for lines in f:
                lines = lines.rstrip()
                suffix_list.append(lines)
        for words in lists:
            for suffix in suffix_list:
                if words.endswith(suffix):
                    final_list = str.replace(words,suffix,'')
                    dictionary[words] = final_list
        return dictionary


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    ui.btn_Action.clicked.connect(lambda: 
    ui.text_output.append(str(ui.suffix_remove())))
    MainWindow.show()
    sys.exit(app.exec_())

enter image description here

2 个答案:

答案 0 :(得分:0)

您遇到的两个主要问题是,如果需要file_open的实例,则不要调用Ui_MainWindow方法并在文件对话框中提供MainWindow的实例。 }。以下是解决此问题的方法:

def retranslateUi(self, MainWindow):
    self.main_window = MainWindow
    # your code here
    self.actionOpen_File.triggered.connect(self.file_open)

def file_open(self):
    name, _ = QtWidgets.QFileDialog(self.main_window, 'Open File', options=QtWidgets.QFileDialog.DontUseNativeDialog)
    # your code here

但是,在您的代码结构中,此问题的范围之外存在一些问题,这将导致难以扩展。

答案 1 :(得分:0)

谢谢大家<
这是我的回答我真正想做的事情。

IsList