'Event'对象没有属性'controller' - 函数无法访问类的参数作为Event对象

时间:2016-07-18 16:58:03

标签: python python-3.x matplotlib tkinter tkinter-canvas

现在,一旦输入了条目,我需要一个类(GraphPage)来访问其他类的参数(StartPage)。因此,我已将update中的GraphPage功能绑定到StartPage中的一个事件。但是,更新功能需要访问两个帧的controller以获取条目输入。但现在更新功能被视为一个事件,无法访问其类的任何属性。我该如何工作?

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from lmfit.models import ExponentialGaussianModel
from lmfit import Parameters
from tkinter import messagebox
import matplotlib.pyplot as plt
from numpy import loadtxt
import math

import tkinter as tk
from tkinter import ttk


class PeakFitting(tk.Tk):

    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 (StartPage, GraphPage):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]

        frame.tkraise()

    def get_page(self, classname):
        '''Returns an instance of a page given it's class name as a string'''
        for page in self.frames.values():
            if str(page.__class__.__name__) == classname:
                return page
        return None

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.controller = controller

       # Setting up frame and widget
        label_iso = tk.Label(self, text="Isotope A, Element (ex: 133,Cs)"
        label_iso.grid(row=4, column=0)

        self.entry1 = tk.Entry(self)
        self.entry1.grid(row=4, column=1)

        button3 = ttk.Button(self, text="Graph Page",
                     command=lambda: controller.show_frame(GraphPage))
        self.button3.bind("<Button-1>", GraphPage.update)
        button3.grid(row=7, columnspan=2)

class GraphPage(tk.Frame):
    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        self.controller = controller

        fig = Figure(figsize=(5, 5), dpi=100)
        self.a = fig.add_subplot()

        canvas = FigureCanvasTkAgg(fig, 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)

        startpage = self.controller.get_page("StartPage")


    def update(self):

        startpage = self.controller.get_page("StartPage")
        self.iso = startpage.entry1.get()

##Calculation code for analysis not needed for question

app = PeakFitting()
app.mainloop()

错误'事件'对象没有属性'controller'位于update

GraphPage函数的第一行

谢谢!!!

0 个答案:

没有答案