PyHulk
PyHulk

Reputation: 48

Use methods as arguments for a partial command

Can I use a method with arguments as an argument in a partial statement?

I am using partial and can pass strings, but when I try to pass a method with variables as an arguments, I do not get any results when I click the message box buttons but the parial statements are executed when the tool opens and I get the result(s) without interacting with the tool

self.ui.psh_bttn_go.clicked.connect(partial(self.yes_no_messagebox, 'poop', 'loop'))# this works

self.ui.psh_bttn_go.clicked.connect(partial(self.yes_no_messagebox, (partial(self.test_fn, 'Help')), (partial(self.test_fn, 'meeeeee!'))))) # This produces nothing

# Using the following methods

def yes_no_messagebox(self, yes_fn, no_fn):
    yes_no_msg_box = QMessageBox()
    yes_no_msg_box.setWindowTitle("Continue?")
    yes_no_msg_box.setText('This is the message box message')
    yes_no_msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
    yes_no_msg_box.setDefaultButton(QMessageBox.No)
    yes_no_msg_box_ex = yes_no_msg_box.exec_()

    if yes_no_msg_box_ex == QMessageBox.Yes:
        yes_fn
    elif yes_no_msg_box_ex == QMessageBox.No:
        no_fn

def test_fn(self, msg = ''):
    print msg

I expected the partial statement to work as I am only passing in an condition that should be recognised as an argument. Does it matter that the argument is a partial statement?

Upvotes: 1

Views: 68

Answers (1)

eyllanesc
eyllanesc

Reputation: 244202

partial(fun, x) generates a callable so if you want to invoke the function you must use ():

# ...
if yes_no_msg_box_ex == QMessageBox.Yes:
    yes_fn() # <---
elif yes_no_msg_box_ex == QMessageBox.No:
    no_fn()  # <---

MWE:

from functools import partial

from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        button = QPushButton("Press me")
        self.setCentralWidget(button)

        button.clicked.connect(
            partial(
                self.yes_no_messagebox,
                (partial(self.test_fn, "Help")),
                (partial(self.test_fn, "meeeeee!")),
            )
        )

    def yes_no_messagebox(self, yes_fn, no_fn):

        yes_no_msg_box = QMessageBox()
        yes_no_msg_box.setWindowTitle("Continue?")
        yes_no_msg_box.setText("This is the message box message")
        yes_no_msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        yes_no_msg_box.setDefaultButton(QMessageBox.No)
        yes_no_msg_box_ex = yes_no_msg_box.exec_()

        if yes_no_msg_box_ex == QMessageBox.Yes:
            yes_fn()
        elif yes_no_msg_box_ex == QMessageBox.No:
            no_fn()

    def test_fn(self, arg):
        print(arg)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)

    w = MainWindow()
    w.show()

    sys.exit(app.exec_())

Upvotes: 1

Related Questions