cdelker / schemdraw

MIT License
108 stars 20 forks source link

tkinter integration #13

Closed zorce closed 1 year ago

zorce commented 1 year ago

Tries to get the matplotlib integration to work with the tkinter matplotlib backend. But cant seem to get the drawing to work with FigureCanvasTkAgg

AttributeError: Figure.show works only for figures managed by pyplot, normally created by pyplot.figure()

Any ideas on how to fix this? Bellow is a small snippet example.

from tkinter import *
from tkinter import ttk

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk

import schemdraw
import schemdraw.elements as elm

class Overview(Tk):

    def __init__(self):
        super().__init__()

        self.title('Overview')

        frame = ttk.Frame(self)
        frame.pack()
        ttk.Label(frame, text="hello").pack()

        drawing = schemdraw.Drawing()
        drawing.add(elm.Capacitor())
        drawing.add(elm.Resistor())
        drawing.add(elm.Wire('-').color('red'))

        fig = plt.Figure(tight_layout=True)
        canvas = FigureCanvasTkAgg(fig, self)
        ax = fig.subplots()
        ax.set_aspect('equal')

        canvas.draw()
        drawing.draw(canvas=ax)

        canvas.get_tk_widget().pack()

if __name__ == '__main__':

    app = Overview()
    app.mainloop()
TheCleric commented 1 year ago

I was able to get SOMETHING to work here, though I'm unsure if it's what you were hoping for. If you change your drawing.draw(...) call to drawing.draw(canvas=ax, backend="matplotlib") it does show a diagram instead of an error.

zorce commented 1 year ago

That will draw circuit diagram in a separate matplotlib window, but I'm looking for an integrated diagram in my tkinter frame. Also tried with the latest version and newer python, but get the same error. But in with the warning now "DeprecationWarning: Use of backend is deprecated. Use canvas. drawing.draw(canvas=ax, backend='matplotlib')"

TheCleric commented 1 year ago

Ahh, I think I figured it out! Try drawing.draw(canvas=ax, show=False)

zorce commented 1 year ago

Ah nice!! Thanks!