Parakoopa
Parakoopa

Reputation: 525

Shared Data between Frames Tkinter

I was hoping someone could tell me where I am going wrong. I would love to be able to access this shared_Data dictionary from any one of my classes, however I get the error "AttributeError: '_tkinter.tkapp' object has no attribute 'shared_Data'" when I run script. I am trying to implement what is described in this answer https://stackoverflow.com/a/33650527/7336464

import tkinter as tk
from tkinter import ttk

LARGE_FONT = ("Verdana", 20)

class APTInventoryapp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self, default='barcode_scanner.ico')
        tk.Tk.title(self, 'Inventory Scanning')
        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, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='nsew')

        self.shared_Data = {'String1': tk.StringVar(),
                            'String2' : tk.StringVar(),
                            'number1' : tk.IntVar()
            }

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()    

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        upcOutputLabel = ttk.Label(self, text = self.controller.shared_Data['String1'].get(), font=LARGE_FONT)
        upcOutputLabel.pack(pady=10,padx=10)
        upcEntryField = ttk.Entry(self)
        upcEntryField.focus()
        upcEntryField.pack()
        upcEnterButton = ttk.Button(self, text = 'Enter', command=print('Enter Clicked!'))
        upcEnterButton.pack()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        mgrOutputLabel = ttk.Label(self, text = '', font=LARGE_FONT)
        mgrOutputLabel.pack(pady=10,padx=10)
        mgrbackButton = ttk.Button(self,text = 'Back to home', command=lambda: controller.show_frame(StartPage))
        mgrbackButton.pack()

app = APTInventoryapp()
app.mainloop()

Upvotes: 0

Views: 762

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

You don't create self.shared_Data until after you create the pages, but you are using it while creating the pages. There appears to be other errors in the app, but that's why you're getting this specific error.

Upvotes: 3

Related Questions