-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyFile.py
More file actions
179 lines (140 loc) · 6.19 KB
/
pyFile.py
File metadata and controls
179 lines (140 loc) · 6.19 KB
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
import os
from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
class Filebrowser:
def __init__(self):
"""
Luokkaan määritelty ohjelman käyttöliittymän elementit
ja nappien funktiot.
"""
self.__rootfolder = "/"
self.__pathtext = "Current path: "
self.__location = self.__rootfolder
self.__window = Tk()
self.__window.title("File browser")
self.__window.columnconfigure(1, weight=1)
self.__window.columnconfigure(1, weight=1)
for i in range(6, 31):
self.__window.rowconfigure(i, weight=1)
self.__pathlabel = Label(text=self.__pathtext + self.__location)
self.__pathlabel.grid(row=0, column=0, columnspan=2, pady=2, sticky=NW)
self.__open_button = Button(text="Open", command=self.open, width=8)
self.__open_button.grid(row=1, column=0, pady=1, padx=2)
self.__back_button = Button(text="Back", command=self.back, width=8,
state="disabled")
self.__back_button.grid(row=2, column=0, pady=1)
self.__makefolder_button = Button(text="New Folder",
width=8, command=self.makefolder)
self.__makefolder_button.grid(row=3, column=0, pady=1)
self.__deletebutton = Button(text="Delete", width=8,
command=self.delete_file)
self.__deletebutton.grid(row=4, column=0, pady=1)
self.__lstbx = Listbox(self.__window, width=60, height=25,
active="dotbox")
self.__lstbx.grid(row=1, column=1, rowspan=30, padx=1, pady=0, sticky=NSEW)
self.__lstbx.bind("<Double-Button>", lambda x: self.__open_button.invoke())
for i in os.listdir(self.__location):
self.__lstbx.insert(END, i)
def open(self):
"""
Open-napin ja kaksoisnäpäytyksen funktio joka avaa valitun kansion sisällön näytettäväksi
listboxissa. Näyttää myös virheikkunan mikäli käyttäjä
yrittää avata jonkin muun tiedoston kuin kansion
tai kansion johon hänellä ei ole pääsyoikeutta.
"""
goto = self.__lstbx.get(ACTIVE)
if self.__location != self.__rootfolder:
self.__location += ("/" + goto)
else:
self.__location += goto
self.__pathlabel.configure(text=self.__pathtext + self.__location)
self.__back_button.configure(state="active")
try:
self.refresh()
except NotADirectoryError:
self.back()
messagebox.showerror("Error",
"Choose a directory, not a file.")
except PermissionError:
self.back()
messagebox.showerror("Error",
"You do not have permission"
" to view this folder.")
def back(self):
# Back-napin funktio joka palauttaa
# listboxin sisällöksi edellisen kansion.
if self.__location != self.__rootfolder:
newpath = self.__location.split("/")
newpath.remove(newpath[-1])
path = "/".join(newpath)
if path == "":
path = self.__rootfolder
self.__back_button.configure(state="disabled")
self.__location = path
self.__pathlabel.configure(text=self.__pathtext + self.__location)
else:
self.__back_button.configure(state="disabled")
self.refresh()
def makefolder(self):
# New folder-napin funktio. Avaa uuden ikkunan
# jossa pyydetään käyttäjältä uuden kansion nimeä
# jonka jälkeen uusi kansio luodaan nykyisen kansion sisälle.
inputwindow = Tk()
inputwindow.withdraw()
user_input = simpledialog.askstring(
title="Name your folder",
prompt="Create new folder in {}".format(self.__location))
if user_input:
if self.__location != self.__rootfolder:
new_folder_path = self.__location + "/" + user_input
else:
new_folder_path = self.__location + user_input
try:
os.mkdir(new_folder_path)
except PermissionError:
messagebox.showerror("Error",
"You do not have permission"
" to make a folder in this directory.")
self.refresh()
def delete_file(self):
# Poistaa valitun tiedoston tai tyhjän kansion.
# Kysyy ensin käyttäjältä varmistusta.
to_delete = self.__lstbx.get(ACTIVE)
confirmation_window = messagebox.askyesno("Delete file",
"Delete {}?"
.format(to_delete))
if self.__location != self.__rootfolder:
deletion_path = self.__location + "/" + to_delete
else:
deletion_path = self.__rootfolder + to_delete
if confirmation_window:
try:
os.rmdir(deletion_path)
except NotADirectoryError:
os.remove(deletion_path)
except OSError:
messagebox.showerror(
"Folder not empty",
"Remove all files from {} before deleting"
.format(to_delete))
self.refresh()
def refresh(self):
# Päivittää Listboxin sisällön self.__path muuttujan mukaiseksi.
# ja muuttaa open ja delete nappien tilaa mikäli
# kansiossa on tai ei ole tiedostoja.
directory_contents = os.listdir(self.__location)
self.__lstbx.delete(0, END)
for i in directory_contents:
self.__lstbx.insert(END, i)
if len(directory_contents) == 0:
self.__open_button.configure(state="disabled")
self.__deletebutton.configure(state="disabled")
else:
self.__open_button.configure(state="active")
self.__deletebutton.configure(state="active")
def start(self):
# Käynnistää käyttöliittymän.
self.__window.mainloop()
ui = Filebrowser()
ui.start()