@@ -14,6 +14,7 @@ def __init__(self, master):
14
14
self .frames = [Frame (self ) for _ in range (3 )]
15
15
self .selectButton = Button (self .frames [0 ], text = 'Select File' , command = self .select )
16
16
self .convertButton = Button (self .frames [0 ], text = 'Convert' , command = self .convert )
17
+ self .revertButton = Button (self .frames [0 ], text = 'Revert' , command = self .revert )
17
18
self .fileLabel = Label (self .frames [1 ], text = 'File: ' )
18
19
self .filenameLabel = Label (self .frames [1 ])
19
20
self .toLabel = Label (self .frames [1 ], text = 'Format: ' )
@@ -25,30 +26,52 @@ def __init__(self, master):
25
26
self .toEntry .select_range (0 , END )
26
27
for i , w in enumerate (self .frames ):
27
28
w .grid (row = i , column = 0 , sticky = NSEW )
28
- for i , (w1 , w2 , w3 ) in enumerate (zip ([self .selectButton , self .convertButton ],
29
- [self .fileLabel , self .filenameLabel ],
30
- [self .toLabel , self .toEntry ])):
31
- w1 .grid (row = 0 , column = i , sticky = NSEW )
32
- w2 .grid (row = 0 , column = i )
33
- w3 .grid (row = 1 , column = i , sticky = NSEW )
29
+ for i , w in enumerate ([self .selectButton , self .convertButton , self .revertButton ]):
30
+ w .grid (row = 0 , column = i , sticky = NSEW )
31
+ for i , (w1 , w2 ) in enumerate (zip ([self .fileLabel , self .filenameLabel ],
32
+ [self .toLabel , self .toEntry ])):
33
+ w1 .grid (row = 0 , column = i )
34
+ w2 .grid (row = 1 , column = i , sticky = NSEW )
35
+ self .lastConversion = ()
34
36
35
37
def select (self ):
36
38
filename = filedialog .askopenfilename (initialdir = self .settings .setdefault (LAST_DIR , '' ), \
37
39
title = 'Select a File' )
38
40
if not filename : return
39
41
self .settings [LAST_DIR ] = os .path .dirname (filename )
40
42
self .filenameLabel ['text' ] = filename
43
+ self .lastConversion = ()
41
44
42
45
def convert (self ):
43
46
filename = self .filenameLabel ['text' ]
44
47
fileFormat = self .toEntry .get ()
45
- if not filename or not fileFormat : return
46
48
if fileFormat .startswith ('.' ):
47
49
fileFormat = fileFormat [1 :]
48
- if not fileFormat : return
50
+ if not filename or not fileFormat :
51
+ messagebox .showerror (title = 'Error' , message = 'Nothing to Convert!' )
52
+ return
49
53
self .settings [LAST_FORMAT ] = fileFormat
50
- os .rename (filename , filename [:filename .find ('.' ) + 1 ] + fileFormat )
51
- messagebox .showinfo (title = 'Finished' , message = 'Done!' )
54
+ dot = filename .find ('.' )
55
+ newFileName = filename + '.' + fileFormat if dot == - 1 else filename [:dot + 1 ] + fileFormat
56
+ self .__rename (filename , newFileName , 'Renaming Done!' )
57
+
58
+ def revert (self ):
59
+ if not self .lastConversion :
60
+ messagebox .showerror (title = 'Error' , message = 'Nothing to Revert!' )
61
+ return
62
+ fromFile , toFile = self .lastConversion
63
+ self .__rename (toFile , fromFile , 'Reversion Done!' )
64
+
65
+ def __rename (self , fromFile , toFile , doneMessage ):
66
+ try :
67
+ os .rename (fromFile , toFile )
68
+ except :
69
+ messagebox .showerror (title = 'Error' , message = 'File not found!' )
70
+ return False
71
+ self .filenameLabel ['text' ] = toFile
72
+ self .lastConversion = (fromFile , toFile )
73
+ messagebox .showinfo (title = 'Finished' , message = doneMessage )
74
+ return True
52
75
53
76
root = Tk ()
54
77
app = Converter (root )
0 commit comments