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 :

Scrollbare avec un widget Text


Sujet :

Tkinter Python

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2008
    Messages : 52
    Points : 29
    Points
    29
    Par défaut Scrollbare avec un widget Text
    hello
    Pour faire simple: je veux ajouter à droite du widjet text un scrollbar vertical qui fait défiler le text (comme votre navigateur internet )

    Seulement sa coince, voila 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
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
     
    #!/usr/bin/env python
    # -*- coding:Utf-8 -*-
    import sys
    import os
    from PIL import Image,ImageTk
    from Tkinter import *
    #Récupère les fichier et vérifie s'ils sont présent
    def startannex(send,my_file_name):
        #Ret = []
        Ret = os.popen(send).readlines()
        Ret = str(Ret)
        Ret = Ret.strip("[")
        Ret = Ret.strip("]")
        Ret = Ret.strip("'")
        Ret = Ret[:Ret.find('\\')]
        return Ret + "/" + my_file_name
     
     
    def teste_exist_fichier(my_name_file):
        #print os.path.abspath(__file__)
     
        if os.path.abspath(os.path.dirname(__file__) + "/" + my_name_file):
            return os.path.abspath(os.path.dirname(__file__) + "/" + \
            my_name_file)
        if os.path.isfile(startannex("pwd", my_name_file)):
            return startannex("pwd", my_name_file)
        S = sys.argv[0]
        F=S[0:len(sys.argv[0])-len(my_name_file)]
        if os.path.isfile(F+my_name_file):
            return F+my_name_file
        if os.path.isfile(os.path.split(__file__)[0] +"/"+my_name_file):
            return os.path.split(__file__)[0] +"/"+my_name_file
        if os.path.isfile(os.path.split(__file__)[0] +"/"+my_name_file):
            return os.path.split(__file__)[0] +"/"+my_name_file
        print Lfr[0] + my_name_file  
        return False
    fichierPngcadre=teste_exist_fichier("cadre.png")
    if fichierPngcadre == False: sys.exit()
     
     
     
    def read_my_file(path_my_file_read):
        file=open(path_my_file_read,'r')
        convercharset = file.read().decode('utf-8').\
        encode(sys.stdout.encoding,'replace')
        file.close()
        #N_lignes = len(lecture)
        return convercharset
     
    def defgeometry(self):
        resolution=(self.winfo_screenwidth()/2,self.winfo_screenheight()/2)
        appsize=(self.winfo_width()/4,self.winfo_height()/4)
        tmpw=((resolution[0] - appsize[0])+15)
        tmph=((resolution[1] - appsize[1])-60)
        self.geometry('+'+str(tmpw)+'+'+str(tmph))
     
    def startinfo():
        self =Tk()
        L=800
        H=600
        H_image=L/2
        L_image=H/2
     
        can = Canvas(self, width =L, height =H, bg ='white')
        mon_image=ImageTk.PhotoImage(file=fichierPngcadre)
        can.create_image(H_image,L_image,image=mon_image)
     
        try:
            readme=read_my_file("/home/user/Desktop/python/README.txt")
        except:
            print "Charset non suporter"
     
        text = Text(self, \
        background="white", \
        borderwidth=0, \
        relief=RAISED, \
        foreground = "blue", \
        selectforeground="blue")
        w=text.insert(END, readme)
     
        scrolltext = text.Scrollbar(can)
        text.grid(row=10,column=5)
        text.config(yscrollcommand = scrolltext.set)
     
        can.grid(row = 5, rowspan = 20,column =2, columnspan=20, sticky=N)
        self.update_idletasks()
        defgeometry(self)
        self.mainloop()
     
    startinfo()
    l'erreur:
    Traceback (most recent call last):
    File "/home/taz/Desktop/python/build_kernel_tk_info.py", line 90, in <module>
    startinfo()
    File "/home/taz/Desktop/python/build_kernel_tk_info.py", line 81, in startinfo
    scrolltext = text.Scrollbar(can)
    AttributeError: Text instance has no attribute 'Scrollbar'
    je voit pas pourquoi sa miaule

  2. #2
    Membre régulier
    Inscrit en
    Mars 2005
    Messages
    160
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 160
    Points : 91
    Points
    91
    Par défaut
    Salut, je n'ai pas regardé le code mais pourquoi n'utilises tu pas simplement un ScrolledText?

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2008
    Messages : 52
    Points : 29
    Points
    29
    Par défaut
    Citation Envoyé par atalon1 Voir le message
    Salut, je n'ai pas regardé le code mais pourquoi n'utilises tu pas simplement un ScrolledText?

    je veux bien mai je m'en ser comment ?

  4. #4
    Expert éminent
    Avatar de tyrtamos
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2007
    Messages
    4 480
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2007
    Messages : 4 480
    Points : 9 277
    Points
    9 277
    Billets dans le blog
    6
    Par défaut
    Bonjour,

    Scrollbar est un widget, et pas un des attributs du widget Text. D'où le message d'erreur: "AttributeError: Text instance has no attribute 'Scrollbar'".

    Voilà un petit 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
     
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
     
    from Tkinter import *
     
    class Edtexte(Frame):
        def __init__(self, master=None):
            Frame.__init__(self, master, width=600, height=480)
     
            self.grid()
            self.ed = Text(self, wrap=NONE)
            self.ed.grid(row=0, column=0, sticky=N+E+S+W)
            self.sbv = Scrollbar(self, orient=VERTICAL, command=self.ed.yview)
            self.ed.config(yscrollcommand = self.sbv.set)
            self.sbv.grid(row=0, column=1, sticky=N+S+E)
     
    fen = Tk()
    app = Edtexte(fen)
    fen.mainloop()
    Voir ici le chapitre 16.2 de: http://infohost.nmt.edu/tcc/help/pubs/tkinter/

    Par ailleurs, ton code serait plus clair si ta fenêtre était représentée par une classe comme dit dans le chapitre 2 du lien précédent et comme je l'ai fait dans mon exemple.

    Tyrtamos

  5. #5
    Expert éminent sénior
    Avatar de Guigui_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2002
    Messages
    1 864
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2002
    Messages : 1 864
    Points : 10 067
    Points
    10 067

  6. #6
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2008
    Messages : 52
    Points : 29
    Points
    29
    Par défaut
    pour les clase c'est vrai que sa serait plus pratique seulement je comprend pas son utilisation de manière très claire, ce qui n'embrouille complet ment dans d'autre language je n'utilisais pas ce genre de chose, mai je suis avec le cours de
    Gérard Swinnen : Apprendre à programmer avec Python
    et donc j'étudie le cas.

    revenons à nos mouton
    pour tyrtamos j'ai essayer ceci je me retrouve soi avec une erreur
    soi avec le texte qui recouvre completment le caneva
    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
     
    #!/usr/bin/env python
    # -*- coding:Utf-8 -*-
    import sys
    import os
    from PIL import Image,ImageTk
    from Tkinter import *
    #Récupère les fichier et vérifie s'ils sont présent
    def startannex(send,my_file_name):
        #Ret = []
        Ret = os.popen(send).readlines()
        Ret = str(Ret)
        Ret = Ret.strip("[")
        Ret = Ret.strip("]")
        Ret = Ret.strip("'")
        Ret = Ret[:Ret.find('\\')]
        return Ret + "/" + my_file_name
     
     
    def teste_exist_fichier(my_name_file):
        #print os.path.abspath(__file__)
     
        if os.path.abspath(os.path.dirname(__file__) + "/" + my_name_file):
            return os.path.abspath(os.path.dirname(__file__) + "/" + \
            my_name_file)
        if os.path.isfile(startannex("pwd", my_name_file)):
            return startannex("pwd", my_name_file)
        S = sys.argv[0]
        F=S[0:len(sys.argv[0])-len(my_name_file)]
        if os.path.isfile(F+my_name_file):
            return F+my_name_file
        if os.path.isfile(os.path.split(__file__)[0] +"/"+my_name_file):
            return os.path.split(__file__)[0] +"/"+my_name_file
        if os.path.isfile(os.path.split(__file__)[0] +"/"+my_name_file):
            return os.path.split(__file__)[0] +"/"+my_name_file
        print Lfr[0] + my_name_file  
        return False
     
    fichierPngcadre=teste_exist_fichier("cadre.png")
    if fichierPngcadre == False: sys.exit()
     
     
     
    def read_my_file(path_my_file_read):
        file=open(path_my_file_read,'r')
        convercharset = file.read().decode('utf-8').\
        encode(sys.stdout.encoding,'replace')
        file.close()
        #N_lignes = len(lecture)
        return convercharset
     
     
     
     
    def defgeometry(self):
        resolution=(self.winfo_screenwidth()/2,self.winfo_screenheight()/2)
        appsize=(self.winfo_width()/4,self.winfo_height()/4)
        tmpw=((resolution[0] - appsize[0])+15)
        tmph=((resolution[1] - appsize[1])-60)
        self.geometry('+'+str(tmpw)+'+'+str(tmph))
     
    def startinfo():
        self =Tk()
        L=800
        H=600
        H_image=L/2
        L_image=H/2
     
        can = Canvas(self, width =L, height =H, bg ='white')
        mon_image=ImageTk.PhotoImage(file=fichierPngcadre)
        can.create_image(H_image,L_image,image=mon_image)
     
        try:
            readme=read_my_file("/home/taz/Desktop/python/README.txt")
        except:
            print "Charset non suporter"
     
        self.grid()
        can = Text(self, wrap=NONE,width=10,height=10)
        can.grid(row=0, column=0, sticky=N+E+S+W)
        self.sbv = Scrollbar(self, orient=VERTICAL, command=can.yview)
        can.config(yscrollcommand = self.sbv.set)
        self.sbv.grid(row=0, column=1, sticky=N+S+E)
        self.update_idletasks()
        defgeometry(self)
        self.mainloop()
     
    startinfo()



    Citation Envoyé par Guigui_ Voir le message
    Merci pour ta réponse

    Donc j'ai essaye, mai rien a faire sa veux rien savoir

    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
     
    #!/usr/bin/env python
    # -*- coding:Utf-8 -*-
    import sys
    import os
    from PIL import Image,ImageTk
    from Tkinter import *
    #Récupère les fichier et vérifie s'ils sont présent
    def startannex(send,my_file_name):
        #Ret = []
        Ret = os.popen(send).readlines()
        Ret = str(Ret)
        Ret = Ret.strip("[")
        Ret = Ret.strip("]")
        Ret = Ret.strip("'")
        Ret = Ret[:Ret.find('\\')]
        return Ret + "/" + my_file_name
     
     
    def teste_exist_fichier(my_name_file):
        #print os.path.abspath(__file__)
     
        if os.path.abspath(os.path.dirname(__file__) + "/" + my_name_file):
            return os.path.abspath(os.path.dirname(__file__) + "/" + \
            my_name_file)
        if os.path.isfile(startannex("pwd", my_name_file)):
            return startannex("pwd", my_name_file)
        S = sys.argv[0]
        F=S[0:len(sys.argv[0])-len(my_name_file)]
        if os.path.isfile(F+my_name_file):
            return F+my_name_file
        if os.path.isfile(os.path.split(__file__)[0] +"/"+my_name_file):
            return os.path.split(__file__)[0] +"/"+my_name_file
        if os.path.isfile(os.path.split(__file__)[0] +"/"+my_name_file):
            return os.path.split(__file__)[0] +"/"+my_name_file
        print Lfr[0] + my_name_file  
        return False
     
    fichierPngcadre=teste_exist_fichier("cadre.png")
    if fichierPngcadre == False: sys.exit()
     
     
     
    def read_my_file(path_my_file_read):
        file=open(path_my_file_read,'r')
        convercharset = file.read().decode('utf-8').\
        encode(sys.stdout.encoding,'replace')
        file.close()
        #N_lignes = len(lecture)
        return convercharset
     
    def defgeometry(self):
        resolution=(self.winfo_screenwidth()/2,self.winfo_screenheight()/2)
        appsize=(self.winfo_width()/4,self.winfo_height()/4)
        tmpw=((resolution[0] - appsize[0])+15)
        tmph=((resolution[1] - appsize[1])-60)
        self.geometry('+'+str(tmpw)+'+'+str(tmph))
     
    def startinfo():
        self =Tk()
        L=800
        H=600
        H_image=L/2
        L_image=H/2
     
        can = Canvas(self, width =L, height =H, bg ='white')
        mon_image=ImageTk.PhotoImage(file=fichierPngcadre)
        can.create_image(H_image,L_image,image=mon_image)
     
        try:
            readme=read_my_file("/home/taz/Desktop/python/README.txt")
        except:
            print "Charset non suporter"
     
         ## Déclaration des différents widgets
        f1 = can
        s1 = self.Scrollbar(f1, orient=self.VERTICAL)
        s2 = self.Scrollbar(f1, orient=self.HORIZONTAL)
        t1 = self.Text(f1, wrap=self.NONE)  
     
        ## association du déplacement de la glissière des scrollbar avec la position visible dans 
        ## le widget Text et inversement.              
        s1.config(command = t1.yview)
        s2.config(command = t1.xview)
        t1.config(yscrollcommand = s1.set, xscrollcommand = s2.set)
     
        ## Placement du widget Text et des Scrollbar associés
        t1.grid(column=0, row=0)
        s1.grid(column=1, row=0, sticky=self.S+self.N)
        s2.grid(column=0, row=1, sticky=self.W+self.E)
     
        can.grid(row = 5, rowspan = 20,column =2, columnspan=20, sticky=N)
        self.update_idletasks()
        defgeometry(self)
        self.mainloop()
     
    startinfo()
    donc je suppose que le caneva n'est pas fait pour sa. Seulement il contiens une image et il faut que les contrôles soie placer aux desus.

    Merci d'avance

  7. #7
    Expert éminent
    Avatar de tyrtamos
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2007
    Messages
    4 480
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2007
    Messages : 4 480
    Points : 9 277
    Points
    9 277
    Billets dans le blog
    6
    Par défaut
    Dans ton 1er code, ta variable "can" ne peut pas représenter en même temps un "Canvas" et un "Text". D'où le mélange.

    Tyrtamos

  8. #8
    Membre averti
    Homme Profil pro
    Responsable du parc et des réseaux de télécommunication
    Inscrit en
    Mai 2003
    Messages
    290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Responsable du parc et des réseaux de télécommunication
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2003
    Messages : 290
    Points : 388
    Points
    388
    Par défaut
    Bonjour,
    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
    def defgeometry(root):
        resolution=(root.winfo_screenwidth()/2,root.winfo_screenheight()/2)
        appsize=(root.winfo_width()/4,root.winfo_height()/4)
        tmpw=((resolution[0] - appsize[0])+15)
        tmph=((resolution[1] - appsize[1])-60)
        root.geometry('+'+str(tmpw)+'+'+str(tmph))
     
    def startinfo():
        root =Tk()
        L=800
        H=600
        H_image=L/2
        L_image=H/2
     
        can = Canvas(root, width =L, height =H, bg ='white')
        mon_image=ImageTk.PhotoImage(file=fichierPngcadre)
        can.create_image(H_image,L_image,image=mon_image)
     
        try:
            readme=read_my_file("/home/taz/Desktop/python/README.txt")
        except:
            print "Charset non suporter"
     
         ## Déclaration des différents widgets
     
        fr=Frame(can)
        s1 = Scrollbar(fr, orient=VERTICAL)
        s2 = Scrollbar(fr, orient=HORIZONTAL)
        t1 = Text(fr, wrap=NONE)  
        t1.grid(column=0, row=0)
        s1.grid(column=1, row=0, sticky=S+N)
        s2.grid(column=0, row=1, sticky=W+E)
     
        ## association du déplacement de la glissière des scrollbar avec la position visible dans 
        ## le widget Text et inversement.              
        t1.config(yscrollcommand = s1.set, xscrollcommand = s2.set)
     
        ## Placement du widget Text et des Scrollbar associés
        can.create_window(0,0,window=fr,anchor=NW) #tu places ta frame où tu veux
        can.grid(row = 5, rowspan = 20,column =2, columnspan=20, sticky=N)
        root.update_idletasks()
        defgeometry(root)
        mainloop()
     
    startinfo()
    Tu devrait utiliser un ScrolledText.
    (from ScrolledText import ScrolledText)

  9. #9
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2008
    Messages : 52
    Points : 29
    Points
    29
    Par défaut
    Citation Envoyé par pierjean Voir le message
    Bonjour,
    Tu devrait utiliser un ScrolledText.
    (from ScrolledText import ScrolledText)
    Mai une question pourquoi cette préférence sinon

    Merci pour le code sa marche.
    Merci également a ceux qui m'on apporter des information complémentaire.

    c'est possible de jouer avec la transparence avec Tinker ? car j'ai 3 image qui contienne du text mai si a la place je pourrait écrire par dessu via le code sa serait plus pratique

Discussions similaires

  1. Réponses: 6
    Dernier message: 28/06/2012, 21h12
  2. Widget Text avec variable
    Par frites.saucisse dans le forum Tkinter
    Réponses: 5
    Dernier message: 14/10/2008, 14h28
  3. undo avec un widget Text
    Par will97 dans le forum Tkinter
    Réponses: 9
    Dernier message: 05/06/2007, 20h52
  4. Probleme avec widget Text
    Par will97 dans le forum Tkinter
    Réponses: 4
    Dernier message: 16/03/2007, 21h58
  5. [tkinter] widget text et scrollbar
    Par jojolapine dans le forum Tkinter
    Réponses: 3
    Dernier message: 29/04/2006, 15h58

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