Python GUI, on saving calculated output

Question:

I am making an application that does some Electrical Calculations. We provide input, the calculations are all done within a class called EarthCalc(Frame), and I have called that class as:

root = Tk()
C = EarthCalc(root)

Now, from that class I’m trying to save some the calculated outputs into a dictionary [defined outside class] so that I may be able to save the output in a notepad. But it is not working!

I have saved Input Data but the Output data is a blank:

def save_data():

    fileName = tkFileDialog.asksaveasfilename(initialfile='Untitled.txt',defaultextension=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt")])
    try:
        file = open(fileName, 'w')
        mydata = {"Conductor Material" : C.conductor_material.get(), "Soil Resistivity" : C.a.get(), "Earth Fault Current" : C.b.get(), "Fault Clearance Time" : C.c.get(), "Electrode Type" : "Pipe", "Electrode Length" : C.d.get(), "Diameter of Pipe" : C.e.get(), "Initial Temperature" : C.f.get(), "Conductor Strip Length" : C.g.get(), "Conductor Strip Width" : C.h.get(), "Conductor Strip Thickness" : C.i.get(), "Earth Grid Burial Depth" : C.j.get(), "Current Division Factor" : C.sf.get(), "Decrement Factor" : C.df.get(), "Surface Layer Resistivity" : C.slr.get(), "Surface Layer Thickness" : C.slt.get(), "Weight Category" : C.weight_category.get()}
        myop = {"Number of Pits" : C.pits}
        #myop = {"Number of Pits" : C.pits, "Protective Conductor Cross Section" : C.cross_section, "Earth Grid Resistance" : C.grid_resistance, "Maximum Grid Current" : C.maximum_grid_current, "Surface Layer Derating Factor" : C.sldf, "Touch Potential Criteria" : C.tpc, "Step Potential Criteria" : C.spc, "Ground Potential Rise" : C.gpr, "Grid Area" : C.area1}
        #textoutput = "Input Data nConductor Material: %s nSoil Resistivity: %s nEarth Fault Time: %s nFault Clearance Time: %s nLength of Pipe: %s nDiameter of Pipe: %s nInitial Temperature: %s nConductor Strip Length: %s nConductor Strip Width: %s nConductor Strip Thickness: %s nGrid Burial Depth: %s nCurrent Division Factor: %s nDecrement Factor: %s nSurface Layer Resistivity: %s nSurface Layer Height: %s nWeight Category: %s nnnOutput Data " %self.conductor_material.get(), %self.a.get(), %self.b.grt(), %self.c.get(), %self.d.get(), %self.e.get(), %self.f.get(), %self.g.get(), %self.h.get(), %self.i.get(), %self.j.get(), %self.sf.get(), %self.df.get(), %self.slr.get(), %self.slt.get(), %self.weight_category.get()
        file.write("Input Datann")
        for line in mydata:
            file.write(line + ": " + mydata[line] + "n")
        file.write("nnOutput Data nn")
        file.write(myop["Number of Pits"])
        #for item in myop:
            #file.write(myop[item])

Help me resolve this. Please

Asked By: Haarsh Dubey

||

Answers:

There are a few potential problems with your code. If you are using Python 2.x you should avoid the variable name “file” since it is also a pre-defined class, it will work in this short snippet, but might give you weird bugs in other cases. The name file is fine in Python3 though.

It is a bit problematic that you don’t show what is in your except block since a crash (for example if Number of Pits is a number.write will return a TypeError) which in turn might lead to the program crashing before the data have actually been flushed to disk.

I would probably write it like this:

def save_data():
   fileName = tkFileDialog.asksaveasfilename(initialfile='Untitled.txt',defaultex»tension=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt")])

   try:
      with open(fileName,'w') as f:
         mydata = {"Conductor Material" :...
         myop = {"Number of Pits" : C.pits}
         f.write("Input Datann")
         for line,value in mydata.iteritems():
            f.write("{0} : {1}n".format(line, value ))
         f.write("nnOutput Data nn")
         f.write(str(myop["Number of Pits"]))
   except IOError as e:
      print "Error opening or writing {0}: {1}".format(fileName,e.message)
Answered By: Zinob
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.