0

I am having trouble with passing the variables into different classes. I am a beginner. I am trying to get the entry box to pass the textvariable which is self.margaritapizza(). However, when I try run it, it says 'nextage' has no attribute to 'margaritapizza'. So not sure how to pass the variable to different classes. I am using tkinter and it is in visual studio code if that helps.

import tkinter as tk
from tkinter import IntVar

class SampleApp(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.margaritapizza = IntVar()

        self.frames = {}
        for F in (StartPage, nextpage):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        '''Show a frame for the given page name'''

        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, bg='#3d3d5c')
        self.controller = controller

        heading_label = tk.Label(self,
                             text="start page",
                             font=('arial', 45, 'bold'),
                             foreground='#ffffff',
                             background='#3d3d5c')
        heading_label.pack(pady=25)

        def next():
            controller.show_frame('nextpage')
        next_button = tk.Button(text="start page",command=next).pack

class nextpage(tk.Frame,SampleApp):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, bg='#3d3d5c')
        self.controller = controller

        heading_label = tk.Label(self,
                                 text="start page",
                                 font=('arial', 45, 'bold'),
                                 foreground='#ffffff',
                                 background='#3d3d5c')
        heading_label.pack(pady=25)

        entry = tk.entry(borderwidth=2,width=15,textvariable=self.margaritapizza).pack

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
JC_101
  • 5
  • 3
  • 8
    That's quite a lot of code, most of which I assume is irrelevan to the question. Can you please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so others can reproduce this problem easily? – Mushroomator Mar 23 '22 at 12:55
  • I have changed it – JC_101 Mar 23 '22 at 14:35
  • Unfortunately, I get this error which is '_tkinter.tkapp' object has no attribute 'margaritapizza'. I do appreciate the help though. I did update the code so you can see what I did wrong if that helps – JC_101 Mar 23 '22 at 17:49
  • Try ```entry = tk.entry(borderwidth=2,width=15,textvariable=SampleApp().margaritapizza).pack``` and you can change it back to ```class nextpage(tk.Frame)``` – Mark Mar 23 '22 at 18:12
  • Unfortunately, that didn't work. A bunch of errors in the terminal came up. I kept repeating this: import tkinter as tk.py", line 52, in __init__ entry = tk.Entry(borderwidth=2,width=15,textvariable=SampleApp().margaritapizza) File "c:\Users\Joshu\OneDrive\Documents\Kate's Pizza\import tkinter as tk.py", line 19, in __init__ frame = F(parent=container, controller=self) – JC_101 Mar 23 '22 at 18:35
  • @Mark your suggestion would create a second instance of `SampleApp()` which almost certainly is the wrong thing to do. – Bryan Oakley Mar 23 '22 at 21:44
  • Does this help answer your question? https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter Or this? https://stackoverflow.com/questions/32212408/how-to-get-variable-data-from-a-class – Bryan Oakley Mar 23 '22 at 21:47

1 Answers1

0

First, nextpage should definitely not inherit from SampleApp. So, change the definition to:

class nextpage(tk.Frame):

Second, margaritapizza is an attribute of SampleApp, so you need to refer to it via the instance of SampleApp. In your case, self.controller is that instance, so you can use self.controller.margaritapizza.

There are other problems with how you define the entry, I'll fix them as well:

self.entry = tk.Entry(self, borderwidth=2,width=15,textvariable=self.controller.margaritapizza)
self.entry.pack()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685