How to pass arguments to functions by the click of button in PyQt?

后端 未结 6 1501
时光说笑
时光说笑 2020-12-07 20:45

I want to pass the arguments to a function when I click the button. What should I add to this line button.connect(button, QtCore.SIGNAL(\'clicked()\'), calluser(name))

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 21:47

    In Python, class instances are callable. You can just use an instance of a class as a function. That class can contain whatever you want. (In some languages or frameworks, a callable object is called a functor or a function object.)

    class CallUser:
        def __init__(self, name):
            self.name = name
        def __call__(self):
            print(self.name)
    
    def Qbutton():
        button = QtGui.QPushButton("button",widget)
        name = "user"
        button.setGeometry(100,100, 60, 35)
        button.clicked.connect(CallUser(name))
        # Object of type CallUser will work as a function!
    

提交回复
热议问题