두 파이썬 클래스 사이에 전달하고자하는 데이터는 x
과 y
입니다. button
을 클릭하면 command=lambda: controller.show_frame(PlotPage)
을 실행하여 SelectPage
(데이터 선택)에서 PlotPage
(x 및 y를 표시)으로 전환합니다. 페이지 전환 전에 또는 button
람다 내에 x
및 y
을 저장하고 싶습니다. 데이터를 PlotPage
으로 전달하는 가장 좋은 방법은 배열을 전역 변수로 저장하는 것입니까? 아니면이 배열을 버튼 람다 함수에 포함시키는 더 편리한 방법이 있습니까?파이썬 클래스 사이에 배열 전달하기
# 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
컨트롤러 클래스 :
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()
컨트롤러의 클래스 란 무엇입니까? – daragua
데이터를 포함하고'Frame' 객체에 의해 공유되는'Page' 객체를 갖는 것이 합리적이라고 생각합니다. –
@daragua 컨트롤러 클래스를 보여주기 위해 코드가 업데이트되었습니다. –