0

I subclassed a widget (MyButton) and gave it my stylesheet,animation,etc.

in another class (MyForm) i want to use that button but without layout .

class MyButton(QPushButton):
    def __init__(self):
        super(MyButton, self).__init__()
        .
        .


class MyForm(QDialog):
    def __init__(self):
        super(MyForm, self).__init__()
        self.btn=MyButton(self)
        self.btn.move(220, 30)

If i try to say self.btn=MyButton(self) and then self.btn.move() this error comes up:

self.btn=MyButton(self)
TypeError: __init__() takes 1 positional argument but 2 were given

what should i do?

IMAN4K
  • 1,265
  • 3
  • 24
  • 42
  • I think you should remove the self from `self.btn=MyButton(self)` ---> `self.btn=MyButton()` – Ehsan88 Dec 31 '15 at 17:15
  • @ Ehsan Abd in such case self.btn would not be visible in MyForm.BTW with standard QPushButton(self) it's fine – IMAN4K Dec 31 '15 at 17:24

1 Answers1

2

The __init__ function of your MyButton class takes only one argument - self, the pointer to the newly created MyButton instance. But you give it two parameters - self, which every method receives as the first argument, and parent. The possible solution is:

class MyButton(QtGui.QPushButton):
    def __init__(self, parent):
        super(MyButton, self).__init__(parent)

That's not handy one, though, as you have to specify the whole (possibly very long) list of your superclass' parameters, both positional and named. In such situations, Python stars (*, **) are your best friends:

class MyButton(QtGui.QPushButton):
    def __init__(self, *args, **kwargs):
        super(MyButton, self).__init__(*args, **kwargs)
Community
  • 1
  • 1
Alexander Lutsenko
  • 2,130
  • 8
  • 14