1
def write_lor():
    with open("data.txt", 'r+') as filehandle:
        filecontents = filehandle.readlines()
        filehandle.seek(0)
        new = (int(filecontents[0])+1)
        filehandle.write(str(new))
        filehandle.truncate()

This is a basic counter sort of system that saves to a txt. Is there anyway to reference the variable new later down the line? For example my usage is:

button1 = Button(frame, text=("Left On Read:\n", **VARIABLE**), fg="black", bg="white", height = 5, width = 20, command=write_lor)

with the variable needing to be the variable new referenced in the function above.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
OhItsTom
  • 39
  • 4

1 Answers1

0
def write_lor():
    with open("data.txt", 'r+') as filehandle:
        filecontents = filehandle.readlines()
        filehandle.seek(0)
        global lor
        lor = (int(filecontents[0])+1)
        filehandle.write(str(lor)+ "\n")
OhItsTom
  • 39
  • 4
  • 1
    Please add documentation explaining why this solution works, what it targets, and the problem it tries to address. – Skarlett Jun 26 '21 at 18:16
  • 1
    Please be more elaborate, provide an [explanation](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers) for your answer. – rawrex Jun 27 '21 at 03:09