如何在PyQt5中从父方法访问子对象?

时间:2017-02-08 23:38:15

标签: pyqt5

我在widget小部件中有一个按钮,它是MyApp类的中心小部件,它继承自QMainWindow。我已将widget's按钮与名为MyApp's的{​​{1}}方法相关联。这很好,因为如果我在我的主类MyApp(QMainWindow)中编写一个方法:

logic

...然后点击按钮我收到消息:

  

PyQt5.QtWidgets.QPushButton对象位于0x7ff92e19eaf8

我的问题是如何从名为def logic(self): sender = self.sender() print(sender) 的{​​{1}}方法访问MyApp's这样的logic对象的子对象?我需要阅读这一行。

MyApp.widget.saltLine

或者我定义了centralwidget QLineEdit错了吗?如果我没有中央小部件,它可以正常工作:

class MyApp(QMainWindow):

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

    def initui(self):
        self.setMinimumSize(500, 150)

        # create central widget
        widget = QWidget()

        # lines for entering data
        widget.saltLabel = QLabel("Salt:")
        widget.hashSunkenLabel = QLabel()
        widget.passwordLine = QLineEdit()
        widget.resultButton = QPushButton("&Calculate", self)

        # set layout
        grid = QGridLayout()
        grid.addWidget(widget.saltLabel, 0, 0)
        grid.addWidget(widget.passwordLine, 1, 0)
        grid.addWidget(widget.hashSunkenLabel, 2, 0)
        grid.addWidget(widget.resultButton, 2, 1)

        # set widget a grid layout and set widget
        # as central widget of QMainWindows
        widget.setLayout(grid)
        self.setCentralWidget(widget)

        # don't know how should this look like
        widget.resultButton.clicked.connect(self.logic)

    def logic(self):
        salt = self.saltLine.text()
        password = self.passwordLine.text()
        resulting_hash = crypt.crypt(password, salt)
        self.hashSunkenLabel.setText(resulting_hash)

0 个答案:

没有答案