2つのPythonクラスの間で渡したいデータをx
とy
の配列にします。 button
をクリックするとcommand=lambda: controller.show_frame(PlotPage)
を実行してSelectPage
(データを選択)からPlotPage
(xとyをプロット)に切り替わります。ページスイッチの前、またはbutton
ラムダ内には、x
とy
が保存されています。グローバル変数として配列を保存して、データをPlotPage
に渡す最善の方法か、これらの配列をボタンのラムダ関数に含める方が便利ですか?Pythonクラス間で配列を渡す
# 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コントローラークラスを表示するようにコードが更新されました –