如何在page init Python中更改数据

2024-04-26 01:30:33 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个较大的应用程序的片段,用于绘制用户选择的数据。在Page1(未显示)上,用户标识要打印的文本文件。然后,函数transferToPlotPage()将所需的文本文件保存到应用程序范围的变量app_data,并调用下一页PlotPagePlotPage根据数据文件中的值生成的x1y1x2y2生成绘图。你知道吗

注意-plotData()如果我像[1,2],[3,4],[5,6],[7,8]那样传递数据数组,它就会工作。但这些数组与用户选择的数据无关。这些只是硬编码。你知道吗

我打算绘制文本文件数据,而不是硬编码常量。但是,我意识到PlotPage __init__在我有机会让程序知道要使用什么数据文件之前就已经运行了。你知道吗

我试着从transferToPlotPage调用plotData(),但是生成了一个空白图。没有数据显示。我试着用给定的数组调用plotData()x1x2y1y2并观察到一个空白图。你知道吗

我已经验证了数组中存在数据。你知道吗

问题:如何指示程序,使数组在PlotPage的图形上绘制数据?你知道吗

def transferToPlotPage(self,controller,filename1,filename2):
        controller.app_data["filename1"].set(filename1)  # accessible from all classes
        controller.app_data["filename2"].set(filename2)  # accessible from all classes
        ...
        [extract the file data and save to arrays x1, x2, y1, y2 ]
        [ -->> Want to call plotData here <<-- ]
        ...
        controller.show_frame(PlotPage)  # Serve up the data plot

def plotData(self, controller, x1, y1,x2,y2):
        f = Figure(figsize=(7.9,3.4),dpi=100)

        # Setup figure to hold subplots
        f = Figure(figsize=(7.9,3.4), dpi=100)
        # Setup subplots
        subplot1=f.add_subplot(2,1,1)
        subplot1.plot(x1,y1, "-o")
        subplot1.set_title("Plot 1")


        subplot2=f.add_subplot(2,1,2)
        subplot2.plot(x2,y2, "-o")
        subplot2.set_title("Plot 2")

        # Show Plots
        canvas = FigureCanvasTkAgg(f, self)
        canvas.show()
        canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
        toolbar = NavigationToolbar2TkAgg(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

class PlotPage(tk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        # Window title
        tk.Frame.__init__(self, parent)
        plotData(self, controller,x1,y1,x2,y2)

# Load desired page frame (elsewhere in app, but here for reference)
def show_frame(self, container):
    frame = self.frames[container]
    frame.tkraise()

Tags: 数据selfappdata数组frametkcanvas