简体   繁体   中英

Open XML file in Python and add user input

I am creating a program with python 2.7, using Tkinter as the GUI, and elementtree, where the data entered by the user is stored in an XML file. I have managed to create the XML file using python and this saves to the file 'crimeFile.xml', with all empty tags. The GUI also displays the fields and text boxes that allow the user to enter in text. The problem is that when I try and save an entry, I get an error, and the data is not saved to the XML file.

from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
import xml.etree.ElementTree as ET
from Tkinter import *

class Application(Frame):

    def create_XML(self):
        crimeReport = Element('crime report')
        caseNo = SubElement(crimeReport, 'case number')

        victimDetails = SubElement(caseNo, 'victim details')
        victimFirstName = SubElement(victimDetails, 'victims first name')
        victimSecondName = SubElement(victimDetails, 'victim surname')
        victimAddress = SubElement(victimDetails, 'victim address')

        output_file = open('crimeFile.xml', 'w')
        output_file.write('<?xml version="1.0"?>')
        output_file.write(ElementTree.tostring(crimeReport))
        output_file.close()

    def save_XML(self):

        record = ET.SubElement(self.crimeReport, 'crime report')      
        caseNoNode = ET.SubElement(record, 'case number')
        caseNoNode.text = self.caseNo.get()
        victimFirstNameNode = ET.SubElement(record, 'first name')
        victimFirstNameNode.text = self.victimFirstName.get()
        victimSecondNameNode = ET.SubElement(record, 'surname')
        victimSecondNameNode.text = self.victimSecondName.get()
        victimAddressNode = ET.SubElement(record, 'address')
        victimAddressNode.text = self.victimAddress.get()

        self.tree.write('crimeReport.xml')
        self.clear_field()


    def create_Widgets(self):
        self.save = Button(self)
        self.save["text"] = "Save",
        self.save["command"] = self.save_XML
        self.save["bg"]   = "cyan"
        self.save.grid(row=0,column =2,sticky=W+E+N+S)

        self.crimeReportLabel = Label(self, text = 'Crime Report')
        self.crimeReportLabel.grid(row=1,column =1,sticky=W)

        self.caseNoLabel = Label(self,text="Case Number")
        self.caseNoLabel.grid(row=2,column =1,sticky=W)
        self.caseNo = Entry(self)
        self.caseNo.grid(row=2,column =2,sticky=W)

        self.victimDetailsLabel = Label(self,text="Victim Details")
        self.victimDetailsLabel.grid(row=3,column =1,sticky=W)

        self.victimFirstNameLabel = Label(self,text="First Name")
        self.victimFirstNameLabel.grid(row=4,column =1,sticky=W)
        self.victimFirstName = Entry(self)
        self.victimFirstName.grid(row=4,column =2,sticky=W)

        self.victimSecondNameLabel = Label(self,text="Surname")
        self.victimSecondNameLabel.grid(row=4,column =3,sticky=W)
        self.victimSecondName = Entry(self)
        self.victimSecondName.grid(row=4,column =4,sticky=W)

        self.victimAddressLabel = Label(self,text="Address")
        self.victimAddressLabel.grid(row=6,column =1,sticky=W)
        self.victimAddress = Entry(self)
        self.victimAddress.grid(row=6,column =2,sticky=W)

    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.grid(column=5,row=25)
        self.create_Widgets()
        self.create_XML()


crimeReport = Tk()
app = Application(master = crimeReport)
app.mainloop()
crimeReport.destroy()

The error that I get is:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args)
  File "C:\Users\Laleh\workspace\AdvDB20-02\program1\program1.py", line 60,  in save_XML
    record = ET.SubElement(self.crimeReport, 'crime report')
AttributeError: Application instance has no attribute 'crimeReport'

What the program is meant to do is create an XML file to use, for multiple entries by the user. Update and store the new entries in this XML file (not overriding the previous ones), so that later it can be searched and a report produced. I'm a newbie to python and am not sure where I am going wrong?

Your application instance doesn't have any attribute named crimeReport

def save_XML(self):
    record = ET.SubElement(self.crimeReport, 'crime report')
                           ^

crimeReport is only defined in your create_XML method. You can define crimeReport as class attribute instead.

class Application(Frame):
    crimeReport = Element('crime report')

and use self.crimeReport in your methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM