2016-11-28 18 views
1

私はラベルを更新しようとしていますが、私が書いたコードは毎回新しいラベルを作成します。私は比較的新しいtkinterですので、私のコードに他の答えを適用する方法を理解できませんでした。tkinterラベルを更新する

from tkinter import * 
import random 

class Window(Frame): 

    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.master=master 
     self.init_window() 

    def init_window(self): 
     self.pack(fill=BOTH, expand=1) 
     testButton=Button(self, text="Press", command=calc) 
     testButton.pack() 
     l1=Label(text="") 

    def testbutton(ans): #creates a new instance of l1 each time, I want to update existing l1 
     var=StringVar() 
     l1=Label(textvariable=var) #l1.configure() gives error l1 not defined 
     var.set(ans) 
     l1.pack() 

def calc(): 
    list1=["a","b","c"] 
    index=random.randint(0,2) 
    answer=list1[index] 
    Window.testbutton(answer) 

root=Tk() 
root.geometry("400x300") 
app=Window(root) 
root.mainloop() 

ボタンを押すたびに、既存のラベルのテキストを更新する代わりに、新しいラベルが作成されます。 これは私の実際のプロジェクトの簡略版ですが、ラベルの問題点を強調しています。 私はをtestbutton関数内で使用しようとしましたが、l1が定義されていないというエラーが発生します。

答えて

3

新しいLabelが毎回作成されないようにするには、1つを作成してWindowインスタンスの属性として保存する必要があります。 calc()関数にアクセスできるようにするには、グローバル変数の使用を避けるために、Windowインスタンスを引数として渡す必要があります。 tkinterでこれを行う理由は、Buttoncommand=引数としてlamba関数を使用し、selfを引数のデフォルト値にすることです。

from tkinter import * 
import random 

class Window(Frame): 
    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.init_window() 

    def init_window(self): 
     self.pack(fill=BOTH, expand=1) 
     testButton = Button(self, text="Press", 
          command=lambda win=self: calc(win)) 
     testButton.pack() 
     self.l1 = Label(text="") 
     self.l1.pack() 

    def testbutton(self, ans): 
     self.l1.configure(text=ans) 

def calc(window): # note window argument added 
    list1 = ["a","b","c"] 
    index = random.randint(0,2) 
    answer = list1[index] 
    window.testbutton(answer) 

root = Tk() 
root.geometry("400x300") 
app = Window(root) 
root.mainloop() 
+0

が、これは私の問題を解決し、ありがとうございます。 init_windowのl1ラベルにself.l1が必要なのはなぜですか? –

+0

Becky: 'self.l1 = Label(text =" ")'は、作成された 'Label'ウィジェットが後で参照できるように' Window'インスタンスにどのようにアタッチされているかを示します。 Pythonでは、使用する名前に値を割り当てることで、いつでも新しいインスタンス属性を作成できます。つまり、 'self.attribute_name = ...'です。 'del self.attribute_name'でそれらを削除することもできます。 – martineau

0

クラスのメソッドと属性を使用できます。
は、ラベルのテキストを変更するStringVarを使用します。

class Window(Frame): 

    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.init_window() 

    def init_window(self): 
     self.pack(fill=BOTH, expand=1) 
     testButton = Button(self, text="Press", command=self.calc) 
     testButton.pack() 
     self.ltext = StringVar() 
     l1 = Label(textvariable=self.ltext) 
     l1.pack() 

    def testbutton(self, ans): 
     self.ltext.set(ans) 

    def calc(self): 
     list1 = ["a", "b", "c"] 
     index = random.randint(0, 2) 
     answer = list1[index] 
     self.testbutton(answer) 

root = Tk() 
root.geometry("400x300") 
app = Window(root) 
root.mainloop() 
関連する問題