在控制器/视图架构中获取/设置Tkinter变量-属性错误

时间:2019-02-07 21:24:13

标签: python model-view-controller tkinter

该问题针对的是熟悉“ psudeo-mvc”代码以及与布莱恩·奥克利(Bryan Oakley)的Tkinter UI示例here相关的问题的人–不幸的是,我没有足够的声誉来简单地评论现有问题

我已经阅读了原始问题的所有链接,但仍然无法弄清楚我的代码出了什么问题。我看到的唯一区别是,我将不同的“视图”类HomeWindow, OutfileWindow, TestWindow放在了单独的python文件中,因此将它们导入控制器中,以便可以看到它们-显然我遗漏了一些东西...所以问题是:

在我的两个视图类HomeWindowOutfileWindow中,我试图在shared_data字典中获取和设置值。为什么我在AttributeError变量上得到shared_data

这是相关的控制器代码:

import Tkinter as T        
import HomeWindow
import OutfileWindow
import TestWindow

## This class inherits from Tkinter.Tk class.
class Controller(T.Tk):

    ## constructor for our PSSE Plotting Application
    def __init__(self, *args, **kwargs):

        T.Tk.__init__(self, *args, **kwargs)
        T.Tk.title(self, "My Application")
        self.configure(background = COLOR_WINDOW)

        ## Build a container frame within the root TK window that will hold all other widgets.
        containerFrame = T.Frame(self)
        containerFrame.configure(bg=COLOR_FRAME)
        containerFrame.grid(row=0,column=0,sticky='nsew')
        containerFrame.grid_rowconfigure(0, weight=1)
        containerFrame.grid_columnconfigure(0, weight=1)

        ## build a dictionary of windows (tk frames) hashed by each frame's __name__ variable
        windows = (   HomeWindow.HomeWindow, 
                      OutfileWindow.OutfileWindow, 
                      TestWindow.TestWindow            )

        ## list of Frame Class __name__ variables which will act as keys
        self.windowKeys = []
        ## dictionary Frame Class Objects
        self.windowDictionary = {}
        for frameClass in windows:
            windowKey = frameClass.__name__
            self.windowKeys.append(windowKey)
            window = frameClass(parent=containerFrame, controller=self)
            self.windowDictionary[windowKey] = window
            window.grid(row=0, column=0, sticky="nsew")

        ## Call up the main window.
        self.show_window("HomeWindow")

        ## ---------------------------------------------------------------------------
        ## dictionary of data that is accessible to all classes via the controller 
        self.shared_data = {
            # file management
            "cwd"              :  T.StringVar(),
            "cwdSet"           :  T.BooleanVar(),
            "allOutfiles"      :  [],
            "selectedOutfiles" :  [],
            "allCsvfiles"      :  [],
            "selectedCsvfiles" :  [],
            # data managment
            "allChannels"      :  [],
            "selectedChannels" :  [],  
            "allColumns"       :  [],
            "selectedColumns"  :  [],
            # plot(s) management
            "plot_titles"      :  [],
            "xlabels"          :  [],
            "ylabels"          :  [],
            # Room for more....
            "test"             :  "Hello World"
        }

    def show_window(self, windowKey):
        window = self.windowDict[windowKey]
        window.tkraise()
        window.refresh()

在这里,当我尝试访问/使用shared_data中包含的值时会出现错误:

class HomeWindow(T.Frame):

    def __init__(self, parent, controller):
        T.Frame.__init__(self,parent)
        self.controller = controller
        print self.controller.shared_data["test"]

再次在这里:

class OutfileWindow(T.Frame):
    def __init__(self, parent, controller):
        T.Frame.__init__(self,parent)
        self.controller = controller
        self.controller.shared_data["cwd"].set("Specify a directory...")
        self.controller.shared_data["cwdSet"].set(False)

这是第一个实例的错误:(第二个是相同的错误和变量)

> python27 Controller.py
  Traceback (most recent call last):
  File "Controller.py", line 211, in <module>
    main()
  File "Controller.py", line 199, in main
    PlottingApplication = Controller()
  File "Controller.py", line 71, in __init__
    window = frameClass(parent=containerFrame, controller=self)
  File "C:\Users\user\Documents\004_MyTools\005_Plotter\HomeWindow.py", line 37, in __init__
    print self.controller.shared_data["test"]
  File "C:\Program Files (x86)\Python27\lib\lib-tk\Tkinter.py", line 1903, in __getattr__
    return getattr(self.tk, attr)
  AttributeError: shared_data

0 个答案:

没有答案