-2

I have a problem with my code. I am unable to pass a variable to another class once a submit button is pressed in my tkinter frame.

I have followed the advice from a post already (How to access variables from different classes in tkinter?), which has helped but I still have issues.

From where to where I need these variables is commented on the code below:

import tkinter as tk
from tkinter import StringVar
LARGE_FONT = ("Verdana", 12)

class Club(tk.Tk):

    def get_page(self, page_class):
        return self.frames[page_class]

    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.shared_data = {
            "username": tk.StringVar(),
            "password": tk.StringVar(),
        }

        self.frames = {}

        for F in (Terminal, newUser, newUserSubmitButton, delUser):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Terminal)

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


class Terminal(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Welcome to the club terminal. Click the options below", font=LARGE_FONT)
        label.grid(columnspan=3)

        button = tk.Button(self, text="Visit new User",
                           command=lambda: controller.show_frame(newUser))
        button.grid(row=1, column=0)

        button2 = tk.Button(self, text="Visit del User",
                            command=lambda: controller.show_frame(delUser))
        button2.grid(row=1, column=1)

class newUser(tk.Frame):

    def __init__(self, parent, controller):

        def submitButton():
            username = self.controller.shared_data["username"].get()
            print(username)
            controller.show_frame(newUserSubmitButton)
            ##username variable from here to...

        tk.Frame.__init__(self, parent)
        welcomelabel = tk.Label(self, text="Add New User/s", font=LARGE_FONT)
        welcomelabel.grid(columnspan=2, sticky="ew")

        userNameLabel = tk.Label(self, text="Username")
        userNameLabel.grid(row=1, column=0, sticky="e")
        userNameEntry = tk.Entry(self, textvariable=self.controller.shared_data["username"])
        userNameEntry.grid(row=1, column=1)

        userMemTypeLabel = tk.Label(self, text="Membership Type")
        userMemTypeLabel.grid(row=2, column=0, sticky="e")
        variable = StringVar(self)
        variable.set("Full")
        userMemTypeMenu = tk.OptionMenu(self, variable, "Full", "Half")
        userMemTypeMenu.grid(row=2, column=1)

        userMemYearsLabel = tk.Label(self, text="Years that member is in the club")
        userMemYearsLabel.grid(row=3, column=0, sticky="e")
        userMemYearsEntry = tk.Entry(self)
        userMemYearsEntry.grid(row=3, column=1)
        self.controller = controller

        newusersubmitbutton = tk.Button(self, text="submit", command=submitButton)
        newusersubmitbutton.grid(columnspan=2)

class newUserSubmitButton(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        ##username variable goes here
        page1 = self.controller.get_page(newUser.submitButton)
        page1.username.set("Hello, world")
        print(page1)

class delUser(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="del User!!!", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(Terminal))
        button1.pack()

        button2 = tk.Button(self, text="new User",
                            command=lambda: controller.show_frame(newUser))
        button2.pack()

app = Club()
app.title("Club Terminal")
app.iconbitmap("table.ico")
app.mainloop()

Whenever I run this code, I get an AttributeError: 'newUser' object has no attribute 'controller'.

Any help is greatly appreciated, I'll be more than happy to try any ideas out.

With regards.

1 Answers1

0

There are more problems in this code, but to solve that one, add the line:

self.controller=controller

To the newUser classes __init__ function.

Artemis
  • 2,553
  • 7
  • 21
  • 36