2

I have been working on creating an app to help some of our office workers.I have somewhat of a skeleton working but my problem is I can't get my windows to update their values after the initialization of them.
I have global variables along with some get and set functions:

building = "SomeBuilding"
def setBuilding(bldg):
    global building
    building = bldg
    print("building:", building)

def getBuilding():
    print("Building from get:",building)
    return building

And I have a class which is used to create the app and the frames:

class App(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.frames = {}
        for F in (StartPage, PageOne,PageTwo):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

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

The problem comes in when I take some values from a frame and store them in the global variable. The global variable gets updated but when moving to the next frame I have a label which displays the value of the global variable. This happens to be the value the global variable was initially.

class PageOne(tk.Frame):

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

        #buidling
        bldg_label = ttk.Label(self, text="Building Name (ie. Bldg1):", font=LARGE_FONT)
        bldg_label.grid(row=0, column=0, padx=2, pady = 10)

        self.bldg_var = tk.StringVar()
        bldg_entry =ttk.Entry(self,width = 6, textvariable = self.bldg_var)
        bldg_entry.grid(row=0, column=1, padx=2, pady=10)

Within this class I have a button that sets the values of the building entry

button2 = ttk.Button(self, text="Apply",command=lambda: setBuilding(self.bldg_var.get()))
button2.grid(row=row_num, column = 1)

and a button to go to the next frame:

button3 = ttk.Button(self, text="next", command = lambda: controller.show_frame(PageTwo))

When PageTwo is then raised it prints out the building label with the old value "SomeBuilding" instead of what the user submitted. I tried as you can see calling the global variable and then printing it out in hopes of getting the value but only get the same old value, I also tried using an update but it doesn't work. Any help would be greatly appreciated. I've looked online for examples but they only go over a single page or frame but not updating across frames. Thank you!

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        row_num = 0
        global building
        used_building = building
        bldg_label = ttk.Label(self, text = "Building: " + used_building, font=LARGE_FONT)

        bldg_label.grid(row = row_num, column =0)
jaleman
  • 63
  • 6

0 Answers0