0

I am using a text file as a way to save several inputs from an app build in Kivy. I want to display these inputs later on as a summary in a textinput-widget with the inputs as the starting text.

The widget is created in the .kv file and uses text: root.text

def read():
    with open(f"./drafts/draft.txt", "r") as file: #Does not read out current order but old order file
        text = file.read()
        return text
text = read()

I can not seem to get the current state of the file displayed. It always just shows the version of the text file when the program starts, even through the file gets modified.

Is it possible to update the text variable, and/or display the current state of the text file in the text widget.

Adrian David Smith
  • 574
  • 1
  • 4
  • 26
z3nox
  • 15
  • 1
  • 5
  • When the program does modify the contents are you sure they are saved properly? Can you check that using any other tool before you try to read again? – Gameplay Dec 09 '22 at 13:35
  • They are being saved properly. I think the problem is that the read function is being called directly after starting the programm since its not bound to any buttons, etc. Thsi would explain it always returns the state of the file after the last time i closed the program – z3nox Dec 09 '22 at 13:44
  • Yep, that sounds right. So just move the read execution to a proper place and you're good to go. – Gameplay Dec 09 '22 at 13:50
  • You can call `file.read()` again every so often to see if there is more contents in the file. It will load whatever additional contents have been added since the last read. – joanis Dec 09 '22 at 13:58
  • I just dont get where to move it to so that it automatically updates – z3nox Dec 09 '22 at 13:58
  • Maybe this will help: https://stackoverflow.com/q/182197/3216427 – joanis Dec 09 '22 at 14:17
  • An extremely simple solution is to just start another thread with a `while True:` loop that contains a call to your `read()` method and a `sleep` to control how often it is called. – John Anderson Dec 09 '22 at 14:34
  • @JohnAnderson This would work but the rest of my code goes to "sleep" since I'd call this loop not in a functions but simply in a ScreenClass – z3nox Dec 09 '22 at 14:36
  • Call it wherever you want, but create a new thread for it. – John Anderson Dec 09 '22 at 14:41

0 Answers0