Embedding, Drawing, Curser in Tkinter

Hello to everyone.

I develop a GUI and i want to embedd Matplotlib in a Tkinter Canvas. Theres
is no problem until here. But i want to draw the lines and have a curser
line in the diagram. In the code below i embedd the canvas in the
INIT-Function.

class BOStrab_Fahrzeugeinschraenkung:
  def __init__(self, top=None):
            ......
    self.ya = np.array(ddd)
    self.yb = np.array(ddd)
    self.z = np.array(ddd)
    
    self.canvasframe11 = Frame(self.TPanedwindow4_f2)
    self.canvasframe11.place(x=0, y=0, relwidth=1.0, relheight=0.95,
bordermode=INSIDE)
    self.canvasframe12 = Frame(self.TPanedwindow4_f2)
    self.canvasframe12.place(x=0, rely=0.95, relwidth=1.0, bordermode=INSIDE)
    
    global ax1
    self.fig1 = Figure (figsize=(5,4), dpi=100)

    self.ax1 = self.fig1.add_subplot(111) #f?r 2d-Plot
    self.ax1.set_title('Definition der LRUGL')
    self.ax1.set_xlabel('Breite y [mm]')
    self.ax1.set_ylabel('Hoehe z [mm]')
    
    self.ax1.axis([-2000,2000,0, 5000])
    self.ax1.grid(True)
    self.ax1.plot(self.ya,self.z, color='red', linestyle='--', marker='')
    self.ax1.plot(self.yb,self.z, color='red', linestyle='--', marker='')
    
    self.canvas1 = FigureCanvasTkAgg(self.fig1,self.canvasframe11)
    toolbar1 = NavigationToolbar2TkAgg(self.canvas1, self.canvasframe12)

    self.canvas1.get_tk_widget().pack(fill=BOTH, expand=TRUE, pady=2, padx=2,
anchor="n")

In several examples i saw the drawing in def's and the input for the drawing
from root. So the following code is function then.

def onMouseMove(event):
  ax1.lines = [ax1.lines[0]]
  ax1.axhline(y=event.ydata, color="k")
  ax1.axvline(x=event.xdata, color="k")

def updateData():
  global level1, val1
  clamp = lambda n, minn, maxn: max(min(maxn, n), minn)
  yield 1 # FuncAnimation expects an iterator

def visualize(i):
  lineVal1.set_ydata(val1)
  return lineVal1

My question is. Is my way to embedd the code in Tkinter right? Or its better
to declare the diagram functionalities outside the class and only draw into
the class?

Best regards from Stefan

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Embedding-Drawing-Curser-in-Tkinter-tp48038.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Stefan,

On a quick skim that does not look wrong.

I would not use globals and have the callbacks be methods on
your BOStrab_Fahrzeugeinschraenkung class.

Tom

···

On Sat, Aug 5, 2017 at 12:35 PM stefanxfg via Matplotlib-users < matplotlib-users at python.org> wrote:

Hello to everyone.

I develop a GUI and i want to embedd Matplotlib in a Tkinter Canvas. Theres
is no problem until here. But i want to draw the lines and have a curser
line in the diagram. In the code below i embedd the canvas in the
INIT-Function.

class BOStrab_Fahrzeugeinschraenkung:
        def __init__(self, top=None):
            ......
                self.ya = np.array(ddd)
                self.yb = np.array(ddd)
                self.z = np.array(ddd)

                self.canvasframe11 = Frame(self.TPanedwindow4_f2)
                self.canvasframe11.place(x=0, y=0, relwidth=1.0,
relheight=0.95,
bordermode=INSIDE)
                self.canvasframe12 = Frame(self.TPanedwindow4_f2)
                self.canvasframe12.place(x=0, rely=0.95, relwidth=1.0,
bordermode=INSIDE)

                global ax1
                self.fig1 = Figure (figsize=(5,4), dpi=100)

                self.ax1 = self.fig1.add_subplot(111) #f?r 2d-Plot
                self.ax1.set_title('Definition der LRUGL')
                self.ax1.set_xlabel('Breite y [mm]')
                self.ax1.set_ylabel('Hoehe z [mm]')

                self.ax1.axis([-2000,2000,0, 5000])
                self.ax1.grid(True)
                self.ax1.plot(self.ya,self.z, color='red', linestyle='--',
marker='')
                self.ax1.plot(self.yb,self.z, color='red', linestyle='--',
marker='')

                self.canvas1 =
FigureCanvasTkAgg(self.fig1,self.canvasframe11)
                toolbar1 = NavigationToolbar2TkAgg(self.canvas1,
self.canvasframe12)

                self.canvas1.get_tk_widget().pack(fill=BOTH, expand=TRUE,
pady=2, padx=2,
anchor="n")

In several examples i saw the drawing in def's and the input for the
drawing
from root. So the following code is function then.

def onMouseMove(event):
        ax1.lines = [ax1.lines[0]]
        ax1.axhline(y=event.ydata, color="k")
        ax1.axvline(x=event.xdata, color="k")

def updateData():
        global level1, val1
        clamp = lambda n, minn, maxn: max(min(maxn, n), minn)
        yield 1 # FuncAnimation expects an iterator

def visualize(i):
        lineVal1.set_ydata(val1)
        return lineVal1

My question is. Is my way to embedd the code in Tkinter right? Or its
better
to declare the diagram functionalities outside the class and only draw into
the class?

Best regards from Stefan

--
View this message in context:
http://matplotlib.1069221.n5.nabble.com/Embedding-Drawing-Curser-in-Tkinter-tp48038.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170805/7252dae8/attachment.html>

Forward of response.

@Stefan Please respond to the mailing list (you will need to join to post
un-moderated https://mail.python.org/mailman/listinfo/matplotlib-users )

Tom

···

---------- Forwarded message ---------
From: Stefan Schubert <stefanschubi@googlemail.com>
Date: Mon, Aug 7, 2017 at 7:02 AM
Subject: Re: [Matplotlib-users] Embedding, Drawing, Curser in Tkinter
To: Thomas Caswell <tcaswell at gmail.com>

Hi Thomas,

thank you for answerring. I have already developed further. Here is my
current code.

def PlotFrame(self):

    self.fig = Figure (figsize=(5,4), dpi=100)
    self.ax = self.fig.add_subplot(111)
    self.ax = self.fig.add_subplot(111) #fuer 2d-Plot
    self.ax.set_title('Definition der LRUGL')
    self.ax.set_xlabel('Fahrzeugbreite y [mm]')
    self.ax.set_ylabel('Fahrzeughoehe z [mm]')
    self.ax.axis([-2000,2000,0, 5000])
    self.ax.grid(True)

    self.canvas = FigureCanvasTkAgg(self.fig,self)
    self.toolbar = NavigationToolbar2TkAgg(self.canvas,self)
    self.toolbar.update()
    self.plot_widget = self.canvas.get_tk_widget()
    self.plot_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    self.toolbar.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)

    self.ax.plot(np.zeros(100))

    def onMouseMove(event):

        self.ax.lines = [self.ax.lines[0]]
        if event.xdata and event.ydata: # python3 often has None
            self.ax.axhline(y=event.ydata, color="k") #(y=event.ydata,
color="k")
            self.ax.axvline(x=event.xdata, color="k") #(x=event.xdata,
color="k")
        self.canvas.show()

    self.canvas.mpl_connect('motion_notify_event', onMouseMove)
    self.canvas.show()

    def fig_ax_request():
        publish('FIG,AX',self.fig,self.ax)

    subscribe('REQUEST_FIG,AX',fig_ax_request)

def DrawPlot(self):

    def test_plot():

