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 :

Taille de la fenêtre : root.winfo_width()


Sujet :

Tkinter Python

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Février 2003
    Messages
    926
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2003
    Messages : 926
    Points : 273
    Points
    273
    Par défaut Taille de la fenêtre : root.winfo_width()
    Bonjour,

    J'ai écrit le code ci-dessous qui crée des frames et y ajoute des boutons. Je voudrais que l'ajout des boutons s'achève dès que le bord droit du root est dépassé.

    Si je remplace 10 par root.winfo_width() dans mon code, j'obtiens pas le résultat escompté. Pourriez-vous m'aider à résoudre ce problème svp? Merci.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    if TBtn0<10 : #root.winfo_width():
    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
    from tkinter import *
     
    root = Tk()
    root.geometry('500x500')
    root.update_idletasks()
     
    i = 0
    TBtn0 = 0
    text = Text(root)
    text.pack(fill=X)
     
    while i<51 :
        bt = Button(text, text="XXXX", background="red")
        TBtn = bt.winfo_width()
        TBtn0 = TBtn0 + TBtn
        if TBtn0<10 : #root.winfo_width():        
            bt.pack(side=LEFT)      
        else :
            text = Text(root)
            text.pack(fill=X)
            bt = Button(text, text="YYYY", background="red")
            bt.pack(side=LEFT)
            TBtn0 = 0
        i = i+1
     
    print(root.winfo_width())

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    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 287
    Points : 36 778
    Points
    36 778
    Par défaut
    Citation Envoyé par Arsene12 Voir le message
    J'ai écrit le code ci-dessous qui crée des frames et y ajoute des boutons.
    Déjà pour créer des frame, c'est le widget Frame et non le widget Text.

    Citation Envoyé par Arsene12 Voir le message
    Je voudrais que l'ajout des boutons s'achève dès que le bord droit du root est dépassé.
    La taille d'un Button ne sera fixée/donnée qu'après son affichage via .pack ou .grid (et éventuellement un .udpate).
    Avant, ce sera probablement 1 (et non ce que çà va occuper).
    Vous pouvez essayer de la calculer puisque ce sera fonction de la taille de la police de caractères et de leur nombre. Vous pouvez aussi l'afficher, regarder si la taille totale ne dépasse pas la fenêtre et dans ce cas, le retirer et l'afficher en dessous.
    Mais çà serait plus facile d'utiliser le widget Text directement plutôt que d'essayer de le refaire.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Février 2003
    Messages
    926
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2003
    Messages : 926
    Points : 273
    Points
    273
    Par défaut
    Citation Envoyé par wiztricks Voir le message
    Vous pouvez essayer de la calculer puisque ce sera fonction de la taille de la police de caractères et de leur nombre. Vous pouvez aussi l'afficher, regarder si la taille totale ne dépasse pas la fenêtre et dans ce cas, le retirer et l'afficher en dessous.
    Mais çà serait plus facile d'utiliser le widget Text directement plutôt que d'essayer de le refaire.
    - W
    Merci. J'ai rectifié 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
    from tkinter import *
     
    root = Tk()
    root.geometry('500x500')
    root.update_idletasks()
    n=0
    text = "text"+str(n)
    i = 0
    TBtn0 = 0
    text = Frame(root)
    text.pack(fill=X)
    root.update()
    print(text.winfo_width())
    while i<51 :
        bt = Button(text, text="XXXX", background="red")
        bt.pack(side=LEFT)
        root.update()
        TBtn = bt.winfo_width()
        TBtn0 = TBtn0 + TBtn
        if TBtn0<root.winfo_width():
            pass
            #print(TBtn)
        else :
            n=n+1
            text = "text"+str(n)
            text = Frame(root)
            text.pack(fill=X)
            bt = Button(text, text="YYYY", background="red")
            bt.pack(side=LEFT)
            root.update()
            TBtn0 = 0
        i = i+1
    Ça ne fonctionne que pour la première ligne. Dans mon programme, où y'a une classe et une fonction, ça marche. Mais quand la fenêtre s'affiche lorsque je fait run, elle correspond pas à la taille du root :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
        L = 600
        H = 600
        root.geometry('{}x{}'.format(L, H))
    Je vais rechercher ce qui va pas dans mon code.

  4. #4
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    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 287
    Points : 36 778
    Points
    36 778
    Par défaut
    Juste pour vous montrer comment créer des sortes de Button dans le texte d'un widget Text:
    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
    import tkinter as tk
     
    text = tk.Text()
    text.tag_config("label", foreground="lightblue")
    text.tag_config("button", foreground="grey")
    text.pack()
     
    def on_click(e):
        indexes = text.tag_prevrange('button', "@%d,%d"%(e.x,e.y))
        print(text.get(*indexes))
     
    text.tag_bind("button", "<Button-1>", on_click)
    for i in range(20):
        text.insert('end', 'label-%d ' % i, "label", "button-%d " % i, "button")
     
    tk.mainloop()
    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Février 2003
    Messages
    926
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2003
    Messages : 926
    Points : 273
    Points
    273
    Par défaut
    Citation Envoyé par wiztricks Voir le message
    Juste pour vous montrer comment créer des sortes de Button dans le texte d'un widget Text:
    - W
    Merci pour cet exemple qui m'indique surtout comment rendre un mot cliquable dans un widget text. Je l'ai adapté et je pense qu'il me servira plus tard:
    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
     
    import tkinter as tk
     
    text = tk.Text()
    text.tag_config("space", foreground="lightblue")
    text.tag_config("button", foreground="grey")
    text.pack()
     
    def on_click(e):
        indexes = text.tag_prevrange('button', "@%d,%d"%(e.x,e.y))
        print(text.get(*indexes))
     
    text.tag_bind("button", "<Button-1>", on_click)
     
    for i in range(20):
        text.insert('end', ' ', "space", "button-%d " % i, "button")
    tk.mainloop()
    Mais en ce qui concerne mon problème, j'ai presque trouvé la solution :

    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
    from tkinter import *
     
    def create_frame(master, t):
     
        L = 400
        H = 600
     
        frame = Frame(master, bd=2, bg="beige")#, relief=SUNKEN)
     
        t = "mon lab" 
        monText = Text(frame)
     
        newBtn = Button(toolbar, text="Tableau", bg="maroon", fg="white", borderwidth=3, command=myClass)
        newBtn.pack(side=LEFT, fill=X)
     
        root.geometry('{}x{}'.format(L, H))
        root.minsize(L, H)
        root.update_idletasks()
        print(root.winfo_width())
        print(frame.winfo_width())
     
        return frame 
     
     
    if __name__ == '__main__':
     
        class myClass(Tk):
     
            def __init__(self, *args, **kwargs):
                for w in frame1.winfo_children():
                        w.destroy()
     
                n=0
                i = 0
                TBtn0 = 0
                text = Frame(root)
                text.pack(fill=X)
                root.update()
                #print(text.winfo_width())
                while i<51 :
                    bt = Button(text, text="XXXX", background="red")
                    bt.pack(side=LEFT)
                    root.update()
                    TBtn = bt.winfo_width()
                    TBtn0 = TBtn0 + TBtn
                    print(TBtn)
                    if TBtn0<root.winfo_width():
                        pass                    
                    else :
                        n=n+1
                        #text = Frame(root)
                        #text.pack(fill=X)
                        bt = Button(text, text="YYYY", background="red")
                        bt.pack(side=LEFT)
                        text = Frame(root)
                        text.pack(fill=X)
                        TBtn0 = 0
                        root.update()                    
                    i = i+1
     
                    #print(root.winfo_width())
     
        #app.mainloop()
     
        root = Tk()    
        color= "lightyellow"
     
        root.title("Syntax Analyser")
        #root.minsize(width=400, height=400)
        #root.maxsize(width=400, height=400)
     
        myWidth = 25 #largeur des boutons du menu
        myHeight = 25  #hauteur des boutons du menu
     
        myColor="lightblue"
     
        toolbar = Frame(root, borderwidth=2, relief='raised', background=myColor)
        toolbar2 = Frame(root, borderwidth=2, relief='raised', background=myColor)
     
        toolbar.pack(side=TOP, fill=X)    
        toolbar2.pack(side=TOP, fill=X)
     
        root.update()
        numero=1
        frame1 = create_frame(root, numero)
     
        # Fin du menu deroulant #############################################  
     
        root.configure(background="green")
        root.mainloop()

  6. #6
    Membre actif
    Profil pro
    Inscrit en
    Février 2003
    Messages
    926
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2003
    Messages : 926
    Points : 273
    Points
    273
    Par défaut
    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
    from tkinter import *
     
    def create_frame(master, t):
     
        L = 400
        H = 600
     
        frame = Frame(master, bd=2, bg="beige")#, relief=SUNKEN)
     
        t = "mon lab" 
        monText = Text(frame)
     
        newBtn = Button(toolbar, text="Tableau", bg="maroon", fg="white", borderwidth=3, command=myClass)
        newBtn.pack(side=LEFT, fill=X)
     
        root.geometry('{}x{}'.format(L, H))
        root.minsize(L, H)
        root.update_idletasks()
        print(root.winfo_width())
        print(frame.winfo_width())
     
        return frame 
     
     
    if __name__ == '__main__':
     
        class myClass(Tk):
     
            def __init__(self, *args, **kwargs):
                for w in frame1.winfo_children():
                        w.destroy()
     
                n=0
                i = 0
                TBtn0 = 0
                text = Frame(root)
                text.pack(fill=X)
                root.update()
                #print(text.winfo_width())
                while i<51 :
                    myBt= "bt"+str(n)
                    myBt = Button(text, text=i, background="red")
                    myBt.pack(side=LEFT)
                    root.update()
                    TBtn = myBt.winfo_width()
                    TBtn0 = TBtn0 + TBtn                               
                    if TBtn0<root.winfo_width():
                        print(TBtn) 
                        n=n+1
                        pass                    
                    else :
                        text = Frame(root)
                        text.pack(fill=X)
                        myBt.destroy()
                        myBt= "bt"+str(n)
                        myBt= Button(text, text=i, background="red")
                        myBt.pack(side=LEFT)
                        text = Frame(root)
                        text.pack(fill=X)
                        TBtn0 = 0
                        n=n+1
                        root.update()                    
                    i = i+1
     
                    #print(root.winfo_width())
     
        #app.mainloop()
     
        root = Tk()    
        color= "lightyellow"
     
        root.title("Syntax Analyser")
        #root.minsize(width=400, height=400)
        #root.maxsize(width=400, height=400)
     
        myWidth = 25 #largeur des boutons du menu
        myHeight = 25  #hauteur des boutons du menu
     
        myColor="lightblue"
     
        toolbar = Frame(root, borderwidth=2, relief='raised', background=myColor)
        toolbar2 = Frame(root, borderwidth=2, relief='raised', background=myColor)
     
        toolbar.pack(side=TOP, fill=X)    
        toolbar2.pack(side=TOP, fill=X)
     
        root.update()
        numero=1
        frame1 = create_frame(root, numero)
     
        # Fin du menu deroulant #############################################  
     
        root.configure(background="green")
        root.mainloop()

  7. #7
    Membre actif
    Profil pro
    Inscrit en
    Février 2003
    Messages
    926
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2003
    Messages : 926
    Points : 273
    Points
    273
    Par défaut
    J'ai trouvé d'où venait mon erreur.
    Il fallait que je réinitialise TBtn0 = myBt.winfo_width() et non TBtn0 = 0.

    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
    from tkinter import *
     
    def create_frame(master, t):
     
        L = 400
        H = 600
     
        frame = Frame(master, bd=2, bg="beige")#, relief=SUNKEN)
     
        t = "mon lab" 
        monText = Text(frame)
     
        newBtn = Button(toolbar, text="Tableau", bg="maroon", fg="white", borderwidth=3, command=myClass)
        newBtn.pack(side=LEFT, fill=X)
     
        root.geometry('{}x{}'.format(L, H))
        root.minsize(L, H)
        root.update_idletasks()
        print(root.winfo_width())
        print(frame.winfo_width())
     
        return frame 
     
     
    if __name__ == '__main__':
     
        class myClass(Tk):
     
            def __init__(self, *args, **kwargs):
                for w in frame1.winfo_children():
                        w.destroy()
     
                n=0
                i = 0
                TBtn0 = 0
                text = Frame(root, bg='maroon')
                text.pack(fill=X)
                root.update()
                #print(text.winfo_width())
                while i<51 :
                    myBt= "bt"+str(n)
                    myBt = Button(text, text=i, background="red")
                    myBt.pack(side=LEFT)
                    root.update()
                    TBtn = myBt.winfo_width()
                    TBtn0 = TBtn0 + TBtn                               
                    if TBtn0<root.winfo_width():
                        pass
                        print(str(n)+ " - " + str(TBtn)) 
                        n=n+1
                        pass                    
                    else :
                        #print(TBtn)
                        myBt.destroy()                
                        root.update() 
                        text = Frame(root, bg='maroon')
                        text.pack(fill=X)
                        root.update() 
                        myBt= Button(text, text=i, background="red")
                        myBt.pack(side=LEFT)
                        root.update()                  
                        TBtn0 = myBt.winfo_width()
                        n=n+1            
                    i = i+1
     
                    #print(root.winfo_width())
     
        #app.mainloop()
     
        root = Tk()    
        color= "lightyellow"
     
        root.title("Syntax Analyser")
        #root.minsize(width=400, height=400)
        #root.maxsize(width=400, height=400)
     
        myWidth = 25 #largeur des boutons du menu
        myHeight = 25  #hauteur des boutons du menu
     
        myColor="lightblue"
     
        toolbar = Frame(root, borderwidth=2, relief='raised', background=myColor)
        toolbar2 = Frame(root, borderwidth=2, relief='raised', background=myColor)
     
        toolbar.pack(side=TOP, fill=X)    
        toolbar2.pack(side=TOP, fill=X)
     
        root.update()
        numero=1
        frame1 = create_frame(root, numero)
     
        # Fin du menu deroulant #############################################  
     
        root.configure(background="green")
        root.mainloop()

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

Discussions similaires

  1. [JFrame] Taille de la fenêtre complète (avec titre et bordures)
    Par RXN dans le forum Agents de placement/Fenêtres
    Réponses: 7
    Dernier message: 23/09/2005, 23h35
  2. Réponses: 2
    Dernier message: 14/06/2005, 16h14
  3. Réponses: 7
    Dernier message: 07/06/2004, 10h42
  4. [Débutant] Trouvez la taille d'une fenêtre
    Par Sharukh Khan dans le forum MFC
    Réponses: 6
    Dernier message: 06/06/2004, 22h23
  5. Modifier la taille de la fenêtre DOS
    Par bobgeldof7 dans le forum Scripts/Batch
    Réponses: 8
    Dernier message: 31/01/2004, 03h10

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