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 :

scrollbar attaché à un canvas [Python 2.X]


Sujet :

Tkinter Python

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    23
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 23
    Points : 15
    Points
    15
    Par défaut scrollbar attaché à un canvas
    Bonjour,


    J'ai du mal à associer un scrollbar à un canvas que j'ai créé.
    Il permettrait de visualiser la totalité des listes placées dans un fichier matrice.txt.

    Voici mon programme (tourne sous la version 2.7 ) :

    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
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    #--- Programme qui change une coordonnée (x,y ou z) de toute une matrice ---#
    from Tkinter import *
    from math import *
     
    def ok():
        with open("matrice.txt") as inf:
            lines = inf.readlines()
     
        roundval = int(E4.get())
        entries = [float(E1.get()), float(E2.get()), float(E3.get())]
        news = ""
        for line in lines:
            vals = line.strip().replace('[','').replace(']','').split(',')
            new = []
            for i, j in zip(vals, entries):
                s = float(i) + j
                if roundval:
                    s = round(s, roundval)
     
                new.append(s)
     
            news = news + str(new) + '\n'
     
        with open('matrice2.txt','w') as outf:
            outf.write(news)
     
        L4.config(text = news)
        L.config(text = "Finished !")
     
    fe=Tk()
    fe.title("coordinate change")
    fe.geometry('1250x1000')
    L=Label(fe,text='This program allows to increase or decrease one of the coordinates (X, Y or Z) of a [X,Y,Z] matrice such as: \n [15,1.454,0.1]\n[12.1,5.2,78]\n[0.3,0,45]\n(...)\n\n Please copy your matrice in a file whose the name is matrice.txt.\nPut the file in the same directory than this program. The new matrice will be in the file named matrice2.txt.',font=('Arial',17,'bold'))
    L.place(x=20,y=50)
    LX=Label(fe,text='X',font=('Helvetica',15,'underline'))
    LX.place(x=200,y=450)
    LY=Label(fe,text='Y',font=('Helvetica',15,'underline'))
    LY.place(x=200,y=550)
    LZ=Label(fe,text='Z',font=('Helvetica',15,'underline'))
    LZ.place(x=200,y=650)
     
    L1=Label(fe,text='Give the value(s)',font=('Helvetica',15,'underline'))
    L1.place(x=400,y=410)
    E1=Entry(fe)
    E1.place(x=400,y=450)
    E1.insert(0,0)
    E2=Entry(fe)
    E2.place(x=400,y=550)
    E2.insert(0,0)
    E3=Entry(fe)
    E3.place(x=400,y=650)
    E3.insert(0,0)
     
    L2=Label(fe,text='Contenu de matrice.txt :',font=('Helvetica',15,'underline'))
    L2.place(x=1000,y=320)
    with open("matrice.txt") as inf:
        lines = inf.readlines()
     
    #Canvas texte contenu matrice.txt :
    w = Canvas(fe, width=500, height=250)
    w.place(x=1000,y=350)
    text=w.create_text(10,0,anchor='nw',text=lines)
     
    #scrollbar :
    hbar=Scrollbar(fe,orient=VERTICAL)
    hbar.pack(side=RIGHT,fill=X)
    hbar.config(command=w.xview)
    w.config(xscrollcommand=hbar.set)
     
    L2=Label(fe,text='Contenu de matrice2.txt :',font=('Helvetica',15,'underline'))
    L2.place(x=1000,y=620)
    L4=Label(fe,text='')
    L4.place(x=1000,y=650)
     
    B=Button(fe,text='OK',width=25,command=ok)
    B.place(x=600,y=550)
     
    L5=Label(fe,text='Voulez-vous arrondir ? Si oui, mettez le nombre de chiffres après la virgule que vous souhaitez faire apparaître :')
    L5.place(x=0,y=320)
    E4=Entry(fe)
    E4.place(x=600,y=320)
     
    fe.mainloop()

  2. #2
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par adrbessy Voir le message
    Bonjour,


    J'ai du mal à associer un scrollbar à un canvas que j'ai créé.
    Il permettrait de visualiser la totalité des listes placées dans un fichier matrice.txt.
    Bonjour,

    Voici un code exemple :

    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
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
     
    from Tkinter import *
     
    # Tcl/Tk interpreter + main window (implicit)
    root = Tk()
     
    # canvas + scrollbars frame container
    cframe = Frame(root)
    cframe.rowconfigure(0, weight=1)
    cframe.columnconfigure(0, weight=1)
    cframe.pack(expand=1, fill=BOTH, padx=5, pady=5)
     
    # canvas
    canvas = Canvas(cframe, bg="white")
    canvas.create_text(100, 100, text="Hello, world!", font="sans 16 bold", fill="blue")
    canvas.grid(row=0, column=0, sticky=NW+SE)
     
    # horizontal scrollbar
    hbar = Scrollbar(cframe, orient=HORIZONTAL)
    hbar.grid(row=1, column=0, sticky=NW+SE)
     
    # vertical scrollbar
    vbar = Scrollbar(cframe, orient=VERTICAL)
    vbar.grid(row=0, column=1, sticky=NW+SE)
     
    # connecting people...
    canvas.configure(
        xscrollcommand=hbar.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    hbar.configure(command=canvas.xview)
    vbar.configure(command=canvas.yview)
     
    # launching events main loop
    root.mainloop()
    Faites attention à vos noms de variables e.g. hbar (horizontal bar) pour un scrollbar vertical, ça le fait moyen, quand même.

    @+.

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    23
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 23
    Points : 15
    Points
    15
    Par défaut
    Savez-vous s'il est possible de se passer d'un Frame() pour créer un scrollbar?
    Ou même s'il est possible de créer un scrollbar sur un Label() sans être obligé de créer un Canvas() ?

    Merci.

  4. #4
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par adrbessy Voir le message
    Savez-vous s'il est possible de se passer d'un Frame() pour créer un scrollbar?
    Oui, c'est tout à fait possible, c'est juste beaucoup moins pratique, voilà tout.

    Exemple - si vous utilisez .grid() partout pour un même widget conteneur :

    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
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
     
    from Tkinter import *
     
    # Tcl/Tk interpreter + main window (implicit)
    root = Tk()
    root.rowconfigure(0, weight=1)
    root.columnconfigure(0, weight=1)
     
    # canvas
    canvas = Canvas(root, bg="white")
    canvas.create_text(100, 100, text="Hello, world!", font="sans 16 bold", fill="blue")
    canvas.grid(row=0, column=0, sticky=NW+SE)
     
    # horizontal scrollbar
    hbar = Scrollbar(root, orient=HORIZONTAL)
    hbar.grid(row=1, column=0, sticky=NW+SE)
     
    # vertical scrollbar
    vbar = Scrollbar(root, orient=VERTICAL)
    vbar.grid(row=0, column=1, sticky=NW+SE)
     
    # connecting people...
    canvas.configure(
        xscrollcommand=hbar.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    hbar.configure(command=canvas.xview)
    vbar.configure(command=canvas.yview)
     
    # launching events main loop
    root.mainloop()
    Attention à ne pas mélanger des .grid() avec des .pack() et/ou des .place() pour un même widget conteneur : le gestionnaire d'affichage tkinter ne saura pas à qui donner la priorité et votre programme va faire un "freeze" étrange et déroutant.

    Lisez ceci, c'est important :

    http://effbot.org/tkinterbook/pack.htm
    http://effbot.org/tkinterbook/grid.htm
    http://effbot.org/tkinterbook/place.htm

    L'intérêt de placer le canvas et les scrollbars dans une frame est de considérer le bloc canvas+scrollbars comme un seul composant solidaire, composant que l'on enveloppe d'une frame pour le rendre justement groupé, solidaire => cela permet de transporter ensuite le composant entier un peu partout dans le code sans que cela ne pose de problème particulier d'adaptation. C'est une technique répandue en programmation de GUI (raisonnement par composants, par briques de Lego(tm) autonomes).

    Exemple avec .pack() - pareil, si vous utilisez partout .pack() pour un même conteneur :

    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
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
     
    from Tkinter import *
     
    # Tcl/Tk interpreter + main window (implicit)
    root = Tk()
     
    # canvas
    canvas = Canvas(root, bg="white")
    canvas.create_text(100, 100, text="Hello, world!", font="sans 16 bold", fill="blue")
     
    # horizontal scrollbar
    hbar = Scrollbar(root, orient=HORIZONTAL)
     
    # vertical scrollbar
    vbar = Scrollbar(root, orient=VERTICAL)
     
    # layout inits
    # ici, l'ordre des déclarations compte (!)
    vbar.pack(side=RIGHT, expand=1, fill=Y)
    canvas.pack(side=TOP, expand=1, fill=BOTH)
    hbar.pack(expand=1, fill=X)
     
    # connecting people...
    canvas.configure(
        xscrollcommand=hbar.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    hbar.configure(command=canvas.xview)
    vbar.configure(command=canvas.yview)
     
    # launching events main loop
    root.mainloop()
    Pas très pro (la vertical scrollbar déborde en bas, le redimensionnement de la fenêtre donne des résultats médiocres et en plus, on doit faire très attention à l'ordre dans lequel on déclare les .pack()).

    Pour placer correctement un canvas et ses scrollbars, préférez toujours .grid(), c'est plus pro comme présentation et préférez - évidemment - le raisonnement par composants autonomes, en plaçant votre canvas et ses scrollbars dans une frame enveloppe.

    Ou même s'il est possible de créer un scrollbar sur un Label() sans être obligé de créer un Canvas() ?

    Merci.
    Vous ne pouvez connecter un objet scrollbar() qu'aux widgets tkinter qui implémentent au moins l'une ou l'autre des options xscrollcommand et yscrollcommand dans leur méthode .configure(), ce qui réduit le nombre de widgets tkinter à la liste suivante :

    Widgets connectables à une scrollbar : Canvas(), Entry(), Spinbox() et Text().

    Et uniquement ces widgets-là (pour tkinter, du moins).

    @+.

  5. #5
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    23
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 23
    Points : 15
    Points
    15
    Par défaut
    Merci pour vos explications, je vais étudier tout cela.

  6. #6
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par adrbessy Voir le message
    Merci pour vos explications, je vais étudier tout cela.
    Merci de cliquer lorsque cette discussion sera close.

    @+.

  7. #7
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    23
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 23
    Points : 15
    Points
    15
    Par défaut
    Bonjour,

    Est-il possible de lier un scrollbar à deux canvas (afin de pouvoir toujours aligner les listes qui correspondent ) ?

    Voici le code du programme en question :

    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
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
     
    from Tkinter import *
    from math import *
     
    def ok():
        with open("matrice.txt") as inf:
            lines = inf.readlines()
     
        roundval = int(E4.get())
        entries = [float(E1.get()), float(E2.get()), float(E3.get())]
        news = ""
        for line in lines:
            vals = line.strip().replace('[','').replace(']','').split(',')
            new = []
            for i, j in zip(vals, entries):
                s = float(i) + j
                if roundval != '':
                    s = round(s, roundval)
     
                new.append(s)
     
            news = news + str(new) + '\n'
     
        with open('matrice2.txt','w') as outf:
            outf.write(news)
     
        canvas2.itemconfig(c2,text=news)
        L.config(text="Finished !")
     
    # Création de fenêtre
    root = Tk()
    root.rowconfigure(0, weight=1)
    root.columnconfigure(0, weight=1)
    root.title("coordinate change")
     
    # Label, Entry
    L=Label(root,text='This program allows to increase or decrease one of the coordinates (X, Y or Z) of a [X,Y,Z] matrice such as: \n [15,1.454,0.1]\n[12.1,5.2,78]\n[0.3,0,45]\n(...)\n\n Please copy your matrice in a file whose the name is matrice.txt.\nPut the file in the same directory than this program. The new matrice will be in the file named matrice2.txt.',font=('Arial',17,'bold'))
    L.grid()
     
    L1=Label(root,text='Give the value(s)',font=('Helvetica',15,'underline'))
    L1.grid(column=1)
     
    LX=Label(root,text='X',font=('Helvetica',15,'underline'))
    LX.grid(row=2)
     
    E1=Entry(root)
    E1.grid(row=2,column=1)
    E1.insert(0,0)
     
    LY=Label(root,text='Y',font=('Helvetica',15,'underline'))
    LY.grid()
     
    E2=Entry(root)
    E2.grid(row=3,column=1)
    E2.insert(0,0)
     
    LZ=Label(root,text='Z',font=('Helvetica',15,'underline'))
    LZ.grid()
     
    E3=Entry(root)
    E3.grid(row=4,column=1)
    E3.insert(0,0)
     
    L2=Label(root,text='Contenu de matrice.txt :',font=('Helvetica',15,'underline'))
    L2.grid(row=6,column=0,columnspan=2)
     
    with open("matrice.txt") as inf:
        lines = inf.readlines()
        print lines
     
    canvas = Canvas(root, bg="white")
    canvas.grid(row=7, column=0, sticky=NW+SE)
    list=canvas.create_text(0,0, text=lines, font="sans 16 bold", fill="blue",anchor='nw')
     
     
    # horizontal scrollbar
    hbar = Scrollbar(root, orient=HORIZONTAL)
    hbar.grid(row=8, column=0, sticky=NW+SE)
     
    # vertical scrollbar
    vbar = Scrollbar(root, orient=VERTICAL)
    vbar.grid(row=7, column=1, sticky=W+N+S)
     
    # connecting people...
    canvas.configure(
        xscrollcommand=hbar.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    hbar.configure(command=canvas.xview)
    vbar.configure(command=canvas.yview)
     
    L2=Label(root,text='Contenu de matrice2.txt :',font=('Helvetica',15,'underline'))
    L2.grid(row=6,column=2,columnspan=2)
     
    canvas2 = Canvas(root, bg="white")
    c2=canvas2.create_text(0,0, text='', font="sans 16 bold", fill="blue",anchor='nw')
    canvas2.grid(row=7, column=2, sticky=NW+SE)
     
    # horizontal scrollbar
    hbar = Scrollbar(root, orient=HORIZONTAL)
    hbar.grid(row=8, column=2, sticky=NW+SE)
     
    # vertical scrollbar
    vbar = Scrollbar(root, orient=VERTICAL)
    vbar.grid(row=7, column=3, sticky=W+N+S)
     
    # connecting people...
    canvas2.configure(
        xscrollcommand=hbar.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    hbar.configure(command=canvas2.xview)
    vbar.configure(command=canvas2.yview)
     
    B=Button(root,text='OK',width=25,command=ok)
    B.grid()
     
    L5=Label(root,text='Voulez-vous arrondir ? Si oui, mettez le nombre de chiffres après la virgule que vous souhaitez faire apparaître :')
    L5.grid(row=5)
     
    E4=Entry(root)
    E4.grid(row=5,column=1)
     
    root.mainloop()
    Merci.

  8. #8
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par adrbessy Voir le message
    Bonjour,

    Est-il possible de lier un scrollbar à deux canvas (afin de pouvoir toujours aligner les listes qui correspondent ) ?

    Merci.
    Oui, il suffit d'appeler une fonction intermédiaire qui va dispatcher les commandes entre plusieurs canevas cible.

    Exemple de code générique :

    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
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
     
    from Tkinter import *
     
    def dispatch_yview (*args):
        """
            cette fonction dispatche les commandes de la scrollbar
            VERTICALE entre les deux canevas;
        """
        canvas1.yview(*args)
        canvas2.yview(*args)
    # end def
     
    # Tcl/Tk interpreter + main window (implicit)
    root = Tk()
    root.rowconfigure(0, weight=1)
    root.columnconfigure(0, weight=1)
    root.columnconfigure(1, weight=1)
     
    # canvas 1
    canvas1 = Canvas(root, bg="white")
    canvas1.create_text(100, 100, text="Hello, world!", font="sans 16 bold", fill="blue")
    canvas1.grid(row=0, column=0, sticky=NW+SE)
     
    # canvas 2
    canvas2 = Canvas(root, bg="sky blue")
    canvas2.create_text(100, 100, text="Hello, world!", font="sans 16 bold", fill="red")
    canvas2.grid(row=0, column=1, sticky=NW+SE)
     
    # horizontal scrollbar canvas 1
    hbar1 = Scrollbar(root, orient=HORIZONTAL)
    hbar1.grid(row=1, column=0, sticky=EW)
     
    # horizontal scrollbar canvas 2
    hbar2 = Scrollbar(root, orient=HORIZONTAL)
    hbar2.grid(row=1, column=1, sticky=EW)
     
    # vertical scrollbar
    vbar = Scrollbar(root, orient=VERTICAL)
    vbar.grid(row=0, column=2, sticky=NS)
     
    # connecting people...
    canvas1.configure(
        xscrollcommand=hbar1.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    canvas2.configure(
        xscrollcommand=hbar2.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    hbar1.configure(command=canvas1.xview)
    hbar2.configure(command=canvas2.xview)
    # special dispatch between 2 canvases
    vbar.configure(command=dispatch_yview)
     
    # launching events main loop
    root.mainloop()
    @+.

  9. #9
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    23
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 23
    Points : 15
    Points
    15
    Par défaut
    Ok c'est bon merci pour tout.

    Voici mon 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
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
     
    from Tkinter import *
    from math import *
     
    def dispatch_yview (*args):
        """
            cette fonction dispatche les commandes de la scrollbar
            VERTICALE entre les deux canevas;
        """
        canvas.yview(*args)
        canvas2.yview(*args)
    # end def
     
    def ok():
        with open("matrice.txt") as inf:
            lines = inf.readlines()
     
        roundval = E4.get()
        entries = [float(E1.get()), float(E2.get()), float(E3.get())]
        news = ""
        for line in lines:
            vals = line.strip().replace('[','').replace(']','').split(',')
            new = []
            for i, j in zip(vals, entries):
                s = float(i) + j
                if roundval :
                    s = round(s, int(roundval))
     
                new.append(s)
     
            news = news + str(new) + '\n'
     
        with open('matrice2.txt','w') as outf:
            outf.write(news)
     
        canvas2.itemconfig(c2,text=news)
        L.config(text="Finished !")
     
    # Création de fenêtre
    root = Tk()
    root.rowconfigure(0, weight=1)
    root.columnconfigure(0, weight=1)
    root.title("coordinate change")
     
    # Label, Entry
    L=Label(root,text='This program allows to increase or decrease one of the coordinates (X, Y or Z) of a [X,Y,Z] matrice such as: \n [15,1.454,0.1]\n[12.1,5.2,78]\n[0.3,0,45]\n(...)\n\n Please copy your matrice in a file whose the name is matrice.txt.\nPut the file in the same directory than this program. The new matrice will be in the file named matrice2.txt.',font=('Arial',17,'bold'))
    L.grid()
     
    L1=Label(root,text='Give the value(s)',font=('Helvetica',15,'underline'))
    L1.grid(column=1)
     
    LX=Label(root,text='X',font=('Helvetica',15,'underline'))
    LX.grid(row=2)
     
    E1=Entry(root)
    E1.grid(row=2,column=1)
    E1.insert(0,0)
     
    LY=Label(root,text='Y',font=('Helvetica',15,'underline'))
    LY.grid()
     
    E2=Entry(root)
    E2.grid(row=3,column=1)
    E2.insert(0,0)
     
    LZ=Label(root,text='Z',font=('Helvetica',15,'underline'))
    LZ.grid()
     
    E3=Entry(root)
    E3.grid(row=4,column=1)
    E3.insert(0,0)
     
    L2=Label(root,text='Contenu de matrice.txt :',font=('Helvetica',15,'underline'))
    L2.grid(row=6,column=0,columnspan=2)
     
    with open("matrice.txt") as inf:
        lines = inf.readlines()
     
    canvas = Canvas(root, bg="white")
    canvas.grid(row=7, column=0, sticky=NW+SE)
    list=canvas.create_text(0,0, text=lines, font="sans 16 bold", fill="blue",anchor='nw')
     
     
    # horizontal scrollbar
    hbar = Scrollbar(root, orient=HORIZONTAL)
    hbar.grid(row=8, column=0, sticky=NW+SE)
    # vertical scrollbar pour les 2
    vbar = Scrollbar(root, orient=VERTICAL)
    vbar.grid(row=7, column=3, sticky=W+N+S)
    # connecting people...
    canvas.configure(
        xscrollcommand=hbar.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    hbar.configure(command=canvas.xview)
    vbar.configure(command=canvas.yview)
     
    L2=Label(root,text='Contenu de matrice2.txt :',font=('Helvetica',15,'underline'))
    L2.grid(row=6,column=2,columnspan=2)
     
    canvas2 = Canvas(root, bg="white")
    c2=canvas2.create_text(0,0, text='', font="sans 16 bold", fill="blue",anchor='nw')
    canvas2.grid(row=7, column=2, sticky=NW+SE)
     
    # horizontal scrollbar
    hbar = Scrollbar(root, orient=HORIZONTAL)
    hbar.grid(row=8, column=2, sticky=NW+SE)
     
    # connecting people...
    canvas2.configure(
        xscrollcommand=hbar.set,
        yscrollcommand=vbar.set,
        scrollregion=(0, 0, 800, 600),
    )
    hbar.configure(command=canvas2.xview)
    vbar.configure(command=canvas2.yview)
     
    # special dispatch between 2 canvases
    vbar.configure(command=dispatch_yview)
     
    B=Button(root,text='OK',width=25,command=ok)
    B.grid(row=4,column=2,columnspan=2)
     
    L5=Label(root,text='Voulez-vous arrondir ? Si oui, mettez le nombre de chiffres après la virgule que vous souhaitez faire apparaître :')
    L5.grid(row=5)
     
    E4=Entry(root)
    E4.grid(row=5,column=1)
     
    root.mainloop()

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

Discussions similaires

  1. ScrollBar Horizontale sur Canvas
    Par macben dans le forum Forms
    Réponses: 2
    Dernier message: 22/02/2012, 04h29
  2. Scrollbar dans un canvas
    Par nwktotof dans le forum Windows Presentation Foundation
    Réponses: 5
    Dernier message: 03/07/2009, 12h04
  3. Scrollbar associée à un canvas
    Par gletare dans le forum Tkinter
    Réponses: 2
    Dernier message: 03/10/2007, 15h16
  4. Probleme de canvas et scrollbar
    Par naoma dans le forum Tkinter
    Réponses: 1
    Dernier message: 26/11/2006, 05h31
  5. Problème ScrollBar et Canvas
    Par nkos dans le forum SWT/JFace
    Réponses: 4
    Dernier message: 14/06/2006, 16h48

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