0

I have a piece of code below, I have a button (prnt_button) that must empty a list and a text box, but unfortunately, it empties only the text box, how could I change the code, so that the button empties also the list?

# This is in a for loop, you press a button to go to the next step,
# every step you comes along the next piece of code:

if k == 3:
    doc = DocxTemplate("template.docx")
    context = { # Fill in some template variables
    }
    doc.render(context)
    docin = f"{numlist[0]}_{numlist[1]}_{numlist[2]}"
    locdoc = f'{text}\\' + docin
    doc.save(f"{locdoc}.docx")
    prntng_l.append(docin)
    # and more things, which work fine.
    k = 1
else:
    k = k + 1

maken_files_not_printed = Text(mult_prnt_frame1, width=column_width, height=10, state='disabled')
maken_files_not_printed.grid(row=1, column=0)


def print_all_files_in_list(prntng_l):
    maken_files_not_printed.config(state="normal")
    maken_files_not_printed.delete(1.0, 'end')
    maken_files_not_printed.config(state="disabled")
    prnt_button.config(text="Print: 0", state='disabled')
    prntng_l = []
    return (prntng_l)


prnt_button = Button(mult_prnt_frame1, text="Print 0", width=column_width + 2, height=1,
                     command=lambda: print_all_files_in_list(prntng_l), state='normal')
prnt_button.grid(row=0, column=0)

maken_files_not_printed.config(state='normal')
maken_files_not_printed.delete(1.0, 'end')
for q in range(len(prntng_l)):
    prnt_button.config(text="Print: " + str(q + 1), state="normal")
    maken_files_not_printed.insert('end', "Naam bestand " + str(q + 1) + ":\n" + prntng_l[q] + "\n")
maken_files_not_printed.config(state='disabled')

Could someone please help me? Thanks in advance.

  • 1
    Add `global prntng_l` to the function so it assigns the global variable, not a local variable. – Barmar Jan 04 '22 at 20:45
  • no, this did not work for me –  Jan 04 '22 at 20:52
  • If you want to access the global variable, don't use the same name for a parameter. – Barmar Jan 04 '22 at 20:53
  • that is not possible, because you always need that variable in the for loop. The code must be something like this: go always through the for q loop, if the button is clicked: the list must be empty, thus the for q loop does nothing, then, without clicking, the list must become longer and longer, again until clicking the button, after that the list is empty, and so on. –  Jan 04 '22 at 21:05
  • `print_all_files_in_list` never uses the parameter, why does it need it? – Barmar Jan 04 '22 at 21:05
  • it doesn't need, I have forgotten to delete it. –  Jan 04 '22 at 21:12
  • Change `prntng_l = []` to `prntng_l.clear()`. – acw1668 Jan 05 '22 at 00:12
  • yes, thank you, that works fine! –  Jan 05 '22 at 06:37

0 Answers0