在运行函数后尝试从其他窗口获取文件时遇到问题。最终目标是从一个窗口获取文件并在主窗口上显示整个文件。我有textedit设置显示文件。代码如下:
from PyQt5.QtWidgets import (QMainWindow, QTextEdit, QAction, QFileDialog, QApplication, QWidget, QDialog, QInputDialog, QLineEdit, QLabel, QGridLayout, QPushButton)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import sys
import analyzer as AN
pathtoJSON = "C:\\Tools"
class Second(QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.initUI()
def initUI(self):
self.button = QPushButton('1', self)
self.button.clicked.connect(self.analyze)
self.button.resize(self.button.sizeHint())
self.button.move(167, 220)
self.text = "2".format(0, 0)
self.label = QLabel(self.text, self)
self.label.move(20,15)
self.textbox1 = QLineEdit(self)
self.textbox1.move(55, 20)
self.textbox1.resize(300,20)
self.text = "3".format(0, 0)
self.label = QLabel(self.text, self)
self.label.move(20,65)
self.textbox2 = QLineEdit(self)
self.textbox2.move(150, 70)
self.textbox2.resize(100,20)
self.text = "4".format(0, 0)
self.label = QLabel(self.text, self)
self.label.move(20,115)
self.textbox3 = QLineEdit(self)
self.textbox3.move(150, 120)
self.textbox3.resize(200,20)
self.text = "5".format(0, 0)
self.label = QLabel(self.text, self)
self.label.move(20,165)
self.textbox4 = QLineEdit(self)
self.textbox4.move(150, 170)
self.textbox4.resize(200, 20)
self.setGeometry(200, 200, 400, 260)
self.setFixedSize(self.size())
self.setWindowTitle('Settings')
def analyze(self):
#This command here generates a file from the following inputs from Second Window. This file appears at the following path in the variable pathtoJSON.
# AN.checkStream(self.textbox1.text(), self.textbox2.text(), self.textbox3.text(), self.textbox4.text())
global urlBox
urlBox = self.textbox1.text()
global durationBox
durationBox = self.textbox2.text()
global json_filename_box
json_filename_box = self.textbox3.text()
global playlistBox
playlistBox = self.textbox4.text()
f = open(pathtoJSON + "\\" + str(self.textbox3.text()) + ".txt")
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
#The file should be displayed in this textEdit box
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
#This action does nothing for now.
openFile = QAction(QIcon('open.png'), 'Run with configuration', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Run with file')
openFile.triggered.connect(self.showDialog)
#This is supposed to ask you for your settings of the file and then when you pressed the button. It analyzes the file with the analyzer script and is supposed to grab the file
singleURL = QAction('Input URL', self)
singleURL.setStatusTip('Analyze with a single URL')
singleURL.triggered.connect(self.showSettings)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
fileMenu.addAction(singleURL)
self.runURL = Second(self)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('HLS Automation')
self.showMaximized()
def showSettings(self):
file = self.runURL.show()
def showDialog(self):
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
if fname[0]:
f = open(fname[0], 'r')
with f:
data = f.read()
self.textEdit.setText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
有两个不同的窗口。单击"输入URL"可访问第二个主窗口。在此窗口中,您可以运行Analyzer(此脚本是由我创建的)。所以我的想法是运行分析器脚本。它在运行后生成一个文件,并存储在pathtoJSON指示的目录中。它做得很好,但到目前为止,我只能将文件保存到变量中。该变量也在第二主窗口函数中。
有人可以向我解释如何从第二个窗口获取文件并将其显示在主窗口的textEdit部分吗?我在代码中做错了什么阻止我这样做?
答案 0 :(得分:0)
试一试:
from PyQt5.QtWidgets import (QMainWindow, QTextEdit, QAction, QFileDialog,
QApplication, QWidget, QDialog, QInputDialog, QLineEdit, QLabel, QGridLayout, QPushButton)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import sys
#import analyzer as AN
pathtoJSON = "C:\\Tools"
class Second(QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.initUI()
def initUI(self):
self.button = QPushButton('1', self)
self.button.clicked.connect(self.analyze)
self.button.resize(self.button.sizeHint())
self.button.move(167, 220)
self.text = "2".format(0, 0)
self.label = QLabel(self.text, self)
self.label.move(20,15)
self.textbox1 = QLineEdit(self)
self.textbox1.move(55, 20)
self.textbox1.resize(300,20)
self.text = "3".format(0, 0)
self.label = QLabel(self.text, self)
self.label.move(20,65)
self.textbox2 = QLineEdit(self)
self.textbox2.move(150, 70)
self.textbox2.resize(100,20)
self.text = "4".format(0, 0)
self.label = QLabel(self.text, self)
self.label.move(20,115)
self.textbox3 = QLineEdit(self)
self.textbox3.move(150, 120)
self.textbox3.resize(200,20)
self.text = "5".format(0, 0)
self.label = QLabel(self.text, self)
self.label.move(20,165)
self.textbox4 = QLineEdit(self)
self.textbox4.move(150, 170)
self.textbox4.resize(200, 20)
self.setGeometry(200, 200, 400, 260)
self.setFixedSize(self.size())
self.setWindowTitle('Settings')
def analyze(self):
#This command here generates a file from the following inputs from Second Window. This file appears at the following path in the variable pathtoJSON.
# AN.checkStream(self.textbox1.text(), self.textbox2.text(), self.textbox3.text(), self.textbox4.text())
global urlBox
urlBox = self.textbox1.text()
global durationBox
durationBox = self.textbox2.text()
global json_filename_box
json_filename_box = self.textbox3.text()
global playlistBox
playlistBox = self.textbox4.text()
f = open(pathtoJSON + "\\" + str(self.textbox3.text()) + ".txt")
# +++
self.ex = Example()
self.ex.showFile(f)
self.ex.show()
self.close()
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
#The file should be displayed in this textEdit box
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
#This action does nothing for now.
openFile = QAction(QIcon('open.png'), 'Run with configuration', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Run with file')
openFile.triggered.connect(self.showDialog)
#This is supposed to ask you for your settings of the file and then when you pressed the button. It analyzes the file with the analyzer script and is supposed to grab the file
singleURL = QAction('Input URL', self)
singleURL.setStatusTip('Analyze with a single URL')
singleURL.triggered.connect(self.showSettings)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
fileMenu.addAction(singleURL)
self.runURL = Second(self)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('HLS Automation')
self.showMaximized()
def showSettings(self):
file = self.runURL.show()
self.hide() # +++
def showDialog(self):
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
if fname[0]:
f = open(fname[0], 'r')
with f:
data = f.read()
self.textEdit.setText(data)
# +++
def showFile(self, f):
with f:
data = f.read()
self.textEdit.setText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())