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

Tkinter Python Discussion :

Changement d'intitulé : tkinter matplotlib mise à jour graphe [Python 3.X]


Sujet :

Tkinter Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut Changement d'intitulé : tkinter matplotlib mise à jour graphe
    [EDIT] Changement d'intitulé.



    Bonsoir,
    J'ai un petit souci avec le programme suivant.
    En effet, je ne m'attends pas à ce que les fonctions "plot_carre" et "plot_triangle" se lancent à l'ouverture.
    J'aurais souhaité que cela se fasse en cliquant sur les boutons correspondant. Que fais-je mal ?
    Merci à vous.

    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
    import numpy as np
    import tkinter as Tk
    import matplotlib
    matplotlib.use('TkAgg')
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    from matplotlib.figure import Figure
     
    Umax=1
    omega=1
    nb_val=1000
    t=np.linspace(0,4*np.pi/omega,nb_val)
     
    def carre(nb_harm):
        tmp_carre=np.zeros(nb_val)
        for n in range(0,nb_harm):
            tmp_carre=tmp_carre+np.sin((2*n+1)*omega*t)/(2 *n +1)
        scale_carre=[4*Umax/np.pi]*nb_val
        tmp_carre=scale_carre*tmp_carre
        return tmp_carre
     
    def triangle(nb_harm):
        tmp_triangle=np.zeros(nb_val)
        for n in range(0,nb_harm):
            tmp_triangle=tmp_triangle+np.cos((2*n+1)*omega*t)/((2 *n +1)*(2*n+1))
        scale_triangle=[4*Umax/np.pi]*nb_val
        tmp_triangle=scale_triangle*tmp_triangle
        return tmp_triangle
     
    root = Tk.Tk()
    root.wm_title("Recomposition de Fourier")
     
    f = Figure(figsize=(8,4), dpi=100)# a tk.DrawingArea
    a = f.add_subplot(111)
     
    def plot_carre(nb_harm):
        a.plot(carre(nb_harm))
    def plot_triangle(nb_harm):
        a.plot(triangle(nb_harm))
    def _quit():
        root.quit()
        root.destroy()
     
    canvas = FigureCanvasTkAgg(f, master=root)
    canvas.show()
    canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
     
    toolbar = NavigationToolbar2TkAgg(canvas, root)
    toolbar.update()
    canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
     
    n=2
    button=Tk.Button(master=root,text='Carré',command=plot_carre(n))
    button.pack(side=Tk.LEFT)
    button=Tk.Button(master=root,text='Triangle',command=plot_triangle(n))
    button.pack(side=Tk.LEFT)
    button=Tk.Button(master=root, text='Quit',command=_quit)
    button.pack(side=Tk.RIGHT)
     
    Tk.mainloop()

  2. #2
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    J'ai ajouté un lambda: après command= mais cela ne fonctionne toujours pas comme je le souhaite...

  3. #3
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    J'avais un conflit de variables (n) mais cela ne semble pas être la raison de mon souci :
    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
    import numpy as np
    import tkinter as Tk
    import matplotlib
    matplotlib.use('TkAgg')
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    from matplotlib.figure import Figure
     
    Umax=1
    omega=1
    nb_val=1000
    t=np.linspace(0,4*np.pi/omega,nb_val)
     
    def carre(nb_harm):
        tmp_carre=np.zeros(nb_val)
        for n in range(0,nb_harm):
            tmp_carre=tmp_carre+np.sin((2*n+1)*omega*t)/(2 *n +1)
        scale_carre=[4*Umax/np.pi]*nb_val
        tmp_carre=scale_carre*tmp_carre
        return tmp_carre
     
    def triangle(nb_harm):
        tmp_triangle=np.zeros(nb_val)
        for n in range(0,nb_harm):
            tmp_triangle=tmp_triangle+np.cos((2*n+1)*omega*t)/((2 *n +1)*(2*n+1))
        scale_triangle=[4*Umax/np.pi]*nb_val
        tmp_triangle=scale_triangle*tmp_triangle
        return tmp_triangle
     
    root = Tk.Tk()
    root.wm_title("Recomposition de Fourier")
     
    f = Figure(figsize=(8,4), dpi=100)# a tk.DrawingArea
    a = f.add_subplot(111)
     
    def plot_carre(nb_harm):
        a.plot(carre(nb_harm))
    def plot_triangle(nb_harm):
        a.plot(triangle(nb_harm))
    def quitter():
        root.quit()
        root.destroy()
     
    canvas = FigureCanvasTkAgg(f, master=root)
    canvas.show()
    canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
     
    toolbar = NavigationToolbar2TkAgg(canvas, root)
    toolbar.update()
    canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
     
    harm=2
    button=Tk.Button(master=root,text='Carré',command=lambda: plot_carre(harm))
    button.pack(side=Tk.LEFT)
    button=Tk.Button(master=root,text='Triangle',command=lambda: plot_triangle(harm))
    button.pack(side=Tk.LEFT)
    button=Tk.Button(master=root, text='Quit',command=quitter)
    button.pack(side=Tk.RIGHT)
     
    Tk.mainloop()

  4. #4
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    Bon, je suis parti sur une autre mise en place, je reviendrai présenter mes nouveaux soucis.
    A très bientôt...

  5. #5
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 741
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 741
    Par défaut
    Salut,
    Citation Envoyé par marco056 Voir le message
    J'ai ajouté un lambda: après command= mais cela ne fonctionne toujours pas comme je le souhaite...
    Ce que vous souhaitez n'est pas si clair... Mais si vous lisez la documentation sur les animations, lorsque le contenu du Canvas matplotlib est mis à jour, il faut un canvas.draw() pour qu'il s'affiche i.e. écrire un truc comme:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    def plot_carre(nb_harm):
        a.plot(carre(nb_harm))
        canvas.draw()
     
    def plot_triangle(nb_harm):
        a.plot(triangle(nb_harm))
        canvas.draw()
    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  6. #6
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    [EDIT] Changement d'intitulé.


    Merci wiztricks,
    Comme je voulais gérer cela avec une échelle, je l'ai ajoutée.
    Mon problème est maintenant déplacé et j'ai beau chercher, je ne vois pas ce qui cloche. J'ai écumé les forums, y compris anglais, sans succès.
    J'ai cherché également dans la FAQ.
    La réponse est sans doute très simple, comme d'habitude.
    Voici mon nouveau programme :
    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
    import numpy as np
    import tkinter as Tk
    import matplotlib
    matplotlib.use('TkAgg')
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    from matplotlib.figure import Figure
     
    Umax=1
    omega=1
    nb_val=500
    t=np.linspace(0,4*np.pi/omega,nb_val)
     
    def carre(nb_harm):
        nb_harm=int(nb_harm)
        tmp_carre=np.zeros(nb_val)
        for n in range(0,nb_harm):
            tmp_carre=tmp_carre+np.sin((2*n+1)*omega*t)/(2 *n +1)
        scale_carre=[4*Umax/np.pi]*nb_val
        tmp_carre=scale_carre*tmp_carre
        return tmp_carre
     
    root = Tk.Tk()
    root.wm_title("Recomposition de Fourier")
     
    def initialisation():
        global f, a
        f = Figure(figsize=(6,3), dpi=100)
        f.clf()
        a = f.add_subplot(111)
     
     
    def raffraichissement():
        canvas = FigureCanvasTkAgg(f, master=root)
        canvas.draw()
        canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
        toolbar = NavigationToolbar2TkAgg(canvas, root)
        toolbar.update()
        canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
     
    def plot_carre(nb_harm):
        initialisation()
        a.plot(carre(nb_harm))
        raffraichissement()
     
    def quitter():
        root.quit()
        root.destroy()
     
    harmo=Tk.Label(root, text="Harmoniques")
    harmo.pack(side=Tk.LEFT)
    val_harm = Tk.IntVar() # On definit val_harm
    val_harm.set(1)        # On donne la valeur que prendra le curseur au départ
    # Création d'un widget Curseur
    echelle_harm = Tk.Scale(root,length=200, orient=Tk.HORIZONTAL, troughcolor ='LightYellow2', \
    sliderlength =20, showvalue=1,from_=1,to=5, resolution=1, tickinterval=2, \
    command=lambda val_harm=0: plot_carre(val_harm)) # width=10
    echelle_harm.set(0),
    echelle_harm.pack(side=Tk.LEFT)
     
    button=Tk.Button(master=root, text='Quit',command=quitter)
    button.pack(side=Tk.RIGHT)
     
    Tk.mainloop()
    Lorsque je fais glisser le curseur, un autre graphe se place dans le même canvas alors que je souhaiterais qu'il soit écrasé par la nouvelle valeur du curseur.

  7. #7
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 741
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 741
    Par défaut
    Citation Envoyé par marco056 Voir le message
    Lorsque je fais glisser le curseur, un autre graphe se place dans le même canvas alors que je souhaiterais qu'il soit écrasé par la nouvelle valeur du curseur.
    Si c'est a.plot(...) qui fabrique le tracé, il faut chercher comment le remettre à zéro avant d'y ajouter un autre tracé. Pas la peine de trop se compliquer la vie:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    def plot_carre(nb_harm):
        a.cla()
        a.plot(carre(nb_harm))
        canvas.draw()
    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  8. #8
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    Merci wiztricks.
    J'ai bien avancé maintenant.
    J'ai une question qui est liée au même problème, je pense :
    pourquoi ma fonction de remise à zéro (raz) ne fonctionne pas ?
    J'y ai mis un cla() et un canvas.draw(), sans succès.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Quelle est cette commande qui plante linux?
    Par 123quatre dans le forum Shell et commandes GNU
    Réponses: 11
    Dernier message: 18/02/2006, 13h48
  2. [MS-DOS] Une commande qui remette l’heure du PC à jour via I
    Par Furius dans le forum Autres Logiciels
    Réponses: 14
    Dernier message: 30/12/2005, 23h42
  3. la commande qui permet d'afficher la description d'une table
    Par dor_boucle dans le forum MS SQL Server
    Réponses: 4
    Dernier message: 14/12/2005, 12h54
  4. Réponses: 11
    Dernier message: 30/08/2005, 10h50

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