How to remove a widget on PySide2?

In C ++, this is done in the following way (as I understand it from tutorials) (for example, deleting a button):

delete button; 

In Python, a similar construction:

 del self.next_btn; 

Gives an error message:

 Traceback (most recent call last): File "*******.py", line 94, in dell del self.next_btn; AttributeError: next_btn 
  • one
    Judging by the error, the object referenced by self does not have the next_btn attribute. Explain in more detail what you are trying to achieve. It is advisable to show a minimal, self-sufficient and reproducible example . - Sergey Gornostaev
  • Attribute is, the error was in the other, already figured out and answered your question. But neprvilny information (apparently) by mistake and entered into a stupor. - froxxendsg

2 answers 2

Try using deleteLater :

 self.next_btn.hide() self.next_btn.deleteLater() 
  • one
    That way works too, thanks. - froxxendsg 1:54 pm
  • However, in QVBLoxout causes some artifacts, voids appear incomprehensible, because in this case it does not work. - froxxendsg 4:47 pm
  • @froxxendsg, add before this hide - gil9red
  • Everything, figured out, maybe it was my shoals. - froxxendsg 1:46 pm

Before deleting, you need to hide the widget:

 self.next_btn.hide(); del self.next_btn; 
  • Use with caution, I don’t know how in C ++, but in Python for some reason this method does not free memory, or I’m doing something wrong, I would like to know exactly what is happening, but alas, there’s no one to ask. - froxxendsg 1:46 pm