I've been looking for a solution to my problem too long. I've reviewed all the pages that are proposed in the following question, exhaustively: Switch between two frames in tkinter, I have documented myself reviewing information from classes on the official python page, I've been reading about self, in case I'm not considering something; I have also gone through more pages, questions and solutions that I have come across along the way and rearranged my code several times, but I cannot find the solution and need help.
The code, as simplified as possible, is the following:
import os, easygui
import xml.etree.ElementTree as ET
import tkinter as tk
LARGE_FONT = ("Verdana", 12)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
print("Second TC:", self.controller.shared_data['tc'])
for i in self.controller.shared_data['tc']:
print("****: ", i)
print("Name:", i['Name'], "Id:", i['Id'])
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text='File Upload: Press the button to open the XML file.', font=LARGE_FONT)
label.place(relx=.01, rely=.01)
# Function to separate the tc "Child_4" inside the xml file
def separate_tc(xml_name):
file_xml = ET.parse(xml_name)
# Get the name and id attributes that the Child_4 tag contains
self.controller.shared_data['tc'] = [
{"Name": signal.attrib["Name"],
"Id": signal.attrib["Id"],
} for signal in file_xml.findall(".//Child_4")
]
print('First TC:', self.controller.shared_data['tc'])
controller.show_frame("PageOne")
# Function to open xml file
def open_file():
try:
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)):
separate_tc(os.path.basename(str(xml_name)))
else:
separate_tc(os.path.basename(str(xml_name)))
except FileNotFoundError:
print('XML file was not loaded.')
button_open = tk.Button(self, text="Open File XML", command=open_file)
button_open.place(relx=.01, rely=.05)
class AppMain(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, 'Analysis of a XML')
self.shared_data = {'tc': []}
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
if __name__ == "__main__":
app = AppMain(None)
app.title('Analysis of XML')
app.geometry("1024x920")
app.mainloop()
What I want to do is obtain the value of tc_xml
and that is why I am assigning it in the variable controller.shared_data ['tc']
in the Home
class, as I think I understand from the response of Storing data in the controller, but at the time of sending calling the variable in the PageOne
class, brings it to me empty and I don't understand why. If someone could help me I would be very grateful, please.