1

I have some pages in a container, and I want the name box that the user is allowed to type into from one of my pages to update and be displayed on the other page. This is not a variable in all my pages, this is only something I want displayed in 1 of my pages. My app has many pages, but this is my minimal reproducible example of my problem.

I know when my "template" page is created, the text in the name box is blank, so I need to somehow pass the variable when the 'load_page' function is called, but I cannot figure out how to make this work. Any help is appreciated.

The code below gives the error: AttributeError: type object 'template' has no attribute 'name_box' - The problem I have is I do not know how to specify the name box from one page to grab the entered text, and then insert it into another box in another page.

See code below:

import tkinter as tk
from tkinter import font as tkfont, filedialog, messagebox

class SLS_v1(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title('SLS')
        self.geometry("552x700")
        self.resizable(False, False)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        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 = {}

        self.frames["MenuPage"] = MenuPage(parent=container, controller=self)
        self.frames["template"] = template(parent=container, controller=self)

        self.frames["MenuPage"].grid(row=0, column=0, sticky="nsew")
        self.frames["template"].grid(row=0, column=0, sticky="nsew")

        self.show_frame("MenuPage")


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


class MenuPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        name_label = tk.Label(self, text='Name:')
        name_label.pack(pady=(20,0))

        self.name_var = tk.StringVar()
        self.name_entry = tk.Entry(self, width=10, textvariable=self.name_var)
        self.name_entry.pack()

        template = tk.Button(self, text='Template', height=3, width=20, bg='white', font=('12'),
                                command=lambda: self.load_page(controller))
        template.pack(pady=50)

    def load_page(self, controller):
        controller.show_frame('template')
        template.name_box.insert(tk.END, self.name_entry.var.get())

class template(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.grid(columnspan=10, rowspan=10)

        top = tk.Label(self, height=3, width=80, bg='dark grey')
        top.grid(row=0, column=0, columnspan=10)

        self.back_btn = tk.Button(self, text='BACK', font=('Helvetica', '14'), bg='dark grey',
                                  command=lambda: controller.show_frame('MenuPage'))
        self.back_btn.grid(row=0, column=0, columnspan=2, padx=10, pady=10)

        name_var = tk.StringVar()
        name_box = tk.Entry(self, width=10, textvariable=name_var)
        name_box.grid(row=1, column=1)



if __name__ == "__main__":
    app = SLS_v1()
    app.mainloop()

Edit: Fixed error in code.

Lzypenguin
  • 945
  • 1
  • 7
  • 18
  • I can't reproduce your problem. When I run your code, I get `AttributeError: '_tkinter.tkapp' object has no attribute 'loading_label'`. If your question is about an error message, please [edit] it to make that more clear. If it is not, please make sure that your [example] has the problem(s) it is supposed to, and no others that may hinder the testing of your code. – Sylvester Kruin Jan 22 '22 at 18:59
  • @SylvesterKruin I fixed the code and added another commend explaining a little more what I am having trouble with. Currently the code does give an error when you click the template button. – Lzypenguin Jan 22 '22 at 19:12

1 Answers1

1

You can solve this by importing GC and changing the function that is called when you click the template button to adding these few lines:

import gc

...
...

    def load_page(self, controller):
        controller.show_frame('template')
        for obj in gc.get_objects():
            if isinstance(obj, template):
                obj.name_box.delete(0, tk.END)
                obj.name_box.insert(tk.END, self.name_entry.get())
BrianC
  • 88
  • 5