Home > Back-end >  How do i store the value of appended list in a for loop, outside the for loop?
How do i store the value of appended list in a for loop, outside the for loop?

Time:11-20

this a tkinter gui to input prices. It will add the prices to the empty list and tell the user the the sum of the list. but now i want to use the data outside of the for loop but whatever version of the list i can think of using it always shows up as an empty list.

EXTRAS = []
def add():
    for x in range(1):
        EXTRAS.append(user_input1g.get())
        EXTRAS_int = [float(x) for x in EXTRAS]
        entry_label1g.config(text=str(sum(EXTRAS_int)))
        user_input1g.delete(0, 10)
        


# Entry
user_input1g = tk.Entry(window, width=5)
user_input1g.grid(row=5, column=1)
# Add button
add_button1g = tk.Button(window, text="Add", command=add)
add_button1g.grid(row=5, column=2)
# Empty label
entry_label1g = tk.Label(window, text="")
entry_label1g.grid(row=5, column=4, pady=10)
# $
entry_label2g = tk.Label(window, text="$")
entry_label2g.grid(row=5, column=3, pady=10)
# Description
entry_label3g = tk.Label(window, text="EXTRAS")
entry_label3g.grid(row=5, column=0, pady=10)`

i tried

  • print(EXTRAS) outcome = []
  • print(ETRRAS_int) but that doesn't exist outside the loop.
def add():
    for x in range(1):
        EXTRAS.append(user_input1g.get())
        EXTRAS_int = [float(x) for x in EXTRAS]
        entry_label1g.config(text=str(sum(EXTRAS_int)))
        user_input1g.delete(0, 10)
        EXTRAS_SUM = sum(EXTRAS_int)

print(EXTRAS_SUM) but that doesnt work either.

CodePudding user response:

    import tkinter as tk
from tkinter import *

window = Tk()

window.title("Test Window")
window.geometry('300x300')

EXTRAS = []

def add():
    for x in range(1):
        EXTRAS.append(float(user_input1g.get()))
        entry_label1g.config(text=str(sum(EXTRAS)))
        user_input1g.delete(0, 10)
        


# Entry
user_input1g = tk.Entry(window, width=5)
user_input1g.grid(row=5, column=1)
# Add button
add_button1g = tk.Button(window, text="Add", command=add)
add_button1g.grid(row=5, column=2)
# Empty label
entry_label1g = tk.Label(window, text="")
entry_label1g.grid(row=5, column=4, pady=10)
# $
entry_label2g = tk.Label(window, text="$")
entry_label2g.grid(row=5, column=3, pady=10)
# Description
entry_label3g = tk.Label(window, text="EXTRAS")
entry_label3g.grid(row=5, column=0, pady=10)

window.mainloop()
print(EXTRAS)

CodePudding user response:

you can use the functions beginning with the keyword global. In this case, it references the value of use EXTRAS and EXTRAS_int so it changes their values also outside the function.

It looks as follows:

def add():
    global EXTRAS_int
    global EXTRAS
    for x in range(1):
        EXTRAS.append(user_input1g.get())
        EXTRAS_int = [float(x) for x in EXTRAS]
        entry_label1g.config(text=str(sum(EXTRAS_int)))
        user_input1g.delete(0, 10)

Try to call another function that prints your lists, it should print the updated values. Good luck :)

  • Related