2017-02-23 7 views
-1

tkinterでは、python、私はtkinterで学んだことを示すことができるので、私の家庭教師のための 'いたずら'プログラムを作ろうとしていますが、StringVar()を使っている間違いがあります。 がここに私のコードです:メッセージボックスのStringVar()?

from tkinter import * 
root = Tk() 
root.geometry("1x1") 
secs = StringVar() 
sec = 60 
secs.set("60") 
def add(): 
    global secs 
    global sec 
    sec += 1 
    secs.set(str(sec)); 
    root.after(1000, add) 
add() 
messagebox.showinfo("Self Destruct", "This computer will self destruct in {} seconds".format(str(secs))) 

私はこのコードを実行すると、私は正しいメッセージを取得し、まだ私はカウント数を得ることはありません、私はPY_VAROを取得します。私は60から数えて、数を得ているはずです。 ありがとう。

+0

STRINGVAR(の値をキャッチするために使用stringvar.get())。あなたの場合 - messagebox.showinfo( "Self Destruct"、 "このコンピュータは{}秒後に自己破壊する"。)format(str(secs.get()))) – Suresh2692

+0

このサイトでは、 'PY_VAR0'に関連する質問を探しましたか? ? –

答えて

1

StringVarの値を取得するには、str(...)の代わりに.get()メソッドを使用します。このオブジェクトは、任意のTkのコントロールにバインドされていないので、

"This computer will self destruct in {} seconds".format(secs.get()) 

しかし、あなたのケースでSTRINGVARを使用しない点が、ありません(あなたmessagebox.showinfoコンテンツはが動的に変更されることはありません)。単純なPython変数を直接使用することもできます。

"This computer will self destruct in {} seconds".format(sec) 

STRINGVARの適切な使用は、次のようになります。

message = StringVar() 
message.set("This computer will self destruct in 60 seconds") 
Label(textvariable=message).grid() 
# bind the `message` StringVar with a Label. 

... later ... 

message.set("This computer is dead, ha ha") 
# when you change the StringVar, the label's text will be updated automatically. 
+0

私が探していたものではなく、その最良の代替品です。ありがとう:) – Jake

関連する問題