【问题标题】:Use methods as arguments for a partial command使用方法作为部分命令的参数
【发布时间】:2020-01-13 02:01:52
【问题描述】:

我可以在部分语句中使用带参数的方法作为参数吗?

我正在使用 partial 并且可以传递字符串,但是当我尝试传递带有变量作为参数的方法时,单击消息框按钮时没有得到任何结果,但是当工具打开并执行 parial 语句时我无需与工具交互即可获得结果

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

我希望部分语句能够工作,因为我只传递了一个应该被识别为参数的条件。论点是部分陈述是否重要?

【问题讨论】:

    标签: python python-2.7 pyqt


    【解决方案1】:

    partial(fun, x) 生成一个可调用对象,因此如果要调用该函数,必须使用 ()

    # ...
    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_())
    

    【讨论】:

    • 不幸的是,这不起作用,只会使工具崩溃。yes_fn 和 no_fn 只是消息框方法的变量,所以我应该可以随意传递它,但它不起作用使用 partial 作为消息框方法的参数。我所说的部分陈述是否可行/正确?
    • @PyHulk 真奇怪,我添加了一个 MWE 以便您进行测试,如果它适用于您,那么问题出在您未显示的代码部分,因此您必须提供 @987654321 @
    • 感谢您的回复,该课程可以正常工作,所以我会考虑将其与我所拥有的结合起来。抱歉,如果这篇文章没有提供应有的信息,我将确保在未来提供更多的信息。再次感谢。
    • @PyHulk 如果我的回答对您有所帮助,请不要忘记标记为正确,如果您不知道该怎么做,请查看tour
    • 我已标记为正确。有一段时间没上,所以我的礼仪有点不对劲,但我会记住未来的。谢谢。
    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 2011-11-18
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多