Drawing electronic circuits or schematics with schemdraw Python package in a wxPython GUI

794 views
Skip to first unread message

mbre...@gmail.com

unread,
Nov 25, 2021, 3:37:13 AM11/25/21
to wxPython-users
Hello

I would like to draw a simple electronic circuit diagram in a wxPython GUI. The circuit diagram is created programmatically using the schemdraw package (see here: https://schemdraw.readthedocs.io/en/latest/index.html).

At first I thought of the following approach:
(1) Extract the SVG bytes from the schemdraw object: bytes = drawing.get_imagedata('svg')
(2) Use this with wx.svg.SVGimage.CreateFromBytes(bytes) to create the SVGimageBase for use with wxPython
However, it seems wxPython version 4.1 is required for step (2), while I am stuck with 4.0 on my machine.

I am sure there are other ways (maybe via matplotlib), but I don't know where (and how) to look. Any pointers or ideas would be welcome!

Thanks!

Dietmar Schwertberger

unread,
Nov 25, 2021, 1:24:27 PM11/25/21
to wxpytho...@googlegroups.com
On 25.11.2021 09:37, mbre...@gmail.com wrote:
> At first I thought of the following approach:
> (1) Extract the SVG bytes from the schemdraw object: bytes =
> drawing.get_imagedata('svg')
> (2) Use this with wx.svg.SVGimage.CreateFromBytes(bytes) to create the
> SVGimageBase for use with wxPython
> However, it seems wxPython version 4.1 is required for step (2), while
> I am stuck with 4.0 on my machine.

Why do you want to convert to SVG first? Schemdraw get_imagedata() seems
to support PNG export.

Regards,

Dietmar

Matthias Brennwald

unread,
Nov 25, 2021, 1:33:22 PM11/25/21
to wxpytho...@googlegroups.com

I was thinking that a vector graphics format would be better in view of different (high) DPI environments. However, PNG (or other bitmap formats) are not necessarily out of question if there is no good way with vector graphics.

How could I make use of the PNG output from schemdraw in wxPython? The PNG examples I found for wxPython always make use of a PNG file as the source for the image data.

Dietmar Schwertberger

unread,
Nov 25, 2021, 2:28:18 PM11/25/21
to wxpytho...@googlegroups.com
On 25.11.2021 19:33, Matthias Brennwald wrote:
I was thinking that a vector graphics format would be better in view of different (high) DPI environments. However, PNG (or other bitmap formats) are not necessarily out of question if there is no good way with vector graphics.

That's only relevant if you want e.g. to zoom and edit or if Schemdraw does no good job in rendering into the image.

If you want to zoom etc., i.e. you want to edit your schematics, then you should look into something else anyway.
Maybe look at KiCad, which uses wx.

How could I make use of the PNG output from schemdraw in wxPython? The PNG examples I found for wxPython always make use of a PNG file as the source for the image data.

Have a look at the "Using Images" section of the wxPython demo and also at the wxPython documentation.


Why are you "stuck at 4.0"? It's not too hard to upgrade or to use project specific versions.

Regards,

Dietmar



Matthias Brennwald

unread,
Nov 26, 2021, 4:13:25 AM11/26/21
to wxpython-users
On Thu, 25 Nov 2021 at 20:28, Dietmar Schwertberger <mail...@schwertberger.de> wrote:
On 25.11.2021 19:33, Matthias Brennwald wrote:
I was thinking that a vector graphics format would be better in view of different (high) DPI environments. However, PNG (or other bitmap formats) are not necessarily out of question if there is no good way with vector graphics.

That's only relevant if you want e.g. to zoom and edit or if Schemdraw does no good job in rendering into the image.

If you want to zoom etc., i.e. you want to edit your schematics, then you should look into something else anyway.

Not sure what you mean here. Something else than wxPython? Something else than SVG? Or something else?

How could I make use of the PNG output from schemdraw in wxPython? The PNG examples I found for wxPython always make use of a PNG file as the source for the image data.

Have a look at the "Using Images" section of the wxPython demo and also at the wxPython documentation.

The wxPython Demo is a bit tricky to get hold of, at least in my Linux environment. The instructions at https://wxpython.org/pages/downloads/index.html are a bit sketchy when it comes to Linux.

That said, I managed to get the schemdraw PNG data to display in a wx panel. I believe the trick is convert the schemdraw PNG data to a stream, which can then feed into wx.ImageFromStream(...). I added a small self-contained example below (code inspired by googling for examples by others). I'd appreciate any thoughts on this.

-------------------------------------------------------
import wx
from io import BytesIO
import schemdraw
import schemdraw.elements as elm
             
class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        p = wx.Panel(self)
        fgs = wx.FlexGridSizer(cols=1)
        d = schemdraw.Drawing()
        d.add(elm.Resistor())
        d.add(elm.SourceV())
        data = d.get_imagedata('png')
        stream = BytesIO(data)
        img = wx.ImageFromStream(stream)
        sb = wx.StaticBitmap(p, -1, wx.BitmapFromImage(img))
        fgs.Add(sb)
        p.SetSizerAndFit(fgs)
        self.Fit()

app = wx.PySimpleApp()
frm = TestFrame()
frm.Show()
app.MainLoop()
-------------------------------------------------------


Why are you "stuck at 4.0"? It's not too hard to upgrade or to use project specific versions.

I like to write my code such that it works with the libraries available in the standard software repositories of most systems. As far as I can tell, many Linux distros have wxPython version 4.0. Version 4.1 seems to rely on some stuff that is not considered stable/ready for inclusion in most mainline distributions.

Dietmar Schwertberger

unread,
Nov 26, 2021, 11:32:47 AM11/26/21
to wxpytho...@googlegroups.com
On 26.11.2021 10:13, Matthias Brennwald wrote:
Not sure what you mean here. Something else than wxPython? Something else than SVG? Or something else?

Something other than static bitmaps. E.g. if you want to build a schematic designer, then you probably need to implement your main window in another way, i.e. draw the elements. But from your posting I understood that you just want to display some static image.


The wxPython Demo is a bit tricky to get hold of, at least in my Linux environment. The instructions at https://wxpython.org/pages/downloads/index.html are a bit sketchy when it comes to Linux.

Well, there's the section "Extra Files".
You can just download it from here and unpack it to your user directory and run from there by "python demo.py":
https://extras.wxpython.org/wxPython4/extras/4.0.0/wxPython-demo-4.0.0.tar.gz


That said, I managed to get the schemdraw PNG data to display in a wx panel. I believe the trick is convert the schemdraw PNG data to a stream, which can then feed into wx.ImageFromStream(...). I added a small self-contained example below (code inspired by googling for examples by others). I'd appreciate any thoughts on this.

Your code looks good for me.

Regards,

Dietmar

Steve Barnes

unread,
Nov 26, 2021, 12:30:06 PM11/26/21
to wxpytho...@googlegroups.com

Dietmar,

 

Following installing wxPython the command wxdemo should download (if not already done), unpack (if not already done) and run the demo for the current version of wxPython - if it doesn't I would welcome any debug information that you may have. Likewise the command wxdocs should download (if not already done), unpack (if not already done) and open in the default browser the documents for the current version of wxpython. Both of these should automatically be installed in the python/Scripts location when you pip install wxPython.

 

I put this together because of the aforementioned tricky bits and was delighted when they made it into the official distribution so if they have stopped working then please let me know.

 

Steve Barnes

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/30378c41-0bd1-00b2-c985-498010420a60%40schwertberger.de.

Reply all
Reply to author
Forward
0 new messages