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
| # -*- coding: utf-8 -*-
"""
Created on Jully 2017
@author: Anon
"""
from tkinter import Frame, Tk, BOTH, Text, Menu, END, Toplevel
import tkinter.filedialog as diag
import tkinter.ttk as ttk
import os
def save(file):
saver = open('saver.txt', 'a+')#os.popen('attrib +h ' + saver)
saver.seek(0)
content = saver.readlines()
toSave = file
if not content:#check if empty
saver.write(toSave)
else:
newContent = '\n'+toSave
saver.write(newContent)
saver.close()
def removeFile(win, bt, file):
with open('saver.txt', 'a+') as saver:
saver.seek(0)
content = saver.readlines()
remove = str(os.path.abspath(file).replace('\\', '/'))
for k in range (0, len(content)):
content[k] = content[k].replace('\n','')
content.remove(remove)
newContent = ''
for k in range(0, len(content)):
newContent += content[k]
os.remove('saver.txt')
saver = open('saver.txt', 'a+')
saver.seek(0)
saver.write(newContent)
bt.pack_forget()
win.destroy()
def openWin(text, title, root):
win4 = Toplevel(root)
win4.txt = Text(win4)
win4.txt.pack(fill=BOTH, expand=1)
win4.txt.insert(END, text)
bouton = ttk.Button(win4, text = "Close", command=win4.destroy)
bouton.pack()
h = win4.winfo_screenheight()
w = win4.winfo_screenwidth()
win4.geometry(str(int(w/1.5))+'x'+str(int(h/1.5))+'+'+str(int((w-(w/1.5))/2))+'+'+str(int((h-(h/1.5))/3)))
win4.title('Mon prog - '+title)
def fileStats(file, root, btRm):
print('#fileStats: ',file,root,btRm)#Mauvais affichage....
win2 = Toplevel(root)
fileName = str(file).split('/')[-1].split('.')[0]
lb = ttk.Label(win2, width=300, text=fileName, anchor="center")
lb.pack()
bt = ttk.Button(win2, text="Details", command=lambda:openDetails(file, root))
bt.pack(fill='x')
bt = ttk.Button(win2, text="Remove file from list", command=lambda:removeFile(win2, btRm, file))
bt.pack(fill='x', side='bottom')
bt = ttk.Button(win2, text="Close", command=win2.destroy)
bt.pack(fill='x', side='bottom')
win2.geometry("250x220+150+150")
win2.title('Mon prog')
def openDetails(file, root):
win3 = Toplevel(root)
win3.txt = Text(win3)
win3.txt.pack(fill=BOTH, expand=1)
f = open(file, "rb")
text = f.readlines()
f.close()
page = ''
for k in range(0, len(text)):
page += text[k].decode('gb2312','ignore')
win3.txt.insert(END, page)
bouton = ttk.Button(win3, text = "Close", command=win3.destroy)
bouton.pack()
h = win3.winfo_screenheight()
w = win3.winfo_screenwidth()
win3.geometry(str(int(w/1.5))+'x'+str(int(h/1.5))+'+'+str(int((w-(w/1.5))/2))+'+'+str(int((h-(h/1.5))/3)))
win3.title('Mon prog - '+str(file).split('/')[-1].split('.')[0]+' - Details')
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
btList = []
self.parent.title("Mon prog")
self.pack(fill=BOTH, expand=1)
saver = open('saver.txt', 'a+')
saver.seek(0)
content = saver.readlines()
saver.close()
if not content:#check if empty
lb = ttk.Label(self, width=300, text="No file", anchor="center")
lb.pack()
else:
lb = ttk.Label(self, width=300, text="Your files", anchor="center")
lb.pack()
for k in range(0, len(content)):
if content[k] != '\n' and content[k] != '\r' and content[k] != '\r\n':
path = content[k].replace('\r\n','')
path = content[k].replace('\n','')
fileShort = path.split('/')[-1]
fileName = fileShort.split('.')[0]
bouton = ttk.Button(self, text=fileName)
bouton.pack(fill='x')
bouton.config(command=lambda:fileStats(fileShort, self, bouton))
print(k,'#',fileShort,bouton)#jai un bon affichage
btList.append(bouton)
for k in range(0, len(btList)):
print('#Liste: ',btList[k].cget("command"))#affichage correct
menubar = Menu(self.parent)
fileMenu = Menu(menubar, tearoff=0)
fileMenu.add_command(label="Open", command=lambda:self.onOpen(lb))
menubar.add_cascade(label="File", menu=fileMenu)
self.parent.config(menu=menubar)
def onOpen(self, lb):
ftypes = [('Text files', '*.txt'), ('All files', '*')]
dlg = diag.Open(self, filetypes = ftypes)
fl = dlg.show()
if fl != '':
if lb.cget("text") == 'No file':
lb.config(text="Your files")
saver = open('saver.txt', 'a+')
saver.seek(0)
content = saver.readlines()
saver.close()
content = [x.replace('\n','') for x in content]
if fl not in content:
fileName = str(fl).split('/')[-1].split('.')[0]
bouton = ttk.Button(self, text=fileName)
bouton.pack(fill='x')
bouton.config(command=lambda:fileStats(fl, self, bouton))
save(fl)
def readFile(self, filename):
f = open(filename, "r")
text = f.read()
return text
def main():
root = Tk()
ex = Example(root)
root.geometry("250x300+100+100")
root.mainloop()
if __name__ == '__main__':
main() |
Partager