Search code examples
pythonpython-3.xtkinterrefresh

How to refresh or update frame after value changes in text file using Python/Tkinter


So basically I am reading a valuefrom a text file which is displayed on the Profile frame using a label. def view_value(self): self.user = self.controller.user self.view_value()

        with open(self.user + '.txt', "r") as f:
            value_line = 2
            for i, line in enumerate(f):
                if i == value_line:
                    self.value.set(line)
                    self.value.config(textvariable=line)

When I go to a different frame to calculate this value again, it will update the text file with the newly calculated value. However, when I go back to the previous page using the back button - the old value is still there. To get the new value to appear I need to reopen/re-run the program.

Is it possible to have the newly updated value displayed on the page without restarting the application? I have tried calling my view_value method to try and update the value and also tried configuring the label from the other class but wasn't able to get it working.

I also realise there are probably a million things wrong with my code, I am very new to Python so apologies!


Solution

  • You need to update the StringVar Profile.allowance in order to make the display in Profile page updated. The simple way is adding the following statement before self.controller.show_frame(Profile) in write_to_file() of CalculateAllowance class:

    self.controller.frames[Profile].allowance.set(self.user_data[2])
    

    Also you need to fix the following issues in your code:

    • Remove calling self.view_allowance() inside view_allowance() in Profile class as it will cause infinite recursion problem.
    • Remove self.holiday_allowance_amount.config(textvariable=line) in view_allowance() as it wrongly reassigns textvariable to a string.