Python - PyQt5 如何将文件作为输入?
Posted
技术标签:
【中文标题】Python - PyQt5 如何将文件作为输入?【英文标题】:Python - PyQt5 how to take file as a input? 【发布时间】:2018-03-14 16:47:46 【问题描述】:下面附有屏幕截图:所以基本上我想将文件作为输入(即 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_())
【问题讨论】:
我尝试了一些东西——你尝试了什么,但它以什么方式没有达到你的预期? @mkrieger1 - 我的意思是我尝试了上述代码。如果您理解我的问题陈述,请帮助我 不,我不明白,也帮不了你。你已经写了你想做的事情(即使我不完全理解这一点),但没有写会发生什么,或者如果你得到一个错误,或其他任何事情。 您想在大文本框中显示文件的内容吗?单击“打开文件”和单击“操作”之间应该有什么区别? 我注意到您已经定义了一个函数file_open
,但没有在任何地方使用它。这可能是一个提示。
【参考方案1】:
您遇到的两个主要问题是您没有调用您的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
但是,您的代码结构中存在一些超出此问题范围的问题,这将使其难以扩展。
【讨论】:
您是否将self.main_window = MainWindow
添加到您的retranslateUi
方法中?【参考方案2】:
谢谢大家 这是我的回答我真正想做的事情。
# -*- 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_())
【讨论】:
以上是关于Python - PyQt5 如何将文件作为输入?的主要内容,如果未能解决你的问题,请参考以下文章