简体   繁体   中英

update tkinter label text in page class python

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 (ie 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")
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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM