I have data in x
and y
arrays that I want to pass between two Python classes. When button
is clicked I run command=lambda: controller.show_frame(PlotPage)
to switch from SelectPage
(which chooses data) to PlotPage
(which plots x and y). I want x
and y
saved before the page switch or within the button
lambda. Is saving the arrays as global variables the best way to pass the data to PlotPage
, or is there a more convenient way to include these arrays in the button lambda function?
# possible global variables
global x = [stuff x]
global y = [stuff y]
class SelectPage(tk.Frame):
def __init__(self,parent,controller):
button = tk.Button(self,text="Plot",
command=lambda: controller.show_frame(PlotPage),
[some_lambda_here]) # Possible lambda addition
class PlotPage(tk.Frame):
def __init__(self,parent,controller):
[Tkinter plot intialization stuff]
plotData(x,y) # plotData creates the plot
Controller Class:
class Project:
def __init__(self, *args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
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 (SelectPage, PlotPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0,column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, container):
frame = self.frames[container]
frame.tkraise()