IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

GUI Python Discussion :

problème de toolbar dans un graphique inséré dans un canvas de tkinter


Sujet :

GUI Python

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Enseignant
    Inscrit en
    Mai 2018
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Mai 2018
    Messages : 1
    Points : 1
    Points
    1
    Par défaut problème de toolbar dans un graphique inséré dans un canvas de tkinter
    Je suis débutant sans tkinter, j'utilise python 3 et je code dans l'environnement spyder/ Anaconda sous windows 10

    Je suis parti d'un exemple de code qui permet d'afficher un graphique avec sa toolbar ( boutons pour Z00M et autres) généré par matplotlib dans un canvas de tkinter. Pour l'exemple de graphique 2D que j'ai trouvé la toolbar fonctionne, mais pour le graphique 3D que j'ai utilisé , la toollbar apparait bien mais ses boutons ne fonctionnent pas!

    Je n'ai pas trouvé de réponse à ce problème dans la FAQ Python / tkinter

    Voici le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    import tkinter
    from matplotlib.figure import Figure
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
     
    from matplotlib.backends.backend_tkagg import (
        FigureCanvasTkAgg, NavigationToolbar2Tk)
    from matplotlib.backend_bases import key_press_handler
     
    import numpy as np
     
    #choix de la figure===================
     
    choix=1  #la toolbar fonctionne
    choix=2  # la toolbar ne fonctionne pas
     
    if choix==1:
        fig = Figure(figsize=(5, 4), dpi=100)
        t = np.arange(0, 3, .01)
        fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
     
    if choix==2: # la figure pour laquelle les bootons (zoom, ...)) fonctionnent 
     
        # Make data.
        X = np.arange(-5, 5, 0.25)
        Y = np.arange(-5, 5, 0.25)
        X, Y = np.meshgrid(X, Y)
        R = np.sqrt(X**2 + Y**2)
        Z = np.sin(R)
     
        #figure 3D
        fig = Figure(figsize=(5, 4), dpi=100)
        ax = fig.add_subplot(111, projection='3d')
        surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                               linewidth=0, antialiased=False)
     
        # Add a color bar which maps values to colors.
        fig.colorbar(surf, shrink=0.5, aspect=5)
     
    #===========================
     
     
    root = tkinter.Tk()
    root.wm_title("Embedding in Tk")
     
    canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
     
    toolbar = NavigationToolbar2Tk(canvas, root)
    toolbar.update()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
     
     
    def on_key_press(event):
        print("you pressed {}".format(event.key))
        key_press_handler(event, canvas, toolbar)
     
    canvas.mpl_connect("key_press_event", on_key_press)
     
    def _quit():
        root.quit()     # stops mainloop
        root.destroy()  # this is necessary on Windows to prevent
                        # Fatal Python Error: PyEval_RestoreThread: NULL tstate
     
    # pour quitter 
    button = tkinter.Button(master=root, text="Quit", command=_quit)
    button.pack(side=tkinter.BOTTOM)
     
    tkinter.mainloop()

    Si quelqu'un a une solution ...Merci

  2. #2
    Membre émérite Avatar de tsuji
    Inscrit en
    Octobre 2011
    Messages
    1 558
    Détails du profil
    Informations forums :
    Inscription : Octobre 2011
    Messages : 1 558
    Points : 2 736
    Points
    2 736
    Par défaut
    Ceci est un problème quelque peu mystérious : ce n'est pas pourquoi une graphique 3d ne marche pas, c'est pourquoi 2d marche apparemment. Et qu'il marche pour 2d, tant mieux: mais ce n'est pas la façon canonique.

    Le règle est plutôt qu'il faut que l'attribut canvas de Figure d'être matplotlib.backends.backend_tkagg.FigureCanvasTkAgg avant d'établir les données qui tracent la Figure. Comment assure ça ? Il faut établir le canvas pour la figure avant de définir les subplots et des plots de la figure.

    Voici comment c'est fait en pratique conservant les lignes existantes mais les remettrant en bon ordre.
    Code python3 : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    import tkinter
    from matplotlib.figure import Figure
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
     
    from matplotlib.backends.backend_tkagg import (
        FigureCanvasTkAgg, NavigationToolbar2Tk)
    from matplotlib.backend_bases import key_press_handler
     
    import numpy as np
     
    root = tkinter.Tk()
    root.wm_title("Embedding in Tk")
     
    fig = Figure(figsize=(5, 4), dpi=100)
    canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
     
    #choix de la figure===================
     
    choix=1  #la toolbar fonctionne
    choix=2  # la toolbar ne fonctionne pas
     
    if choix==1:
        #fig = Figure(figsize=(5, 4), dpi=100)
        t = np.arange(0, 3, .01)
        fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
     
    if choix==2: # la figure pour laquelle les bootons (zoom, ...)) fonctionnent 
     
        # Make data.
        X = np.arange(-5, 5, 0.25)
        Y = np.arange(-5, 5, 0.25)
        X, Y = np.meshgrid(X, Y)
        R = np.sqrt(X**2 + Y**2)
        Z = np.sin(R)
     
        #figure 3D
        #fig = Figure(figsize=(5, 4), dpi=100)
        ax = fig.add_subplot(111, projection='3d')
        surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                               linewidth=0, antialiased=False)
     
        # Add a color bar which maps values to colors.
        fig.colorbar(surf, shrink=0.5, aspect=5)
     
    #===========================
     
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
     
    toolbar = NavigationToolbar2Tk(canvas, root)
    toolbar.update()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
     
    def on_key_press(event):
        print("you pressed {}".format(event.key))
        key_press_handler(event, canvas, toolbar)
     
    canvas.mpl_connect("key_press_event", on_key_press)
     
    def _quit():
        root.quit()     # stops mainloop
        root.destroy()  # this is necessary on Windows to prevent
                        # Fatal Python Error: PyEval_RestoreThread: NULL tstate
     
    # pour quitter 
    button = tkinter.Button(master=root, text="Quit", command=_quit)
    button.pack(side=tkinter.BOTTOM)
     
    tkinter.mainloop()

Discussions similaires

  1. Problème d'affichage dans un canvas
    Par Bjorn28 dans le forum Tkinter
    Réponses: 5
    Dernier message: 17/11/2015, 08h54
  2. [Python 2.X] Ne pas modifier la dynamique de couleur dans un canvas de Tkinter
    Par Nicolas51 dans le forum Tkinter
    Réponses: 1
    Dernier message: 03/05/2015, 12h10
  3. C# \ WPF : Problème de ratio dans un canvas.
    Par Julien Board dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 17/12/2010, 21h14
  4. Problème avec toolbar dans IE
    Par manou90 dans le forum IE
    Réponses: 0
    Dernier message: 02/01/2008, 10h53
  5. Réponses: 7
    Dernier message: 25/06/2007, 22h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo