我有一个python,它将文本文件附加到列表然后读取它们 然后,当用户选择此列表时,系统必须在编辑文本小部件中显示txt文件的内容。
问题是系统不会在编辑文本中显示文件的内容。
我正在使用 PyQt5 库并将GUI类与功能类分开,我将其导入为 pdfviewer 。
我将不胜感激任何帮助。
代码:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QApplication, QCheckBox, QColorDialog, QDialog,
QErrorMessage, QFileDialog, QFontDialog, QFrame, QGridLayout,
QInputDialog, QLabel, QLineEdit, QMessageBox, QPushButton)
from PyQt5.QtCore import QDir, Qt
import os,time
import pdfviewer
from openFileDialog import *
class pdfViewer(pdfviewer.Ui_PdfPreviewWindow):
def __init__(self,PdfPreviewObj ):
self.PdfPreviewObj =PdfPreviewObj
self.setupUi(PdfPreviewObj)
self.PdfPreviewObj.show()
self.pushButtonOpenFolder.clicked.connect(self.setExistingDirectory)
def readFile(self, currentFile):
currentFile = self.listWidgetPDFlist.currentItem().text()
print(currentFile)
try:
with open(currentFile) as ctf:
ctfRead = ctf.read()
print(ctfRead)
except Exception as e:
print("the selected file is not readble because : {0}".format(e))
'''
print the current file name based on the list index
'''
def print_item(self):
print(self.listWidgetPDFlist.currentItem().text())
'''
add the file path to the second listWidget where the cursor currently is.
'''
#here i try to display the file content based
#on the user selection for the item from the list
self.textEditContentPreview.append(self.readFile(self.listWidgetPDFlist.currentItem()))
答案 0 :(得分:0)
我解决了这个问题,需要从readFile()函数返回
因此解决方案成为:
def readFile(self, currentFile):
currentFile = self.listWidgetPDFlist.currentItem().text()
print(currentFile)
try:
with open(currentFile) as ctf:
ctfRead = ctf.read()
print(ctfRead)
return(ctfRread) # <==== the solution
except Exception as e:
print("the selected file is not readble because : {0}".format(e))