簡體   English   中英

更新頁面類python中的tkinter標簽文本

[英]update tkinter label text in page class python

我需要根據不斷變化的JSON文件中的文本自動更新標簽。 我在StackOverflow的幾篇文章中讀到, StringVar()是將標簽文本鏈接到變量的很好的內置tk解決方案。

我的問題與其他帖子的不同之處在於,我僅嘗試從下面列出的Page類中更新標簽(僅在此頁面的代碼中)。 換句話說, Page被稱為在一個更大的應用程序的地方-而且Page需要從JSON文件將適當的值加載標簽。

其他大多數帖子都通過單獨的方法(即頁面上的click事件)來處理標簽更新。 但是,我有幾個頁面從不斷更新的json文件加載數據。

  1. (已解決 )當我運行代碼時,我得到標有“ PY_VAR1”的標簽文本。 我該如何解決?

  2. 首次訪問Page ,標簽文本正確(初始化正確完成了它的工作)。 但是,當訪問其他應用程序頁面然后返回到Page ,標簽文本將保持在初始化值,而不是更新的json值。 使用Page代碼初始化后如何更新標簽值?

注意-Python Tkinter,從類外部修改Text與該問題類似,但是我想從類內部修改文本。

PY_VAR1更新:

PY_VAR1問題已通過text = "Test Type: {}".format(data['test_type']) 但是,仍然需要通過json內容更改成功自動更新標簽的解決方案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM