[Matplotlib-users] tk and matplotlib

hi
i am creating a matplotlib draw (musical sales on a guitar) in function of some parameters (tonic note, type of gamme or chord…)
i am now creating tk radio buttons so that the user can choose the parameters
i can get these parameters and create the matplotlib picture with them
but the matplotlib will open a different window
would it be possible to have a same window, divided as a table, with at the lift the radio buttons, and at the right the matplotlib result ?

Hi Anthony
hi everyone
i feel blocked at the just beginning of my attempts with PySimpleGUI/MatPlotLib
with exactly your code i get this answer :
i dont know in which direction to search :
Vincent

···

-------

2020-12-03 16:20:55.678 Python[17698:7853902] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7ff7e7890560
2020-12-03 16:20:55.686 Python[17698:7853902] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7ff7e7890560'
*** First throw call stack:
(
  0 CoreFoundation 0x00007fff2eafda7d __exceptionPreprocess + 256
  1 libobjc.A.dylib 0x00007fff591cea17 objc_exception_throw + 48
  2 CoreFoundation 0x00007fff2eb77886 -[NSObject(NSObject) __retain_OA] + 0
  3 CoreFoundation 0x00007fff2ea9f8ef ___forwarding___ + 1485
  4 CoreFoundation 0x00007fff2ea9f298 _CF_forwarding_prep_0 + 120
  5 libtk8.6.dylib 0x0000000102bf41f2 TkpInit + 408
  6 libtk8.6.dylib 0x0000000102b63aac Initialize + 2454
  7 _tkinter.cpython-39-darwin.so 0x00000001029b7704 Tcl_AppInit + 84
  8 _tkinter.cpython-39-darwin.so 0x00000001029b7022 _tkinter_create + 1362
  9 Python 0x00000001020d672a cfunction_vectorcall_FASTCALL + 218
  10 Python 0x000000010216e86c call_function + 732
  11 Python 0x000000010216bd93 _PyEval_EvalFrameDefault + 25219
  12 Python 0x000000010216f563 _PyEval_EvalCode + 2611
  13 Python 0x00000001020989b1 _PyFunction_Vectorcall + 289
  14 Python 0x00000001020980b5 _PyObject_FastCallDictTstate + 293
  15 Python 0x0000000102098c98 _PyObject_Call_Prepend + 152
  16 Python 0x00000001020f21e5 slot_tp_init + 165
  17 Python 0x00000001020ed699 type_call + 345
  18 Python 0x000000010209825d _PyObject_MakeTpCall + 365
  19 Python 0x000000010216e8fc call_function + 876
  20 Python 0x000000010216be2b _PyEval_EvalFrameDefault + 25371
  21 Python 0x0000000102098a28 function_code_fastcall + 104
  22 Python 0x000000010216e86c call_function + 732
  23 Python 0x000000010216bd93 _PyEval_EvalFrameDefault + 25219
  24 Python 0x000000010216f563 _PyEval_EvalCode + 2611
  25 Python 0x00000001020989b1 _PyFunction_Vectorcall + 289
  26 Python 0x000000010216e86c call_function + 732
  27 Python 0x000000010216bd93 _PyEval_EvalFrameDefault + 25219
  28 Python 0x000000010216f563 _PyEval_EvalCode + 2611
  29 Python 0x00000001020989b1 _PyFunction_Vectorcall + 289
  30 Python 0x00000001020980b5 _PyObject_FastCallDictTstate + 293
  31 Python 0x0000000102098c98 _PyObject_Call_Prepend + 152
  32 Python 0x00000001020f21e5 slot_tp_init + 165
  33 Python 0x00000001020ed699 type_call + 345
  34 Python 0x000000010209825d _PyObject_MakeTpCall + 365
  35 Python 0x000000010216e8fc call_function + 876
  36 Python 0x000000010216bd93 _PyEval_EvalFrameDefault + 25219
  37 Python 0x0000000102098a28 function_code_fastcall + 104
  38 Python 0x000000010216e86c call_function + 732
  39 Python 0x000000010216be2b _PyEval_EvalFrameDefault + 25371
  40 Python 0x000000010216f563 _PyEval_EvalCode + 2611
  41 Python 0x00000001020989b1 _PyFunction_Vectorcall + 289
  42 Python 0x000000010216e86c call_function + 732
  43 Python 0x000000010216bd72 _PyEval_EvalFrameDefault + 25186
  44 Python 0x000000010216f563 _PyEval_EvalCode + 2611
  45 Python 0x00000001020989b1 _PyFunction_Vectorcall + 289
  46 Python 0x000000010209aa6a method_vectorcall + 202
  47 Python 0x000000010216e86c call_function + 732
  48 Python 0x000000010216bec1 _PyEval_EvalFrameDefault + 25521
  49 Python 0x000000010216f563 _PyEval_EvalCode + 2611
  50 Python 0x00000001020989b1 _PyFunction_Vectorcall + 289
  51 Python 0x000000010209aa6a method_vectorcall + 202
  52 Python 0x000000010216e86c call_function + 732
  53 Python 0x000000010216bec1 _PyEval_EvalFrameDefault + 25521
  54 Python 0x0000000102098a28 function_code_fastcall + 104
  55 Python 0x000000010216e86c call_function + 732
  56 Python 0x000000010216bd72 _PyEval_EvalFrameDefault + 25186
  57 Python 0x000000010216f563 _PyEval_EvalCode + 2611
  58 Python 0x00000001020989b1 _PyFunction_Vectorcall + 289
  59 Python 0x000000010209802f _PyObject_FastCallDictTstate + 159
  60 Python 0x0000000102098c98 _PyObject_Call_Prepend + 152
  61 Python 0x00000001020f21e5 slot_tp_init + 165
  62 Python 0x00000001020ed699 type_call + 345
  63 Python 0x000000010209825d _PyObject_MakeTpCall + 365
  64 Python 0x000000010216e8fc call_function + 876
  65 Python 0x000000010216bec1 _PyEval_EvalFrameDefault + 25521
  66 Python 0x000000010216f563 _PyEval_EvalCode + 2611
  67 Python 0x00000001021659eb PyEval_EvalCode + 139
  68 Python 0x00000001021b8352 PyRun_FileExFlags + 434
  69 Python 0x00000001021b792f PyRun_SimpleFileExFlags + 831
  70 Python 0x00000001021d47e9 Py_RunMain + 1801
  71 Python 0x00000001021d4c7f pymain_main + 223
  72 Python 0x00000001021d4e7b Py_BytesMain + 43
  73 libdyld.dylib 0x00007fff5a99c3d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

Le 30 nov. 2020 à 15:31, Anthony Billings <ABillings@axustech.com> a écrit :

When I want to embed an MPL plot inside of a tkinter window I use PySimpleGUI. It’s a quick and easy module that makes working with tkinter much more manageable. I’ve found an example that uses PySimpleGui and embeds a simple MPL plot inside. I hope this helps. Happy coding!

#!/usr/bin/env python
import PySimpleGUI as sg
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
"""
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
Paste your Pyplot code into the section marked below.
Do all of your plotting as you normally would, but do NOT call plt.show().
Stop just short of calling plt.show() and let the GUI do the rest.
The remainder of the program will convert your plot and display it in the GUI.
If you want to change the GUI, make changes to the GUI portion marked below.
"""

import matplotlib.pyplot as plt
import numpy as np

def abline(slope, intercept):
    """Plot a line from slope and intercept"""
    axes = plt.gca()
    x_vals = np.array(axes.get_xlim())
    y_vals = intercept + slope * x_vals
    plt.plot(x_vals, y_vals, '--')

# ------------------------------- PASTE YOUR MATPLOTLIB CODE HERE -------------------------------

abline(-0.3, 1)
plt.ylim(0.5, 1.5)
plt.xlim(0,1)

# ------------------------------- END OF YOUR MATPLOTLIB CODE -------------------------------

# ------------------------------- Beginning of Matplotlib helper code -----------------------

def draw_figure(canvas, figure, loc=(0, 0)):
    figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
    figure_canvas_agg.draw()
    figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
    return figure_canvas_agg

# ------------------------------- Beginning of GUI CODE -------------------------------

fig = plt.gcf() # if using Pyplot then get the figure from the plot
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds

# define the window layout
layout = [[sg.Text('Plot test', font='Any 18')],
          [sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],
          [sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]]

# create the form and show it without the plot
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI',
    layout, force_toplevel=True, finalize=True)

# add the plot to the window
fig_photo = draw_figure(window['-CANVAS-'].TKCanvas, fig)

# show it all again and get buttons
event, values = window.read()

window.close()

From: Matplotlib-users <matplotlib-users-bounces+abillings=axustech.com@python.org> On Behalf Of Vincent Douce Mathoscope
Sent: Monday, November 30, 2020 5:53 AM
To: matplotlib-users@python.org
Subject: [Matplotlib-users] tk and matplotlib

hi
i am creating a matplotlib draw (musical sales on a guitar) in function of some parameters (tonic note, type of gamme or chord...)
i am now creating tk radio buttons so that the user can choose the parameters
i can get these parameters and create the matplotlib picture with them
but the matplotlib will open a different window
would it be possible to have a same window, divided as a table, with at the lift the radio buttons, and at the right the matplotlib result ?
----
https://lemathoscope.com/ <https://linkprotect.cudasvc.com/url?a=https%3A%2F%2Flemathoscope.com%2F&c=E,1,6vcd1-3cyy9rlYSvGPYjXZjP8pMdDLA0ZtZConnBK6PmFwpI6mZPG-h5-3MDOJ5IiIUqm7msJCw9kYECyLC9LoZVreR0FdEmkAWQ24yv8JApkqfCnPs,&typo=1&gt;
cours de maths postbac par visio

----

cours de maths postbac par visio