Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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 在运行PyQt函数后,如何从一个主窗口到另一个主窗口获取信息?_Python_Pyqt_Pyqt5 - Fatal编程技术网

Python 在运行PyQt函数后,如何从一个主窗口到另一个主窗口获取信息?

Python 在运行PyQt函数后,如何从一个主窗口到另一个主窗口获取信息?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,在运行函数后,尝试从其他窗口获取文件时遇到问题。最终目标是从一个窗口获取一个文件,并在主窗口上显示整个文件。我已经设置了文本编辑来显示文件。代码如下: from PyQt5.QtWidgets import (QMainWindow, QTextEdit, QAction, QFileDialog, QApplication, QWidget, QDialog, QInputDialog, QLineEdit, QLabel, QGridLayout, QPushButton) from PyQ

在运行函数后,尝试从其他窗口获取文件时遇到问题。最终目标是从一个窗口获取一个文件,并在主窗口上显示整个文件。我已经设置了文本编辑来显示文件。代码如下:

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指示的目录中。它做得很好,但到目前为止,我只能将文件保存到变量中。此外,该变量位于第二个主窗口函数中

那么,有人能告诉我如何从第二个窗口获取文件并将其显示在主窗口的文本编辑部分吗?我的代码中是否存在阻止我这样做的错误?

试试看:

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

show analyzer.py文件非常感谢!让我看看我是否理解这一点。那么在analyze函数中,您是否调用示例窗口以便在那里显示信息?另外,如果必须这样做,那么在代码底部的ex=Example()会做什么?是的,在第二节课中,您会做所有需要做的事情,包括调用analyze。当需要将数据从第二个窗口传输到显示和其他计算时,可以创建示例类构造函数,并将数据传递到此类的所需方法以进行显示或进一步处理。