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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog, ttk
from tkinter.filedialog import askopenfile
import os
from os.path import basename
varGlobal = 0
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(nb.index('current'))
basename(filename)
frame1 = create_frame(nb, t)
nb.add(frame1, text=basename(filename))
tbox1 = Text(frame1, wrap=WORD)
#print(tbox1)
#print(nb.index(frame1))
#print(nb.select(nb.index("current")))
nb.select(nb.index(frame1))
file.close()
def callback():
frame.focus_set()
print ("clicked at", event.x, event.y)
def create_frame(master, t):
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, background="beige")
#print ("Mon Onglet : "+str(text))
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
text.insert(0.0, t)
#print(text.grid)
return frame
if __name__ == '__main__':
root = Tk()
root.title("MonkeyCode Editor")
menubar = Menu(root)
filemenu = Menu(menubar)
EditMenu = Menu(menubar)
filemenu.add_command(label="Ouvrir", command=openFile)
menubar.add_cascade(label="Fichier", menu=filemenu)
EditMenu.add_command(label="Couper", \
accelerator="Ctrl+X", \
command=lambda: \
root.focus_get().event_generate('<<Cut>>'))
EditMenu.add_command(label="Copier", \
accelerator="Ctrl+C", \
command=lambda: \
root.focus_get().event_generate('<<Copy>>'))
EditMenu.add_command(label="Coller", \
accelerator="Ctrl+V", \
command=lambda: \
root.focus_get().event_generate('<<Paste>>'))
menubar.add_cascade(label="Édition",menu=EditMenu)
root.config(menu=menubar)
nb = ttk.Notebook(root)
root.update()
def ferme():
global varGlobal
for widget in root.winfo_children():
if isinstance(widget,Toplevel):
widget.destroy()
varGlobal = 0
def fonc4(event):
print ("TOP FENETRE")
print ("Click GAUCHE")
def pointeur(evt):
position.configure(text="Clic détecté en X = "+str(evt.x)+", Y = "+str(evt.y))
def fonc3(event):
global varGlobal
if varGlobal == 0 :
print ("EXISTE PAS")
varGlobal = 1
top = Toplevel(root)
separator = ttk.Separator(top, orient="horizontal")
top.overrideredirect(1)
#print(top.geometry(), root.geometry()) # Affiche 1x1+0+0 1x1+0+0
top.geometry("{}x{}+{}+{}".format(200, 120, 400, 400))
L1=Label(top, text='Couper', activebackground="blue")
L1.grid(row=1, column=0, padx=20, sticky='w')
L2=Label(top, text='Copier', activebackground="blue")
L2.grid(row=2, column=0, padx=20, sticky='w')
L3=Label(top, text='Coller', activebackground="blue")
L3.grid(row=3, column=0, padx=20, sticky='w')
separator.grid(row=4, column=0, sticky="nwse")
L4=Label(top, text='Rechercher', activebackground="blue")
L4.grid(row=5, column=0, padx=20, sticky='w')
B1=Button(top, text='Quitter', activebackground="blue", command=ferme)
B1.grid(row=6, column=0, padx=20, sticky='w')
top.configure(bd=1, relief=RAISED)
top.rowconfigure(7, weight=0)
top.columnconfigure(0, weight=0)
#top.bind("<Button-1>", fonc4)
L1.bind('<FocusIn>', lambda e: L1.config(state='active'))
L1.bind('<Enter>', lambda e: L1.config(state='active'))
L1.bind('<FocusOut>', lambda e: L1.config(state='normal'))
L1.bind('<Leave>', lambda e: L1.config(state='normal'))
L2.bind('<FocusIn>', lambda e: L2.config(state='active'))
L2.bind('<Enter>', lambda e: L2.config(state='active'))
L2.bind('<FocusOut>', lambda e: L2.config(state='normal'))
L2.bind('<Leave>', lambda e: L2.config(state='normal'))
L3.bind('<FocusIn>', lambda e: L3.config(state='active'))
L3.bind('<Enter>', lambda e: L3.config(state='active'))
L3.bind('<FocusOut>', lambda e: L3.config(state='normal'))
L3.bind('<Leave>', lambda e: L3.config(state='normal'))
L4.bind('<FocusIn>', lambda e: L4.config(state='active'))
L4.bind('<Enter>', lambda e: L4.config(state='active'))
L4.bind('<FocusOut>', lambda e: L4.config(state='normal'))
L4.bind('<Leave>', lambda e: L4.config(state='normal'))
top.bind("<Button-1>", pointeur)
else :
print ("EXISTE")
ferme()
#print(top.geometry()) # Affiche 1x1+400+400
def fonc1(event):
ferme()
root.bind("<Button-3>", fonc3)
root.bind("<Button-1>", fonc1)
t=""
frame2 = create_frame(nb, t)
nb.add(frame2, text="new 1")
tbox2 = Text(frame2, wrap=WORD)
#print(tbox2)
nb.pack(expand=YES, fill=BOTH)
position=Label(root,text="Clic détecté en X =''+'','' Y = ")
position.pack()
root.bind("<Button-1>", pointeur)
mainloop() |
Partager