2017-02-23 3 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.get()가 StringVar의 값을 잡는(). 귀하의 경우 - messagebox.showinfo ("Self Destruct", "이 컴퓨터는 {} 초 내에 자체 파괴됩니다.") – Suresh2692

+0

이 사이트에서 'PY_VAR0'과 관련된 질문을 검색 한 적이 있습니까? ? –

답변

1

StringVar에서 값을 가져 오려면 str(...) 대신 .get() 메서드를 사용하십시오. 이 객체가 어떤 Tk의 컨트롤에 바인딩되지 않기 때문에

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

그러나 귀하의 경우에 StringVar를 사용하여 아무 소용이 없다 (당신의 messagebox.showinfo 내용 동적으로 변경되지 않습니다). 평범한 파이썬 변수를 직접 사용할 수도 있습니다.

"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