Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python-PyQt5如何将文件作为输入?_Python 3.x_Pyqt5 - Fatal编程技术网

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

Python 3.x Python-PyQt5如何将文件作为输入?,python-3.x,pyqt5,Python 3.x,Pyqt5,下面附有屏幕截图:所以基本上我想将文件作为inputi.e后缀_list.txt,而不是在函数中指定文件位置,当我单击操作按钮时,我需要在QtTextEdit中执行输出 我试过一些东西,但没能得到。请帮帮我。我是PYQT5的新手谢谢 这是我的gui代码: 您面临的两个主要问题是,您没有调用file_open方法,并且在需要成为MainWindow实例时,您正在向文件对话框提供Ui_MainWindow实例。以下是如何解决此问题: def retranslateUi(self, MainWindo

下面附有屏幕截图:所以基本上我想将文件作为inputi.e后缀_list.txt,而不是在函数中指定文件位置,当我单击操作按钮时,我需要在QtTextEdit中执行输出 我试过一些东西,但没能得到。请帮帮我。我是PYQT5的新手谢谢 这是我的gui代码:


您面临的两个主要问题是,您没有调用file_open方法,并且在需要成为MainWindow实例时,您正在向文件对话框提供Ui_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
但是,您的代码结构中存在一些超出此问题范围的问题,这将使其难以扩展。

谢谢大家< 这是我的答案,我实际上想做什么


我尝试了一些东西-你尝试了什么?它以什么方式没有像你期望的那样工作?@mkrieger1-我的意思是我尝试了上面提到的代码。如果你理解我的问题陈述,请帮助我,我不理解,也帮不了你。即使我不完全理解,你也已经写下了你想做的事情,但是没有写下发生的事情,或者如果你出现错误,或者其他任何事情。你想在大文本框中显示文件的内容吗?单击打开文件和单击操作之间的区别是什么?我注意到您已经定义了一个函数文件,但没有在任何地方使用它。这可能是一个提示。您是否已将self.main\u window=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
# -*- 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.actionOpen_File.setShortcut('Ctrl+O')
        self.actionOpen_File.triggered.connect(self.file_open)
        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(None, 'Open File', options=QtWidgets.QFileDialog.DontUseNativeDialog)
        file = open(name, 'r')
        with file:
            text = file.read()
            self.text_output.setText(text)

    def suffix_remove(self): 
        suffix_list = []
        dictionary = {}
        lists = ['athletic','kitchenette','helpful','terrify']
        f = self.text_output.toPlainText()

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