0

This question is directed at anyone who is familiar with the "psudeo-mvc" code and questions related to Bryan Oakley's Tkinter UI example here -- Unfortunately I do not have reputation enough to simply comment on an existing question.

I have read all the links off of the original question, and still cannot figure out what is wrong with my code. The only difference that I see is that I put my different "view" classes HomeWindow, OutfileWindow, TestWindow in separate python files, and am therefore importing them into the controller so it can see them -- obviously I am missing something... So the question is:

In two of my view classes HomeWindow and OutfileWindow I am trying to both get and set values in the shared_data dictionary. Why am I getting AttributeError on the shared_data variable?

Here is the relevant controller code:

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()

And here is where I get errors when I try to access/use the values contained within 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"]

And Again Here:

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)

Here is the error for the first instance: (the second is the same error and variable)

> 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
wassup_doc
  • 21
  • 1
  • 5
  • Please [edit] your question to include the error message. – Bryan Oakley Feb 07 '19 at 22:17
  • hi @BryanOakley, thanks for taking a look. edit complete. – wassup_doc Feb 07 '19 at 22:54
  • It seems pretty clear that you're creating the windows before you initialize the shared data object. Have you tried initializing `shared_data` before creating the windows? – Bryan Oakley Feb 07 '19 at 23:12
  • OMG. that's it... the windows don't know about `shared_data`. Moving `shared_data` intialization up above the window instantiations fixes the error. Can't believe I didn't catch that. Thanks @BryanOakley. – wassup_doc Feb 07 '19 at 23:24

0 Answers0