2017-02-25 10 views
-1

変更するJSONファイルのテキストに基づいてラベルを自動的に更新する必要があります。私はいくつかのStackOverflowの記事でStringVar()がラベルテキストを変数にリンクするためのtkソリューションを内蔵していることをよく読んでいます。ページクラスpythonのtkinterラベルのテキスト

私の質問は、下記のPageクラス(このページのコードのみ)からのラベルのみを更新しようとしています。つまり、Pageは大きなアプリのどこかで呼び出され、PageはJSONファイルから適切な値でラベルをロードする必要があります。

他のほとんどの投稿は、別の方法(つまり、ページ上のクリックイベント)からラベルを更新するアプローチです。しかし、私は常に更新されているjsonファイルからデータをロードするいくつかのページを持っています。

  1. 私は "PY_VAR1を" と言ってラベルテキストを取得するコードを実行すると(を解決)。これをどうやって解決するのですか?

  2. Pageに最初にアクセスすると、ラベルテキストが正しい(初期化が正しく機能した)。しかし、他のアプリケーションページにアクセスしてからPageが返されると、ラベルテキストは更新されたjson値ではなく、初期化された値のままです。初期化後にラベル値を更新するにはのみPageコードを使用していますか?

注 - Python Tkinter, modify Text from outside the classは問題に似ていますが、私はからクラス内のテキストを変更したいです。

PY_VAR1アップデート:text = "Test Type: {}".format(data['test_type'])で固定

PY_VAR1の問題。しかし、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") 
+0

あなた 'test_type'はtk.StringVar''のインスタンスです。 'str()'を実行すると、 'tk.StringVar'の割り当てられた名前を返すだけです。 – abccd

+0

@abccd 'TypeErrorをエンコードしたためにstr()を使用しました: 'StringVar'オブジェクトをstrに暗黙的に変換できません。 'エラー –

+1

' label = ttk.Label(self、text = "テストタイプ:{} "。format(data ['test_type']))'? – abccd

答えて

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

'after'はまさに私が必要とする機能です。ありがとう!これは長い間私を悩ませている... –

関連する問題