从生成一组按钮的函数中单独读取标签

Read labels separately from a function that generates a set of buttons

我正在使用 Python 中的 Tkinter、ElementTree 和 Pandas 模块进行以下开发:

    from tkinter import *
    import xml.etree.ElementTree as ET
    import pandas as pd
    
    file_xml = ET.parse('example1.xml')
    rootXML = file_xml.getroot()

    root = Tk()
    root.title("Graphical Analysis of Signals of a XML")
    root.geometry("1024x820")
    root.pack_propagate(False)
    root.config(bd=15)

    # Functions to open xml file
    def open_file():
       try:
          global temxml, xml_Name
          xml_Name = str(easygui.fileopenbox(title='Select xml file', default='*.xml'))
          if str(os.path.abspath(xml_Name)) != os.path.join(os.path.abspath(os.getcwd()), os.path.basename(xml_Name)):
              menssage.set("Opened xml file: ", xml_Name)
              child_4_separate(os.path.basename(str(tempxml)))
          else:
              child_4_separate(os.path.basename(str(xml_Name)))
       except FileNotFoundError:
           print('XML file was not loaded.')
    
    # Function to display buttons and choose the Child_4 to be plotted
    def child_4_separate(xml_Name):
        print("File: ", xml_Name)
        file_xml = ET.parse(xml_Name)
        data_xml = [
            {
                "Name": signal.attrib["Name"],
                "Id": signal.attrib["Id"],
            } for signal in file_xml.findall(".//Child_4")
        ]
        # print(data_xml)
    
        for i in data_xml:
           print(i)
           id_tc = i.get('Id')
           dict_tc = str(i.values()).replace('dict_values([\'', '')
           name_tc = dict_tc.replace('\'])', '')
           Button(root, text=f"TC> {name_tc}", command=transfor_data_atri_child_4(xml_Name, id_tc)).pack()

    # Function to transform xml file to DataFrame
    def transfor_data_atri_child_4(rootXML, id_tc):
        print("File: ", rootXML)
        print("id_tc: ", id_tc)

我想做的是,每次我单击一个按钮时,child_4_separate (xml_Name) 函数都会转到带有单个 ID 号的 transform_data_atri_child_4 (rootXML, id_tc) 函数,并且不会像它在我下面显示的最后一张印刷品中确实如此,这是为了能够单独操作它们。

我在这个 link example1.xml 中分享 XML 文件,因为它有多长。

我不知道我是否需要另一个 for inside 或我需要什么,因为在 child_4_separate (xml_Name) 函数中尝试另一个 for inside 已经存在的 for 时它重复了很多次而且它不是什么我想要,但只是使用我指示的两个参数重定向到以下函数,但分别;请帮帮我!在此之前,非常感谢!

为了以后可能的搜索或者相关问题,我把解决方法分享一下:

在按钮属性中取值command = lambda x = xml_Name, y = id_tc: transform_data_atri_child_4 (x, y)就可以了,我的函数是这样的:

    # Function to display buttons and choose the Child_4 to be plotted
    def child_4_separate(xml_Name):
        # print("File: ", xml_Name)
        file_xml = ET.parse(xml_Name)
        data_xml = [
            {
               "Name": signal.attrib["Name"],
               "Id": signal.attrib["Id"],
            } for signal in file_xml.findall(".//Child_4")
        ]
        # print(data_xml)

       for i in data_xml:
          id_tc = i.get('Id')
          dict_tc = str(i.values()).replace('dict_values([\'', '')
          name_tc = dict_tc.replace('\'])', '')
          Button(root, text=f"TC> {name_tc}", command=lambda x=xml_Name, y=id_tc: transfor_data_atri_child_4(x, y)).pack()

我很欣赏@Sujay 的解决方案。大家编纂快乐!!