我正在使用python 2.7创建一个程序,使用Tkinter作为GUI,以及elementtree,其中用户输入的数据存储在XML文件中。我已经设法使用python创建XML文件,并将其保存到文件'crimeFile.xml',包含所有空标记。 GUI还显示允许用户输入文本的字段和文本框。问题是当我尝试保存一个条目时,我收到一个错误,并且数据没有保存到XML文件中。
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()
我得到的错误是:
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'
程序的目的是创建一个XML文件供用户使用,以供多个条目使用。更新并将新条目存储在此XML文件中(不覆盖以前的文件),以便以后可以搜索它并生成报告。 我是python的新手,不知道我哪里出错了?
答案 0 :(得分:1)
您的应用程序实例没有名为crimeReport
的任何属性
def save_XML(self):
record = ET.SubElement(self.crimeReport, 'crime report')
^
crimeReport
仅在您的create_XML
方法中定义。您可以将crimeReport
定义为类属性。
class Application(Frame):
crimeReport = Element('crime report')
并在您的方法中使用self.crimeReport