Open a file from main window to a new window in Py

2019-08-10 03:00发布

问题:

I have two files, one for my main window, which has one image and one button and one for a new window. What I want it to do is that when I push the button from my main window, it lets me load a file and show it in a TextEdit widget in the new window

so here I have the files I'm using:

MainWindow.py

import sys
import os
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QTextEdit, QHBoxLayout, QLabel, QMainWindow, QAction, QFileDialog

class Window(QWidget):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.img = QLabel()
        self.relleno=QLabel()
        self.btn_load = QPushButton('Load')
        self.width = 400
        self.height = 150


        self.init_ui()

    def init_ui(self):
        self.img.setPixmap(QtGui.QPixmap("someimage.png"))


        h_layout = QHBoxLayout()
        v_layout = QVBoxLayout()
        h_final = QHBoxLayout()

        h_layout.addWidget(self.img)

        v_layout.addWidget(self.btn_load)

        h_final.addLayout(h_layout)
        h_final.addLayout(v_layout)

        self.btn_load.clicked.connect(self.loadafile)

        self.setLayout(h_final)
        self.setWindowTitle('This is main window')
        self.setGeometry(600,150,self.width,self.height)

        self.show()

    def loadafile(self):
        filename = QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
        with open(filename[0], 'r') as f:
            file_text = f.read()
            return file_text

def main():
    app = QApplication(sys.argv)
    main = Window()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

NewWindow.py

import os
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout
from MainWindow import loadafile

info=loadafile()

class SecondWindow(QWidget):
    def __init__(self):
        super(SecondWindow, self).__init__()
        self.text = QTextEdit(self)

        self.init_ui()

    def init_ui(self):
        v_layout = QVBoxLayout()

        v_layout.addWidget(self.text)

        self.setLayout(v_layout)
        self.setText(info)
        self.setWindowTitle('Opened Text')

        self.show()

app = QApplication(sys.argv)
shower = SecondWindow()
sys.exit(app.exec_())

I think the loadafile does return my file_text variable but I don't know how to open the new window from there. I think I need to use a destructor for main window and then show the new window but I'm not sure of how to do this (This is the first time I try OOP)

回答1:

A program is not a set of files, especially in OOP a program is the interactions of objects. And the objects interact if they have the same scope, so both windows must be created in one place so that the information from one pass to the other.

On the other hand in Qt there is a fundamental concept that is the signals, this functionality allows to notify the change of a state to another object without a lot of dependency, so in this case I will create a signal that transmits the text to the other object.

NewWindow.py

from PyQt5 import QtWidgets

class SecondWindow(QtWidgets.QWidget):
    def __init__(self):
        super(SecondWindow, self).__init__()
        self.text = QtWidgets.QTextEdit(self)
        self.init_ui()

    def init_ui(self):
        v_layout = QtWidgets.QVBoxLayout(self)
        v_layout.addWidget(self.text)
        self.setWindowTitle('Opened Text')
        self.show()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    shower = SecondWindow()
    sys.exit(app.exec_())

MainWindow.py

import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

from NewWindow import SecondWindow

class Window(QtWidgets.QWidget):
    textChanged = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.img =QtWidgets.QLabel()
        self.relleno=QtWidgets.QLabel()
        self.btn_load = QtWidgets.QPushButton('Load')
        self.width = 400
        self.height = 150


        self.init_ui()

    def init_ui(self):
        self.img.setPixmap(QtGui.QPixmap("someimage.png"))

        h_final = QtWidgets.QHBoxLayout(self)
        h_final.addWidget(self.img)
        h_final.addWidget(self.btn_load)

        self.btn_load.clicked.connect(self.loadafile)
        self.setWindowTitle('This is main window')
        self.setGeometry(600,150,self.width,self.height)

        self.show()

    @QtCore.pyqtSlot()
    def loadafile(self):
        filename, _  = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
        with open(filename, 'r') as f:
            file_text = f.read()
            self.textChanged.emit(file_text)

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = Window()
    s = SecondWindow()
    main.textChanged.connect(s.text.append)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()