简体   繁体   中英

How to access variables from one class to another class in PyQt4?

I want to get a string from the main window to use in a window triggered with a click. I know how to do it by putting all statements into a single class, but now I'm trying to do the same thing with one class per window. Here is the code:

import sys
from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.value = QtGui.QLineEdit('23')
        self.button = QtGui.QPushButton('Open Dialog')
        self.button.clicked.connect(self.openDialog)

        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.value)
        vbox.addWidget(self.button)
        self.setLayout(vbox)

    def openDialog(self):
        self.entry = self.value.text()
        print(self.entry)
        Dialog().exec_()

class Dialog(QtGui.QDialog):
    def __init__(self, parent=Window):
        super().__init__()

        win = Window()
        self.text = win.entry
        self.label = QtGui.QLabel(self.text)

        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.label)
        self.setLayout(hbox)


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

if __name__ == '__main__':
    main()

But I'm getting the error " AttributeError: 'Window' object has no attribute 'entry' " and I don't know any other way to try fix it. Can someone help me with it?

Create an instance of Dialog in the openDialog method, so that you can access its attributes directly. That way, the two classes can operate more independently, and you won't need to access the Window class from within the Dialog class:

    def openDialog(self):
        dialog = Dialog(self)
        dialog.label.setText(self.value.text())
        dialog.exec_()
        print(dialog.label.text())

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.label = QtGui.QLabel(self)
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.label)
        self.setLayout(hbox)

Here

win = Window()
self.text = win.entry

you declare a new window and accesing its entry field but on your window class

class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.initUI()

the entry field is not constructed.

So

  1. Either you want to create a new window, so you have to put the self.entry on the constructor
  2. You want to use the existing window an access its entry after calling openDialog

Edit:

Maybe here

class Dialog(QtGui.QDialog):
    def __init__(self, parent=Window):
        super().__init__()

you are initializing the class wrong. The parent constructor should be called with parent=Window too. Then from inside the dialog you could reference the window by doing self.parent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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