How can I deleteall the layouts of a pg.GraphicsLayoutWidget??

386 views
Skip to first unread message

Thomas B.A.

unread,
Sep 20, 2022, 11:01:56 AM9/20/22
to pyqtgraph
Hello, sorry if noob question but:

I have a program where I have to update the number of plots I want to show.( Might need 3, or 15. A button should make the change)
I created a GraphicsLayoutWidget and put the plots there. When I click my button, thus wanting to delete all the plots and layouts to then create others, I use my_graphicsLayoutWidget.clear() but it only delete de lines and leave the squares of what I assume is the layouts.

How can I erase every single item in a GraphicsLayoutWidget?

png attached showing the problem. When checkbox is pressed/unpressed I execute the function that should clear all plots and layouts to generate new ones.
pyqtgraph_problem_example_.png

Martin Chase

unread,
Sep 20, 2022, 3:16:52 PM9/20/22
to pyqt...@googlegroups.com
Hey Thomas,

That's hopefully just a general PyQt question. It looks like it's answered here: https://stackoverflow.com/questions/4528347/clear-all-widgets-in-a-layout-in-pyqt

Let us know if that doesn't work,
 - Martin (he/him)

Thomas B.A.

unread,
Sep 20, 2022, 6:06:00 PM9/20/22
to pyqt...@googlegroups.com
Hello Martin!

Unfortunately, I can't seem to make this solution work.

in the error log i get:
   for i in range(self.graphWidget.count()): print(i)
AttributeError: 'GraphicsLayoutWidget' object has no attribute 'count'. Did you mean: 'font'?

So, I might be confused about how thing works, but I also tried self.graphWidget.layout and it showed the presence of "built-in method layout of GraphicsLayoutWidget object", but it doesn't work adding .clear() nor .count() to it neither

I think I'm a bit lost with this theme, do you have a solution?

btw, thank you for the help and fast reply!

--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/WHhkfCL2YFQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/CAD_p8v0-tO6gGxVEsi4WXNCoT6v9M1CC0Ga2RV2mREFMUA0x3w%40mail.gmail.com.

Martin Chase

unread,
Sep 21, 2022, 3:05:57 PM9/21/22
to pyqt...@googlegroups.com
Hey Thomas,

I think you would benefit from having a repl to play around with the objects in question. Set yourself up a dev console by adding the following code somewhere where you have access to all the important variables that you use for the widgets being displayed:
from pyqtgraph.console import ConsoleWidget
c = ConsoleWidget(namespace=locals())
c.show()
c.setWindowTitle('dev console')
When that loads, you'll be able to do inspect the objects and play with them like:
myWidget
vars(myWidget)
myWidget.graphWidget
dir(myWidget.graphWidget.layout)

Martin Chase

unread,
Sep 21, 2022, 3:56:16 PM9/21/22
to pyqt...@googlegroups.com
...I hit send a little early there.

As to your error, yeah, I was wrong earlier, your GraphicsLayoutWidget object is pyqtgraph-specific and doesn't behave like a QLayout. It has an attr `ci` on which you should be able to call `.clear()`. You can use the dev console code I posted a moment ago to inspect the e.g. `vars(yourTopLevelWidget.graphWidget) and see if any of those attrs can be `clear()`ed to good effect, or if there's something else on your top-level widget which has a layout you can clear one way or the other.

Good luck!
 - Martin

Thomas B.A.

unread,
Sep 23, 2022, 6:55:20 AM9/23/22
to pyqt...@googlegroups.com
Hello Martin,

thank you for the help, it was very interesting and it helped me found what didn't work. This said, I don't know if it was an error on my part, or an error in pyqt, I'll explain briefly:

When setting borders ( self.graphWidget.ci.setBorder((0, 50, 100))  <- my piece of code, graphWidget being my GraphicsLayoutWidget ) it seems that these borders will stay on the widget after using the command line self.graphWidget.ci.clear(), thus cleaning the lines and axes, but leaving the square borders on the graph. Those squares didn't even resize when the program window was resized, although the new plots would as normal. They would stay static on the view.

I don't know if it is a bad manipulation on my part, but I wanted to inform you just in case. If you need more info you can contact me at this email.

Anyway, my program has continued its development thanks to your help, so thank you very much!

- Thomas

--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/WHhkfCL2YFQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages