2013-04-19 5 views
0

나는 Python과 Tkinter로 GUI 애플리케이션을 준비 중이다. (나는이 언어를 가진 초보자이다).Python/Tkinter : 서브 윈도우에서 입력 값을 읽음

def config_open(): 
    global wdw, e 
    wdw = Toplevel() 
    wdw.geometry('+400+400') 

    w = Label(wdw, text="Parameter 1", justify=RIGHT) 
    w.grid(row=1, column=0) 
    e = Entry(wdw) 
    e.grid(row=1, column=1) 
    e.focus_set() 

그럼 내가 "OK"버튼을 호출 추가하는 것이 :

는 제가 열립니다 일부 텍스트 매개 변수를 사용하여 메인 윈도우와 설정 하위 윈도우가

def config_save(): 
    global wdw, e 
    user_input = e.get().strip() 
    print user_input 

를이 나는 모든 것을 글로벌이라고 선언하고 있습니다. 서브 윈도우 내부의 요소를 참조하는 더 좋은 방법이 있습니까?

+0

'확인'버튼은 어디에 있습니까? 기본 창 또는 구성 창에서? – kalgasnik

+0

그것은 당신을 위해 유용 할 수 있습니다 : http://stackoverflow.com/questions/10718073/how-to-create-child-window-and-communicate-with-parent-in-tkinter – kalgasnik

+0

OK 버튼은 설정 창 안에 있습니다. . – flip79

답변

1
from Tkinter import * 

def config_open(): 
    wdw = Toplevel() 
    wdw.geometry('+400+400') 
    # Makes window modal 
    wdw.grab_set() 

    # Variable to store entry value 
    user_input = StringVar() 
    Label(wdw, text="Parameter 1", justify=RIGHT).grid(row=1, column=0) 
    e = Entry(wdw, textvariable=user_input) 
    e.grid(row=1, column=1) 
    e.focus_set() 
    Button(wdw, text='Ok', command=wdw.destroy).grid(row=2, column=1) 
    # Show the window and wait for it to close 
    wdw.wait_window(wdw) 
    # Window has been closed 
    data = {'user_input': user_input.get().strip(), 
      'another-option': 'value of another-option'} 
    return data 

class App: 
    def __init__(self): 
     self.root = Tk() 
     self.root.geometry('+200+200') 
     self.label_var = StringVar() 
     self.user_input = None 
     Button(self.root, text='Configure', command=self.get_options).place(relx=0.5, rely=0.5, anchor=CENTER) 
     Label(self.root, textvariable=self.label_var).place(relx=0.5, rely=0.3, anchor=CENTER) 
     self.root.mainloop() 

    def get_options(self): 
     options = config_open() 
     self.user_input = options['user_input'] 
     self.label_var.set(self.user_input) 

App() 
관련 문제