基于在中使用Tkinter创建的按钮单击删除xml属性+子元素

2024-09-29 17:16:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python方面有个问题,也许有专家可以帮忙。你知道吗

我有一个XML文件,我解析它,用Xpath提取一些“标记”,然后创建一个Tkinter接口,在该接口中为找到的每个属性创建按钮(这就完成了)。现在我想做的是,只要单击任何按钮(数量取决于找到的属性),XML中选择的属性将在另一个节点(具有相同名称)中删除+旁边的所有元素。你知道吗

比工作更好这是我的代码:

from Tkinter import *
from tkFileDialog import askopenfilename
import xml.etree.ElementTree as xml


y = 0
' '
Tk().withdraw() 
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file


#Getting attribute to be delete in the xml
def getValues(tree, category):
    parent = tree.find("./body/hierarchy[@name='%s']" % category)
    return [child.get('name') for child in parent]
tree = xml.parse(filename)
val = getValues(tree, 'MainHierarchy')

==> here is the function which is not working
def delete():

def callback():


    if tree.findall("./body/objects/object/ORIGIN[@value='%s']" %   btn_dict[nodeval]): 
    root.remove("./body/objects/object/")
==>  Here is the function to be created which will delete the tag selected + all sub element beside which can be click and give the user choise to save the new file and select the new name<==


root = Tk()
#Creation of button with the dictionary 
btn_dict = {}
col = 0 
for nodeval in val:
    btn_dict[nodeval] = Button(root, text=nodeval , command=delete) 
    btn_dict[nodeval].grid(row=0, column=col, pady=10)
    col += 1 
# run the GUI event loop
root.mainloop()

我的xml示例是:

<body>
  <object>
        <objects id="123" instanceof="screw">
            <origin = "M015" />
            <weight = "100/>
            ...
        <objects id="45" instanceof="nut">  
            <origin = "4512" />
            <size = "72" />
            ...
        <objects id="66" instanceof="nut">  
            <origin = "M015" /> 
            ...


  <hierarchy>
      <session name= "M015" />
      <session name= "4512" />

工具将创建两个按钮,一个用于“M015”,一个用于“4512”,如果我单击按钮“M015”,宏将在所有对象中找到。在这种情况下,id“123”应与所有子元素一起删除,id“66”也应删除。你知道吗

我希望这是清楚的,任何帮助都会非常感激。你知道吗


Tags: thetonameidtreeobjectsbodyroot

热门问题