Yes, my first post on this forum, and I come to you as an inexperienced Python coder. Brief background on myself - I'm a hobby programmer, learnt GW & QBASIC back in the day, tried early VB, then kinda left it. Decided to get back into hobby programming, looked at all the languages available, and chose Python. Got a good book, been reading it for the last year on and off, a lot of it makes a lot of sense (especially as my OO experience was zero).
Anyway, onto the issue I've got now. I'm writing a utility program for work, which will take a CSV file in, re-arrange it, and write a new one out. This is currently done by an Excel macro, which is a bit naff as Excel has a funny habit of applying it's own "style" to the file. I've already written a working program that does it from the CLI (well, 2 actually - one OO-based, and one not). My issue is, that it needs a GUI really, as I won't be the one running it in real life. So I've opted for Tkinter, as it's included with Python, so only one install required on the destination machines. Anyway, the code for the program is here:
import csv, sys, datetime __metaclass__ = type def arranger(a): b =  criteria = (15,1,0,1,3,4,10,9,7,5,0,0,6,14) for i in criteria: b.append(a[i]) return b class Fileobject(list): def reader(self,x): self.filename = open(sys.argv,'r') self.readeratt = csv.reader(self.filename) for row in self.readeratt: rowaslist = list(row) x.outputrow = arranger(rowaslist) print x.outputrow x.writer() def writer(self): self.filename = open(self.outputfilename,'a') writeratt = csv.writer(self.filename) writeratt.writerow(self.outputrow) self.filename.close() outputnamedate = datetime.date(2009,10,14) inputfile = Fileobject() outputfile = Fileobject() outputfile.outputfilename = 'Supplier Import File ' + str(outputnamedate.today()) + '.csv' inputfile.reader(outputfile) print outputfile.outputrow outputfile.writer() try: inputfile.readeratt.next() except StopIteration: print "Re-formatting successful" inputfile.filename.close() sys.exit()
It's not ideal at present, as I need to include more exception handling (as it stands, if someone tries to import a non-CSV file, it'll crash in an ugly Python way. Well, I quite like it actually ). I've got the Tkinter part started, based on a simple tutorial - it's going to have 3 buttons: a load, a re-format, and a quit button. My big question, is how do I link the 2? So, when the user clicks the "re-format" button, it executes the program I've already written? Is it something to do with the 'mainloop()' part of the Tkinter program?