Pass extra arguments to PyQt slot without losing default signal arguments

后端 未结 1 645
北恋
北恋 2021-02-03 11:05

A PyQt button event can be connected in the normal way to a function so that the function receives the default signal arguments (in this case the button checked state):

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-03 11:26

    Your lambda could take an argument:

    def connections(self):
        my_button.clicked.connect(lambda checked: self.on_button(checked, 'hi'))
    
    def on_button(self, checked, message):
        print checked, message   # prints "True, hi"
    

    Or you could use functools.partial:

    # imports the functools module
    import functools 
    
    def connections(self):
        my_button.clicked.connect(functools.partial(self.on_button, 'hi'))
    
    def on_button(self, message, checked):
        print checked, message   # prints "True, hi"
    

    0 讨论(0)
提交回复
热议问题