2014-04-20 3 views
1

내 코드에는 두 개의 버튼이 있습니다 - 첫 번째를 클릭하면 프로그램이 "집"창에 씁니다. 두 번째는 "검색"창과 "검색"창에 씁니다. 검색 창. 내 문제는 검색 버튼을 두 번 (또는 그 이상) 클릭하면 검색 창이 더 많이 생성된다는 것입니다. 어떻게 해결할 수 있습니까? (나는 항상 오직 하나의 검색 창을 원한다). 대신 홈 및 검색을 할 수엔트리 위젯 : 하나 이상의 검색 창을 피하십시오

class App(): 
    def __init__(self):  
     self.window = Tk() 

     self.text=Label(self.window, text="Some text") 
     self.text.pack() 
     button_home = Button(self.window, text='Home',command= self.home) 
     button_home.pack() 
     button_search = Button(self.window, text='Search', command=self.search) 
     button_search.pack() 

     self.has_entry = False 

    def home(self): 
     self.text['text'] = 'home' 

    def search(self): 
     self.text["text"] = 'search' 
     if not self.has_entry: 
      self.meno = StringVar() # NOTE - change meno to self.meno so you can 
            # access it later as an attribute 
      m = Entry(self.window, textvariable=self.meno).pack() 
      self.has_entry = True 

이 더욱 이동하려면 :

from tkinter import * 

class App(): 
    def __init__(self):  
     self.window = Tk() 

     self.text=Label(self.window, text="Some text") 
     self.text.pack() 
     button_home = Button(self.window, text='Home',command= self.home) 
     button_home.pack() 
     button_search = Button(self.window, text='Search', command=self.search) 
     button_search.pack() 

    def home(self): 
     self.text['text'] = 'home' 

    def search(self): 
     self.text["text"] = 'search' 
     meno = StringVar() 
     m = Entry(self.window, textvariable=meno).pack() 

답변

1

당신이해야 할 모든 응용 프로그램의 항목이 아직 생성되었는지 여부를 나타내는 변수를 추가입니다 버튼은 입력 창 부품이 실제로 으로 표시되는지 여부를 제어합니다. 당신은 항목의 .pack.pack_forget 방법을 사용하여이 작업을 수행 할 수 있습니다 :

class App(): 
    def __init__(self):  
     self.window = Tk() 

     self.text=Label(self.window, text="Some text") 
     self.text.pack() 
     button_home = Button(self.window, text='Home',command= self.home) 
     button_home.pack() 
     button_search = Button(self.window, text='Search', command=self.search) 
     button_search.pack() 

     self.meno = StringVar() 
     self.entry = Entry(self.window, textvariable=self.meno) 

    def home(self): 
     self.text['text'] = 'home' 
     self.entry.pack_forget() 

    def search(self): 
     self.text["text"] = 'search' 
     self.entry.pack() 

희망이 도움이!

+0

감사합니다. 많은 도움이됩니다. – user3498993

관련 문제