Bonjour,

j'ai fait ce code pour bénéficier de la fonction Autohide d'une scrollbar mais il me prive de la fonction pack() lors de la création des onglets.
Ce qui fait que j'arrive plus à ouvrir un fichier texte dans un des onglets.
Sauriez-vous comment faire, svp.


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
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog, ttk
from tkinter.filedialog import askopenfile
 
class MyTab(Frame):
 
    def __init__(self, root, name):
        Frame.__init__(self, root)
 
        self.root = root
        self.name = name
 
class AutoScrollbar(Scrollbar):
    # a scrollbar that hides itself if it's not needed.  only
    # works if you use the grid geometry manager.
    def set(self, lo, hi):
        if float(lo) <= 0.0 and float(hi) >= 1.0:
            self.grid_remove()
        else:
            self.grid()
        Scrollbar.set(self, lo, hi)
    def pack(self, **kw):
        raise TclError ("cannot use pack with this widget")
    def place(self, **kw):
        raise TclError ("cannot use place with this widget")
 
def openFile():
    global filename
    file = askopenfile(parent=root,title='Select a File')
    filename = file.name
    t = file.read()
 
    print(tab_names[0])
 
    tab_names[1].delete(0.0, END)
    tab_names[1].insert(0.0, t)
    file.close()    
 
def create_frame(master):
    frame = Frame(master, bd=2, relief=SUNKEN)
    yscrollbar = AutoScrollbar(frame)
    yscrollbar.grid(row=0, column=1, sticky=N+S)
    text = Text(frame, 
                yscrollcommand=yscrollbar.set)
 
    text.grid(row=0, column=0, sticky='nwse') # expansion du widget Text
 
    frame.grid_columnconfigure(0, weight=1) # expansion de la colonne
    frame.grid_rowconfigure(0, weight=1) # expansion de la colonne
 
    return frame
 
if __name__ == '__main__':
    root = Tk()
 
    root.title("MonkeyCode Editor")
 
    menubar = Menu(root)
    filemenu = Menu(menubar)
    filemenu.add_command(label="Open", command=openFile)
    menubar.add_cascade(label="File", menu=filemenu)
 
    root.config(menu=menubar)   
 
    nb = ttk.Notebook(root)
 
    root.update()
 
    #for i in range(0, len(tab_names)):
 
    frame1 = create_frame(nb)
    nb.add(frame1, text="Text1")
 
    frame2 = create_frame(nb)
    nb.add(frame2, text="Text2")
 
    frame3 = create_frame(nb)
    nb.add(frame3, text="Text3")
 
    frame4 = create_frame(nb)
    nb.add(frame4, text="Text4") 
 
    nb.pack(expand=YES, fill=BOTH)
 
    mainloop()

Je vous remercie d'avance.

Arsène