Search code examples
python-3.xtkinterglobal-variablesdynamically-generated

How to avoid global variables in a dynamically gen. tkinter form? Values inserted by functions won't save, hand typed values will after code change


I'm attempting to remove the global variable I relied on, instead passing/returning the necessary values. It seems to work, partially, but the results have me confused.

Is there a way to avoid a global variable without switching to object oriented?*

With the global variable holding the widgets, the form works as desired. It creates a default, blank form and can rebuild the form with values loaded from file, and saves the widget values regardless of whether the values are typed in, loaded from file, edited after loading, etc.

However, when I attempt to pass/return the list of widgets instead of using the global variable, only manually entered values are saved, and once the load_file function is called, any call to save_file simply saves the last values manually entered. (To view the difference, toggle the current commenting, lack thereof for lines marked with inline comments).

I'd like help understanding what is wrong here, and the options for doing it correctly.

import tkinter as tk

root = tk.Tk()
root.geometry('900x800')

form_frame = tk.Frame(root) 
button_frame = tk.Frame(root)

form_frame.grid(row = 0)
button_frame.grid(row = 1)

# default form, or form that matches opened dataset 
def build_form(dataset = None): 
    global entry_objects        #<==== Comment out this line (1/4)...
    entry_objects = []
    if dataset == None:            
        rowcount = 2   
    else:
        rowcount = len(dataset)      
    for row_i in range(rowcount):
        entry_list = []
        if dataset is not None:
            data_row = dataset[row_i]     
        for col_i in range(3):   
            entry = tk.Entry(form_frame)
            if dataset is not None:
                entry.insert(0, str(data_row[col_i]))
            entry_list.append(entry)
            entry.grid(row = row_i, column = col_i)    
        entry_objects.append(entry_list)
    #return(entry_objects)      #<==== ... uncomment this line (2/4)...

def open_file():    # open_file(), save_file() are just substitutes.
    test_data = [['a1', 'a2', 'a3'],['b1', 'b2', 'b3'],['c1', 'c2', 'c3']]
    build_form(test_data)

def save_file(entry_objects):
    entry_values =  [[j.get() for j in i]  for i in entry_objects]
    print('--> Saved to csv file: ')
    print(entry_values)

build_form()                    #<==== ... comment this line (3/4)...   
#entry_objects = build_form()   #<==== ... and uncomment this line (4/4).   


open_button = tk.Button(button_frame, text = 'Load Test Data',
                     command = open_file)
save_button = tk.Button(button_frame, text = 'Save', 
                     command = lambda: save_file(entry_objects))
exit_button = tk.Button(button_frame, 
                     text = 'Exit', command=root.quit)

open_button.pack(side = 'left')
save_button.pack(side = 'left')
exit_button.pack(side = 'left')

root.mainloop()

This is the problematic bit from my first program, much trimmed and simplified for clarity.

*I want to clear up my confusion here in procedural terms before learning about OOP. I used a global variable before reading about the problems it causes, and understanding how to avoid using global variables has been a challenge.

There are a lot of questions about accessing values from dynamically generated widgets, avoiding globals, etc., that got me this far, but none that I've understood as addressing this issue.


Solution

  • From what I can tell, this is what I can say.

    With what the code is (global entry_objects is uncommented), everything is okay. When the save_file function runs, the entry_objects variable is set to the updated value and gives the correct data.

    When the build_form function is run with the return statement, the open_file function isn't updated to take build_file as a value, rather it expects it to be a statement.

    def open_file():    # this, save_file are just substitutes.
        test_data = [['a1', 'a2', 'a3'],['b1', 'b2', 'b3'],['c1', 'c2', 'c3']]
        build_form(test_data)    # this does nothing
    

    What it should need to include the global statement (because it's changing a value outside the function) global entry_objects and it needs to set entry_objects to the value the build_form function gives, meaning entry_objects = build_form(test_data). Here's what the updated function looks like:

    # updated open_file for when build_form returns a value rather than changing the value by itself
    def open_file():
        # this makes changes to entry_objects visible to things outside the function
        global entry_objects
        test_data = [['a1', 'a2', 'a3'],['b1', 'b2', 'b3'],['c1', 'c2', 'c3']]
        entry_objects = build_form(test_data)    # this sets the returned value to entry_objects
    

    Basically what I'm saying (in this large jumble of words) is that entry_objects will have to be changed and the only way to make that change and project the updated value to all other calls of that variable is to use global and make all changes to entry_objects visible to everything else, including the

    save_btn = tk.Button(
        button_frame, text='Save',
        command=lambda: save_file(entry_objects)
    ).pack(side='left')
    

    at the end.

    Just a tiny tip, try to wrap your lines to 74 characters as it helps others with small screens see the whole picture rather than scrolling around :)

    If I wasn't clear, feel free to tell me what I needed to explain more on. Great work on that form too :D