调用父类中的函数并访问父类

2024-09-27 07:33:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的主窗口中,我设置了一个变量self.print_this。然后我调用另一个类PhotoViewer,然后在该类中从主窗口调用一个函数。在该函数中,我尝试打印self.print\u这个但是我得到以下错误:AttributeError: PhotoViewer对象没有属性print_this 如何访问window类的self或避免将PhotoViewer的self发送到printfromwindow函数?你知道吗

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QPoint, QRect, QSize, pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QApplication, QRubberBand, QColorDialog
from PyQt5.QtGui import QPixmap, QPainter, QPen
import sys

class PhotoViewer(QtWidgets.QGraphicsView):
    photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
    rectChanged = pyqtSignal(QRect)

    def __init__(self, parent):
        super(PhotoViewer, self).__init__(parent)
        Window.printfromwindow(self)

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.print_this='test'
        PhotoViewer(self)

    def printfromwindow(self):
        print(self.print_this)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 800, 600)
    window.show()
    sys.exit(app.exec_())

Tags: 函数fromimportselfinitwindowthispyqt5
2条回答

要访问父类,需要通过函数传递父类。def printfromwindow(Window)。然后从父类继承所有属性。还可以使用super().__init__修改子类,以便在不影响父类的情况下向子类添加更改

未经测试,但可能是这样的。注意,在您的代码中,您正在调用WindowPhotoViewer类的方法,但没有构造这些类的实例。你知道吗

  1. 如果希望能够引用这些实例的方法/属性,则需要在它们的父/子类实例中实例化并保留这些实例。

  2. 正如我在上面的评论中所指出的,Window.printfromwindow(self)不应该有self参数(而是应该引用<instance of Window>.printfromwindow())。

Window构造函数中,我将self.viewer分配给PhotoViewer类的一个实例,并将self(它是Window实例)作为parent参数传递给它的构造函数。你知道吗

然后,在PhotoViewer类构造函数中,我们执行self.window = parent,它应该允许您调用self.window.printfromwindow()

class PhotoViewer(QtWidgets.QGraphicsView):
    photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
    rectChanged = pyqtSignal(QRect)

    def __init__(self, parent):
        super(PhotoViewer, self).__init__(parent)
        self.window = parent  # relates the "parent" Window instance to this "child" PhotoViewer instance
        self.window.printfromwindow()  # calls the printfromwindow method from the "parent" Window instance

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.print_this='test'
        self.viewer = PhotoViewer(self)  # creates an instance of PhotoViewer class as an attribute of this Window instance

    def printfromwindow(self):
        print(self.print_this)

如果您想使它更容易使用(即,不只是从PhotoViewer的构造函数中),那么将Window.printfromwindow分配给PhotoViewer的属性,如下所示:

class PhotoViewer(QtWidgets.QGraphicsView):
    photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
    rectChanged = pyqtSignal(QRect)

    def __init__(self, parent):
        super(PhotoViewer, self).__init__(parent)
        self.window = parent  # relates the "parent" Window instance to this "child" PhotoViewer instance
        self.printfromwindow = self.window.printfromwindow  

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.print_this='test'
        self.viewer = PhotoViewer(self)  # creates an instance of PhotoViewer class as an attribute of this Window instance

    def printfromwindow(self):
        print(self.print_this)

由于函数是python中的一级对象,因此可以执行以下操作:

app = QtWidgets.QApplication(sys.argv)
window = Window()
window.viewer.printfromwindow()

相关问题 更多 >

    热门问题