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

Python Discussion :

Selection Onglet ttk


Sujet :

Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    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
    Par défaut Selection Onglet ttk
    Bonjour, j'ai un bouton et deux onglets.

    Le premier onglet est sélectionné. Je voudrai que lorsque je clique sur mon bouton le deuxième onglet soit sélectionné.

    ça marche pour mon 1er 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
    import os
    from PIL import Image, ImageTk
    from tkinter import Tk, Frame, Menu, Button, Text, E, W, S, N, WORD, ttk
    from tkinter import LEFT, TOP, X, FLAT, RAISED, BOTH, END
     
    root = Tk()
     
    def Play():
     
        nb.select(1)
     
     
    # Add the toolbar
    toolbar = Frame(root, bd=1, relief=RAISED)
    toolbar.pack(side=TOP, fill=X)
     
    Play1=Image.open('Play1.PNG')    # Je charge l'image et je l'adapte au dimension du Bouton
    image2=Play1.resize((20,20))
    Play1=ImageTk.PhotoImage(image2)
     
    ExecuteBtn = Button(toolbar, image=Play1, command=Play)
    ExecuteBtn.pack(side=LEFT, fill=X) 
     
    menubar = Menu(root)
    root.config(menu=menubar)
     
    # Add the textbox
     
    # Defines and places the notebook widget      
    nb = ttk.Notebook(root)
     
    #nb.grid(row=0, column=0, columnspan=50, rowspan=49, sticky='NESW')
     
    # Adds tab 1 of the notebook
    page1 = ttk.Frame(nb)
    nb.add(page1, text='Texte')
     
    # Add the textbox
    tbox2 = Text(page1, wrap=WORD)
    tbox2.pack(fill=BOTH, expand=1)
     
    # Adds tab 2 of the notebook
    page2 = ttk.Frame(nb)
    nb.add(page2, text='Tab2')
     
    # Add the textbox
    tbox3 = Text(page2, wrap=WORD)
    tbox3.pack(fill=BOTH, expand=1)
     
    nb.pack(fill=BOTH, expand=1)
    mais dans mon 2ème code ça me répond "NameError: name 'nb' is not defined" :

    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
    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
     
    import os
    from PIL import Image, ImageTk
    from tkinter import Tk, Frame, Menu, Button, Text, E, W, S, N, WORD, ttk
    from tkinter import LEFT, TOP, X, FLAT, RAISED, BOTH, END
     
     
    class Example(Frame):
     
        def __init__(self):
            super().__init__()
     
            self.initUI()
     
        def initUI(self):
     
            myWidth = 25 #largeur des boutons du menu
            myHeight = 25  #hauteur des boutons du menu
     
            self.master.title("Toolbar")
     
            self.toolbar = Frame(self.master, bd=1, relief=RAISED)
     
            # Load all the images first as PNGs and use ImageTk to convert
            # them to usable tkinter images.
     
            self.img1=Image.open('Play1.PNG')    # Je charge l'image et je l'adapte au dimension du Bouton
            image1=self.img1.resize((myWidth,myHeight))
            self.useImg1=ImageTk.PhotoImage(image1)
     
            # Set up all the buttons for use on the toolbars.
     
            playBtn = Button(self.toolbar, image=self.useImg1, command=self.play2)        
            playBtn.pack(side=LEFT, fill=X)
     
     
            # Add the toolbar
            self.toolbar.pack(side=TOP, fill=X) 
     
            # Add the textbox
     
            # Defines and places the notebook widget
     
            nb = ttk.Notebook(self.master)
     
            #nb.grid(row=0, column=0, columnspan=50, rowspan=49, sticky='NESW')
     
            # Adds tab 1 of the notebook
            page1 = ttk.Frame(nb)
            nb.add(page1, text='Texte')
     
            # Add the textbox
            self.tbox2 = Text(page1, wrap=WORD)
            self.tbox2.pack(fill=BOTH, expand=1)
     
            # Adds tab 2 of the notebook
            page2 = ttk.Frame(nb)
            nb.add(page2, text='Tab2')
     
            # Add the textbox
            self.tbox3 = Text(page2, wrap=WORD)
            self.tbox3.pack(fill=BOTH, expand=1)
     
            nb.pack(fill=BOTH, expand=1)
     
            #self.pack()
     
     
     
        def play2(self):
           nb.select(1) 
     
     
    def main():
     
        root = Tk()
        root.geometry("850x650+100+100")
     
        app = Example()
        root.mainloop()  
     
     
    if __name__ == '__main__':
        main()

    Pourriez-vous m'aider à faire en sorte que mon 2ème code marche aussi?

    Je vous en remercie d'avance.
    Arsène

  2. #2
    Membre éprouvé
    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
    Par défaut
    J'ai trouvé. Il fallait juste remplacer nb par self.nb.

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

Discussions similaires

  1. [XL-2007] selection de l'onglet à l'ouverture du fichier
    Par bosk1000 dans le forum Macros et VBA Excel
    Réponses: 11
    Dernier message: 18/11/2009, 22h36
  2. Selection l'onglet avec SWING
    Par totonin dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 29/05/2009, 13h32
  3. Selection Onglet Suivant
    Par luige93 dans le forum Macros et VBA Excel
    Réponses: 18
    Dernier message: 11/07/2008, 16h24
  4. selection automatique d'onglet
    Par maverick56 dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 04/01/2008, 18h57
  5. Réponses: 4
    Dernier message: 02/12/2007, 08h31

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