2012-03-26 19 views
1

次のコードを見てください。 私はまだPythonに慣れていません(1週間で数えます)。 ボタンを使用する - 定義をクリックして、ラベルテキストを変更しようとしています。 GUIは、三つのボタンで構成されています 変数1(押されたときに変数1に変数を変更) 変数2(押されたときに変数2に変数を変更) 印刷変数(押されたときに変数を出力します)ラベルテキストの変更に関する問題

私はいろいろなことを試してみましたラベルテキストを更新する - 無駄にする 私は働くことができないグローバル変数を試しました。

ご協力いただければ幸いです。

from Tkinter import * 
import time 

class GridDemo(Frame): 
    def changevar1 (self): #change variable 
     global variable 
     self.variable = "Variable1" 
     print "You have changed the variable to:" , self.variable 
     time.sleep(0.5) 

    def changevar2 (self): #change variable 
     global variable 
     self.variable = "Variable2" 
     print "You have changed the variable to:" , self.variable 
     time.sleep(0.5) 

    def printvar (self): # print variable 
     print "The variable is:" , self.variable 
     print "" 
     time.sleep(0.5) 

    def __init__(self): 
     Frame.__init__(self) 
     self.master.title("Grid Demo") 
     global variable 
     self.variable = "Start Variable" 

     self.master.rowconfigure(0, weight = 1) 
     self.master.columnconfigure(0, weight = 1) 
     self.grid(sticky = W+E+N+S) 

     self.button1 = Button(self, text = "Variable 1", command = self.changevar1) 
     self.button1.grid(row = 1, column = 1, sticky = W+E+N+S) 

     self.button2 = Button(self, text = "Variable 2", command = self.changevar2) 
     self.button2.grid(row = 1, column = 2, sticky = W+E+N+S) 

     self.button3 = Button(self, text = "print variable" , command = self.printvar) 
     self.button3.grid(row = 1, column = 3, sticky = W+E+N+S) 

     self.label4 = Label(self, text = self.variable) 
     self.label4.grid(row = 2, column = 1, columnspan = 2, sticky = W+E+N+S) 

     self.rowconfigure(1, weight = 1) 
     self.columnconfigure(1, weight = 1) 

def main(): 
    GridDemo().mainloop() 

答えて

2

STRINGVARtextvariableを使用してください。ここに作業コードがあります。私が変更したものをコメントしました。

from Tkinter import * 
import time 

class GridDemo(Frame): 
    def changevar1 (self): #change variable 
     self.label4String.set("Variable1") # use set method to change 
     print "You have changed the variable to:" , self.label4String.get() 
     time.sleep(0.5) 

    def changevar2 (self): #change variable 
     self.label4String.set("Variable2") # use set method to change 
     print "You have changed the variable to:" , self.label4String.get() 
     time.sleep(0.5) 

    def printvar (self): # print variable 
     print "The variable is:" , self.label4String.get() 
     print # just this is enough, no need to add "" 
     time.sleep(0.5) 

    def __init__(self): 
     Frame.__init__(self) 
     self.master.title("Grid Demo") 
     self.variable = "Start Variable" 

     self.master.rowconfigure(0, weight = 1) 
     self.master.columnconfigure(0, weight = 1) 
     self.grid(sticky = W+E+N+S) 

     self.button1 = Button(self, text = "Variable 1", command = self.changevar1) 
     self.button1.grid(row = 1, column = 1, sticky = W+E+N+S) 

     self.button2 = Button(self, text = "Variable 2", command = self.changevar2) 
     self.button2.grid(row = 1, column = 2, sticky = W+E+N+S) 

     self.button3 = Button(self, text = "print variable" , command = self.printvar) 
     self.button3.grid(row = 1, column = 3, sticky = W+E+N+S) 

     self.label4String = StringVar() # use Tk's StringVar 
     self.label4 = Label(self, textvariable=self.label4String) # bind a StringVar to textvariable attr 
     self.label4.grid(row = 2, column = 1, columnspan = 2, sticky = W+E+N+S) 

     self.rowconfigure(1, weight = 1) 
     self.columnconfigure(1, weight = 1) 

def main(): 
    GridDemo().mainloop() 

if __name__ == '__main__': 
    main() 
+0

返信のスピードで光驚きです。 – DaveH

2

テキスト(または任意の属性)configure方法でウィジェットのも、あなたは、ウィジェットに変数を関連付けることができますが、次のことができます。例:

self.label4.configure(text="hello, world!") 

適切な文字列を送信するようにボタンを定義できます。たとえば:

カップルのボタンを定義します。

self.button1 = Button(frame, text="Variable 1", 
         command=lambda message="Variable 1": self.changevar(message)) 
self.button2 = Button(frame, text="Variable 2", 
         command=lambda message="Variable 2": self.changevar(message)) 

コールバックを定義:ところで

def changevar(self, message): 
    self.label4.configure(text=message) 

を、私は強くfrom Tkinter import *を使用してないをお勧めします。それは悪い習慣です。代わりに、Tkをインポートして関数を完全修飾します。例:

import Tkinter as tk 
... 
frame = tk.Frame(...) 

import *を実行しているモジュールはありません。 Tkinterからの作業は、Pythonの現代版では特に悪いことです。 Tkinterには、モジュールttkに "themed"ウィジェットがあります。これらは通常のウィジェットと同じクラス名を持ちます.Tkinter.Buttonとttk.Buttonがあります。

Doing import *は、同じファイルで両方を簡単に使用できないようにします。任意の複雑なアプリケーションのために、あなたが望む外観を得るためにミックスして一致させたいと思うでしょう。好みのインポートスタイルを使用する場合は、tk.Label(...)ttk.Label(...)を実行して、使用しているウィジェットセットのコードが明確になります。

+0

はい、私は単一の 'changevar'を定義し、次に' changevar'メソッドを定義するよりもlambdaを使う方が良いと思います。 – Ray