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 :
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 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)
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
Partager