简体   繁体   English

在 PyQt5 中从主窗口打开一个文件到一个新窗口(在不同的文件中)

[英]Open a file from main window to a new window in PyQt5 (in different files)

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我想要它做的是,当我从主窗口按下按钮时,它让我加载一个文件并将其显示在新窗口的TextEdit小部件中

so here I have the files I'm using:所以这里我有我正在使用的文件:

MainWindow.py主窗口.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新窗口.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.我认为loadafile确实返回了我的file_text变量,但我不知道如何从那里打开新窗口。 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)我想我需要对主窗口使用析构函数,然后显示新窗口,但我不确定如何执行此操作(这是我第一次尝试 OOP)

A program is not a set of files, especially in OOP a program is the interactions of objects.一个程序不是一组文件,尤其是在 OOP 中,一个程序是对象之间的交互。 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.另一方面,在 Qt 中有一个基本概念,即信号,此功能允许将状态更改通知另一个对象而无需太多依赖,因此在这种情况下,我将创建一个将文本传输到其他对象。

NewWindow.py新窗口.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主窗口.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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM