0

I am getting:

print (p.s) AttributeError: StartPage instance has no attribute 's'

here is my code:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.s = []
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        container = tk.Frame(self)
        container.grid(row=0, column=0, sticky='we')
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (StartPage, images_result):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            frame.grid(row=0, column=0, sticky='nsew')
            self.frames[page_name] = frame
        self.show_frame("StartPage")
    def get_SP(self):
        return self.frames["StartPage"]
    def images(self, page_name):
        self.s = [1, 2]
        frame = self.frames[page_name]
        frame.tkraise()
    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.grid(row=0, column=0, sticky='snew')
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.image_btn = tk.Button(self, text="Images", command=lambda: controller.images("images_result"))
        self.image_btn.grid(row=0, column=0, sticky='snew')



class images_result(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        p = self.controller.get_SP()
        print (p.s)



app = SampleApp()
app.mainloop()

Edited:

I want the function images in SampleApp to update the attribute s of the SampleApp then I want to be able to access it from the class images_results.

I tried to use

print(self.controller.s)

but that gives me [] and by that it seems that the modification that has been done by the function images to the s attribute are not visible to the images_result.

mark
  • 351
  • 1
  • 3
  • 16
  • So why do you expect `StartPage` to have an `s` attribute? – Aran-Fey Mar 25 '19 at 16:17
  • @Aran-Fey I am trying information from one page to another to display it if that is what you meant by your question – mark Mar 25 '19 at 16:18
  • @Aran-Fey somthing similar to [link](https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter) – mark Mar 25 '19 at 16:20
  • You realize that `SampleApp` has the `s` attribute, not `StartPage`? – Aran-Fey Mar 25 '19 at 16:25
  • @Aran-Fey sorry, my bad, I have edited my question please kindly read it again and try to help me – mark Mar 25 '19 at 16:39
  • Are you aware that `images_result` is run when the app starts up, and not after the user interacts with the other pages? – Bryan Oakley Mar 25 '19 at 16:50
  • @BryanOakley thank you, I feel so dummy because I forgot how the classes work when I was focusing on learning tkinter. would you like please write your comment in answer so I approve it so when anyone encounter the same problem he/she finds the answer quickly? – mark Mar 25 '19 at 16:54

1 Answers1

0

The only class in your code which as an s attribute is SampleApp. The instance of this class is passed as the controller attribute for each page. Thus, to access the s attribute of the SampleApp object from within a page will be through the controller:

print(self.controller.s)

It's important to note that in the code you posted, the __init__ method of images_remote will only run when the app first starts up, so any values set by interacting with the other pages won't be printed by the __init__ function.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • sorry, my bad, I have edited my question please kindly read it again and try to help me – mark Mar 25 '19 at 16:40