self.ax.imshow(np.random.normal(0.,1.,size=[1000,1000]),cmap="hot",aspect="auto")
        self.fig.canvas.draw()

    self.buttonAAA['command'] = test_plot

    def get_fig_ax(fig,ax):
        self.fig = fig
        self.ax = ax

    subscribe('FIG,AX',get_fig_ax)
    publish('REQUEST_FIG,AX')

Now i have a table for input and an embedded diagram in tkinter canvas.
This code above is only for testing. I want the functionality that i can
write values in the table and let it simultanously paint into
matplot-Canvas. Hav eyou some suggestion or imrpovements to do that ?

Best regards from Stefan

On 6 August 2017 at 01:58, Thomas Caswell <tcaswell at gmail.com> wrote:

Stefan,

On a quick skim that does not look wrong.

I would not use globals and have the callbacks be methods on
your BOStrab_Fahrzeugeinschraenkung class.

Tom

On Sat, Aug 5, 2017 at 12:35 PM stefanxfg via Matplotlib-users < > matplotlib-users at python.org> wrote:

Hello to everyone.

I develop a GUI and i want to embedd Matplotlib in a Tkinter Canvas.
Theres
is no problem until here. But i want to draw the lines and have a curser
line in the diagram. In the code below i embedd the canvas in the
INIT-Function.

class BOStrab_Fahrzeugeinschraenkung:
        def __init__(self, top=None):
            ......
                self.ya = np.array(ddd)
                self.yb = np.array(ddd)
                self.z = np.array(ddd)

                self.canvasframe11 = Frame(self.TPanedwindow4_f2)
                self.canvasframe11.place(x=0, y=0, relwidth=1.0,
relheight=0.95,
bordermode=INSIDE)
                self.canvasframe12 = Frame(self.TPanedwindow4_f2)
                self.canvasframe12.place(x=0, rely=0.95, relwidth=1.0,
bordermode=INSIDE)

                global ax1
                self.fig1 = Figure (figsize=(5,4), dpi=100)

                self.ax1 = self.fig1.add_subplot(111) #f?r 2d-Plot
                self.ax1.set_title('Definition der LRUGL')
                self.ax1.set_xlabel('Breite y [mm]')
                self.ax1.set_ylabel('Hoehe z [mm]')

                self.ax1.axis([-2000,2000,0, 5000])
                self.ax1.grid(True)
                self.ax1.plot(self.ya,self.z, color='red',
linestyle='--', marker='')
                self.ax1.plot(self.yb,self.z, color='red',
linestyle='--', marker='')

                self.canvas1 =
FigureCanvasTkAgg(self.fig1,self.canvasframe11)
                toolbar1 = NavigationToolbar2TkAgg(self.canvas1,
self.canvasframe12)

                self.canvas1.get_tk_widget().pack(fill=BOTH, expand=TRUE,
pady=2, padx=2,
anchor="n")

In several examples i saw the drawing in def's and the input for the
drawing
from root. So the following code is function then.

def onMouseMove(event):
        ax1.lines = [ax1.lines[0]]
        ax1.axhline(y=event.ydata, color="k")
        ax1.axvline(x=event.xdata, color="k")

def updateData():
        global level1, val1
        clamp = lambda n, minn, maxn: max(min(maxn, n), minn)
        yield 1 # FuncAnimation expects an iterator

def visualize(i):
        lineVal1.set_ydata(val1)
        return lineVal1

My question is. Is my way to embedd the code in Tkinter right? Or its
better
to declare the diagram functionalities outside the class and only draw
into
the class?

Best regards from Stefan

--
View this message in context:
http://matplotlib.1069221.n5.nabble.com/Embedding-Drawing-Curser-in-Tkinter-tp48038.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Mit freundlichen Gr??en

Schubert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170807/eed91e15/attachment-0001.html>

Hi Thomas,

okay we can communicate here.

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Embedding-Drawing-Curser-in-Tkinter-tp48038p48173.html
Sent from the matplotlib - users mailing list archive at Nabble.com.