How to avoid global variables in a dynamically gen. tkinter form? Values inserted by functions won't save,...











up vote
1
down vote

favorite












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.










share|improve this question




























    up vote
    1
    down vote

    favorite












    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.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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.










      share|improve this question















      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.







      python-3.x tkinter global-variables dynamically-generated






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 22:03

























      asked Nov 12 at 2:02









      fitzl

      464




      464
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          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






          share|improve this answer





















          • This is very helpful. I've will have to play with it to better understand what's happening, but knowing where it breaks down is a big help. In addition to adding line wraps to the code (thank you! I found meta.stackexchange.com/q/3122/409970 ), I've edited the title and question to more clearly state my goal of not relying on a global variable. I'm leaving it as unanswered in hopes of understanding how to reach that goal.
            – fitzl
            Nov 12 at 22:00












          • With regards to getting values, I would use the .get() method to retrieve a value from the Entry widget. entry.get() will get the string value from the widget entry.
            – GeeTransit
            Nov 12 at 22:08












          • Thanks, I've got .get in save_file. OK, I've it working using StringVar() objects instead of global variables. Short version, pretending form only has one field: 1) At the top level, create a tkinter StringVar() using x = StringVar() 2) Change build_form to use entry = tk.Entry(form_frame, textvariable = x) in creating the entry widget. 3) Change open_file() so it sets the values of the StringVar x with x.set(new_value) where new_value is, yup, the new value for the field from the file. I'll post the fixed version once I've got it iterating through.
            – fitzl
            Nov 13 at 3:33













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255136%2fhow-to-avoid-global-variables-in-a-dynamically-gen-tkinter-form-values-inserte%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          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






          share|improve this answer





















          • This is very helpful. I've will have to play with it to better understand what's happening, but knowing where it breaks down is a big help. In addition to adding line wraps to the code (thank you! I found meta.stackexchange.com/q/3122/409970 ), I've edited the title and question to more clearly state my goal of not relying on a global variable. I'm leaving it as unanswered in hopes of understanding how to reach that goal.
            – fitzl
            Nov 12 at 22:00












          • With regards to getting values, I would use the .get() method to retrieve a value from the Entry widget. entry.get() will get the string value from the widget entry.
            – GeeTransit
            Nov 12 at 22:08












          • Thanks, I've got .get in save_file. OK, I've it working using StringVar() objects instead of global variables. Short version, pretending form only has one field: 1) At the top level, create a tkinter StringVar() using x = StringVar() 2) Change build_form to use entry = tk.Entry(form_frame, textvariable = x) in creating the entry widget. 3) Change open_file() so it sets the values of the StringVar x with x.set(new_value) where new_value is, yup, the new value for the field from the file. I'll post the fixed version once I've got it iterating through.
            – fitzl
            Nov 13 at 3:33

















          up vote
          1
          down vote













          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






          share|improve this answer





















          • This is very helpful. I've will have to play with it to better understand what's happening, but knowing where it breaks down is a big help. In addition to adding line wraps to the code (thank you! I found meta.stackexchange.com/q/3122/409970 ), I've edited the title and question to more clearly state my goal of not relying on a global variable. I'm leaving it as unanswered in hopes of understanding how to reach that goal.
            – fitzl
            Nov 12 at 22:00












          • With regards to getting values, I would use the .get() method to retrieve a value from the Entry widget. entry.get() will get the string value from the widget entry.
            – GeeTransit
            Nov 12 at 22:08












          • Thanks, I've got .get in save_file. OK, I've it working using StringVar() objects instead of global variables. Short version, pretending form only has one field: 1) At the top level, create a tkinter StringVar() using x = StringVar() 2) Change build_form to use entry = tk.Entry(form_frame, textvariable = x) in creating the entry widget. 3) Change open_file() so it sets the values of the StringVar x with x.set(new_value) where new_value is, yup, the new value for the field from the file. I'll post the fixed version once I've got it iterating through.
            – fitzl
            Nov 13 at 3:33















          up vote
          1
          down vote










          up vote
          1
          down vote









          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






          share|improve this answer












          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 4:09









          GeeTransit

          337




          337












          • This is very helpful. I've will have to play with it to better understand what's happening, but knowing where it breaks down is a big help. In addition to adding line wraps to the code (thank you! I found meta.stackexchange.com/q/3122/409970 ), I've edited the title and question to more clearly state my goal of not relying on a global variable. I'm leaving it as unanswered in hopes of understanding how to reach that goal.
            – fitzl
            Nov 12 at 22:00












          • With regards to getting values, I would use the .get() method to retrieve a value from the Entry widget. entry.get() will get the string value from the widget entry.
            – GeeTransit
            Nov 12 at 22:08












          • Thanks, I've got .get in save_file. OK, I've it working using StringVar() objects instead of global variables. Short version, pretending form only has one field: 1) At the top level, create a tkinter StringVar() using x = StringVar() 2) Change build_form to use entry = tk.Entry(form_frame, textvariable = x) in creating the entry widget. 3) Change open_file() so it sets the values of the StringVar x with x.set(new_value) where new_value is, yup, the new value for the field from the file. I'll post the fixed version once I've got it iterating through.
            – fitzl
            Nov 13 at 3:33




















          • This is very helpful. I've will have to play with it to better understand what's happening, but knowing where it breaks down is a big help. In addition to adding line wraps to the code (thank you! I found meta.stackexchange.com/q/3122/409970 ), I've edited the title and question to more clearly state my goal of not relying on a global variable. I'm leaving it as unanswered in hopes of understanding how to reach that goal.
            – fitzl
            Nov 12 at 22:00












          • With regards to getting values, I would use the .get() method to retrieve a value from the Entry widget. entry.get() will get the string value from the widget entry.
            – GeeTransit
            Nov 12 at 22:08












          • Thanks, I've got .get in save_file. OK, I've it working using StringVar() objects instead of global variables. Short version, pretending form only has one field: 1) At the top level, create a tkinter StringVar() using x = StringVar() 2) Change build_form to use entry = tk.Entry(form_frame, textvariable = x) in creating the entry widget. 3) Change open_file() so it sets the values of the StringVar x with x.set(new_value) where new_value is, yup, the new value for the field from the file. I'll post the fixed version once I've got it iterating through.
            – fitzl
            Nov 13 at 3:33


















          This is very helpful. I've will have to play with it to better understand what's happening, but knowing where it breaks down is a big help. In addition to adding line wraps to the code (thank you! I found meta.stackexchange.com/q/3122/409970 ), I've edited the title and question to more clearly state my goal of not relying on a global variable. I'm leaving it as unanswered in hopes of understanding how to reach that goal.
          – fitzl
          Nov 12 at 22:00






          This is very helpful. I've will have to play with it to better understand what's happening, but knowing where it breaks down is a big help. In addition to adding line wraps to the code (thank you! I found meta.stackexchange.com/q/3122/409970 ), I've edited the title and question to more clearly state my goal of not relying on a global variable. I'm leaving it as unanswered in hopes of understanding how to reach that goal.
          – fitzl
          Nov 12 at 22:00














          With regards to getting values, I would use the .get() method to retrieve a value from the Entry widget. entry.get() will get the string value from the widget entry.
          – GeeTransit
          Nov 12 at 22:08






          With regards to getting values, I would use the .get() method to retrieve a value from the Entry widget. entry.get() will get the string value from the widget entry.
          – GeeTransit
          Nov 12 at 22:08














          Thanks, I've got .get in save_file. OK, I've it working using StringVar() objects instead of global variables. Short version, pretending form only has one field: 1) At the top level, create a tkinter StringVar() using x = StringVar() 2) Change build_form to use entry = tk.Entry(form_frame, textvariable = x) in creating the entry widget. 3) Change open_file() so it sets the values of the StringVar x with x.set(new_value) where new_value is, yup, the new value for the field from the file. I'll post the fixed version once I've got it iterating through.
          – fitzl
          Nov 13 at 3:33






          Thanks, I've got .get in save_file. OK, I've it working using StringVar() objects instead of global variables. Short version, pretending form only has one field: 1) At the top level, create a tkinter StringVar() using x = StringVar() 2) Change build_form to use entry = tk.Entry(form_frame, textvariable = x) in creating the entry widget. 3) Change open_file() so it sets the values of the StringVar x with x.set(new_value) where new_value is, yup, the new value for the field from the file. I'll post the fixed version once I've got it iterating through.
          – fitzl
          Nov 13 at 3:33




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255136%2fhow-to-avoid-global-variables-in-a-dynamically-gen-tkinter-form-values-inserte%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents