-1

I need to automatically update a label based on text in a changing JSON file. I read in several StackOverflow posts that StringVar() is a good built-in tk solution for linking label text to a variable.

My question differs from other posts in that I am attempting to update the label only from the Page class listed below (in just the code for this page). In other words, Page is called somewhere in a larger app - and Page needs to load the label with the appropriate value from the JSON file.

Most other posts approach label updating from a separate method (i.e. a click event on the page). However, I have several pages that load data from a json file that is constantly updating.

  1. (resolved) When I run the code I get label text saying "PY_VAR1". How do I fix this?

  2. When Page is first accessed, the label text is correct (initialize did its work correctly). However, when other app pages are accessed and then Page is returned to, the label text stays at the initialized value, not the updated json value. How can the label value be updated after initialization only using Page code?

Note - Python Tkinter, modify Text from outside the class is similar to the issue, but I want to modify the text from inside the class.

PY_VAR1 Update:

PY_VAR1 issue fixed with text = "Test Type: {}".format(data['test_type']). However, still need solution for successful auto-updates of the label with json content changes.

import tkinter as tk
from tkinter import messagebox
from tkinter import ttk

# File system access library
import glob, os

import json

class Page(tk.Frame):
        test_type = tk.StringVar()

        def __init__(self, parent, controller):
                tk.Frame.__init__(self, parent)

                # app controller
                self.controller = controller

                test_type = tk.StringVar()

                # Read json file
                with open('data.json','r') as f:
                        data = json.load(f)

                test_type.set(data['test_type'])

                label = ttk.Label(self, text=str("Test Type: " + str(test_type)))
                label.pack(pady=1,padx=1, side = "top", anchor = "n")

                button = ttk.Button(self, text="Previous Page",
                                    command=lambda: controller.show_page("Save_Test_Page"))
                button.pack(pady=1,padx=15, side = "left", expand = "no", anchor = "n")
Community
  • 1
  • 1
Spencer H
  • 653
  • 3
  • 12
  • 30
  • your `test_type` is a instance of `tk.StringVar`. By `str()`ing it, you are just returning the assigned name for the `tk.StringVar`. – Taku Feb 26 '17 at 00:07
  • @abccd I used str() due when I encoutered a `TypeError: Can't convert 'StringVar' object to str implicitly` error – Spencer H Feb 26 '17 at 00:08
  • 1
    car't you just do `label = ttk.Label(self, text="Test Type: {}".format(data['test_type']))`? – Taku Feb 26 '17 at 00:12
  • @abccd OK, text in json file showing correctly now. Question also asks how to update the label each time from in `Page`. Any input on this? Label does not auto-update yet. – Spencer H Feb 26 '17 at 00:16
  • Have you done any research? There are literally hundreds of questions and answers related to updating labels, and most tkinter documentation covers this as well. – Bryan Oakley Feb 26 '17 at 00:31
  • @BryanOakley Yes, I have researched this in depth. Most other posts approach label updating from a separate method (i.e. a click event on the page). However, I have several pages that load data from a json file that is constantly updating. When `Page` is first accessed, the label text is correct (initialize did its work correctly). However, when other app pages are accessed and then `Page` is returned to, the label text stays at the initialized value, not the updated json value. The question is - how can the label value be updated after initialization _only_ using `Page` code? – Spencer H Feb 26 '17 at 00:38
  • [Python Tkinter, modify Text from outside the class](http://stackoverflow.com/questions/40477338/python-tkinter-modify-text-from-outside-the-class) is essentially similar to the issue, but I want to modify the text from _inside_ the class. – Spencer H Feb 26 '17 at 01:20

1 Answers1

2
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk

# File system access library
import glob, os

import json

class Page(tk.Frame):
        test_type = tk.StringVar()

        def update_lable(self, label):
                # Read json file
                with open('data.json','r') as f:
                        data = json.load(f)
                label['text'] = "Test Type: {}".format(data['test_type'])
                #rerun this every 1000 ms or 1 second
                root.after(1000, self.update_lable(label)) #or whatever your root was called


        def __init__(self, parent, controller):
                tk.Frame.__init__(self, parent)

                # app controller
                self.controller = controller


                # Read json file
                with open('data.json','r') as f:
                        data = json.load(f)


                label = ttk.Label(self, text="Test Type: {}".format(data['test_type']))
                label.pack(pady=1,padx=1, side = "top", anchor = "n")
                self.update_label(label)
                button = ttk.Button(self, text="Previous Page",
                                    command=lambda: controller.show_page("Save_Test_Page"))
                button.pack(pady=1,padx=15, side = "left", expand = "no", anchor = "n")
Taku
  • 31,927
  • 11
  • 74
  • 85