2014-12-04 1 views
0

GUI를 초기화 할 때 Toplevel 위젯 중 하나를 보이지 않게하려면 2 개의 최상위 위젯과 루트 프레임이있는 GUI를 생성합니다. 루트 프레임의 버튼을 클릭하여 볼 수 있습니다. 나는 선 삽입 할 수는 GUI가 초기화 될 때 최상위 레벨 위젯이 보이지 않는 것 (전체 GUI를 구축)을 start_window 코드Toplevel 위젯을 표시하고 사라지게하는 방법

win2.withdraw() 

을,하지만 난 다시 그리는 방법을 알아낼 수 없습니다 일단 내가 보길 원한다면 위젯을 표시하십시오. 내가 이것을 시도 할 때

win2.deiconify() 

원하는 기능을 제공하지만 다음과 같은 오류 메시지가 나타납니다 : 나는 명령을 예상 여기

AttributeError: start_window instance has no attribute 'deiconify' 

내 코드, 돌이() 에드, 작성 GUI. 이 코드에서 사용하는 두 개의 개별 위젯 클래스가 있습니다. 하나는 ScrolledList 위젯이고 다른 하나는 ScrolledText 위젯입니다. GUI를 시작한 다음 ScrolledText 위젯을 볼 수있게하는 루트 프레임의 버튼을 클릭하면 보이지 않게되는 ScrolledText 위젯입니다.

class start_window(Frame): 
def __init__(self, parent=None): 
    Frame.__init__(self, parent) 
    #self.pa() 
    Frame.pack(self) 
    win1 = Toplevel() 
    win2 = Toplevel() 
    Label(self, text = 'Bioasys DataBase', width = 30).pack() 
    btn = Button(self, text='Make Widget Visible', command=win2.master.deiconify()) 
    btn.pack(side=TOP) 
    win1.title('Company Lookup') 
    win2.title('Company Information') 
    ScrolledList(win1).pack(side=TOP, fill=BOTH) 

    #stock_sym = 'acad' 

    #text1, text2 = create_company_information(stock_sym) 


    try: 
     stco_name = ScrolledText(win2, file=sys.argv[1], width= 50, height=15).pack() 
    except IndexError: 
     #text1 = 'Company Name - Company Symbol\n\nCompany URL\n\nCompany Address Line 1\nCompany Address Line 2\ncompany Address Line 3\nCompany Address Line 4' 
     stco_name = ScrolledText(win2, text= text1, width= 50, height=15).pack() 

    try: 
     stco_sym = ScrolledText(win2,file=sys.argv[1], width=50, height=15).pack() 
    except IndexError: 
     #text2 = 'Company Description' 
     stco_sym = ScrolledText(win2, text= text2, width=50, height=15).pack() 

    win2.withdraw() # makes the win2 frame invisible at startup 
    #win2.pack_forget() 
    #win2.master.deiconify() 

두 개의 다른 상위 위젯의 코드는 다음과 같습니다.이 위젯 클래스는 모두 올바르게 작동합니다. 이 코드를 포함하여 start_window 클래스가 제대로 작동하도록 만들 수 있습니다.

class ScrolledList(Frame): 
def __init__(self, parent=None): 
    Frame.__init__(self, parent) 
    self.pack(expand=YES, fill=BOTH) 
    self.makeWidgets() 
def handleList(self, event): 
    index = self.listbox.curselection() 
    label = self.listbox.get(index) 
    self.runCommand(label) 
def fetch(self): 
    print 'Input => "%s"' % self.ent.get() 

def makeWidgets(self): 
    self.ent = Entry(self) 
    btn = Button(self, text='ENTER', command=self.fetch) 
    sbar = Scrollbar(self) 
    list = Listbox(self, relief=SUNKEN) 
    self.ent.insert(0, 'Type Stock Symbol Here') 
    self.ent.pack(side=TOP, fill=X) 
    self.ent.focus() 
    self.ent.bind('<Return>', (lambda event: self.fetch())) 
    value = self.ent.get() 
    btn.pack(side=TOP) 
    sbar.config(command=list.yview) 
    list.config(yscrollcommand=sbar.set) 
    sbar.pack(side=RIGHT, fill=Y) 
    list.pack(side=LEFT, expand=YES, fill=BOTH) 
    options_init = open_pickled_company_list() # 
    options = [i[1] for i in options_init]  # 
    for label in options: 
     list.insert('end', label) 
    #list.config(selectmode=SINGLE, setgrid=1) 
    list.bind('<Double-1>', self.handleList) 
    self.listbox = list 


def runCommand(self, selection): 
    print 'You selected: ', selection 
    self.ent.delete(0, END) 
    self.ent.insert(0, selection) 



class ScrolledText(Frame): 
def __init__(self, parent=None, text='', file=None, width='', height=''): 
    Frame.__init__(self, parent) 
    self.pack(expand=YES, fill=BOTH)    # make me expandable 
    self.width = width 
    self.height = height 
    self.makewidgets() 
    self.settext(text, file) 
def makewidgets(self): 
    sbar = Scrollbar(self) 
    text = Text(self, relief=SUNKEN, width=self.width, height=self.height) 
    sbar.config(command=text.yview)     # xlink sbar and text 
    text.config(yscrollcommand=sbar.set)    # move one moves other 
    sbar.pack(side=RIGHT, fill=Y)     # pack first=clip last 
    text.pack(side=LEFT, expand=YES, fill=BOTH)  # text clipped first 
    self.text = text 
def settext(self, text='', file=None): 
    if file: 
     text = open(file, 'r').read() 
    self.text.delete('1.0', END)      # delete current text 
    self.text.insert('1.0', text)     # add at line 1, col 0 
    self.text.mark_set(INSERT, '1.0')    # set insert cursor 
    self.text.focus()        # save user a click 
def gettext(self):         # returns a string 
    return self.text.get('1.0', END+'-1c')   # first through last 

그리고 여기에는 GUI intiates 돌이() 명령입니다 : 내가 보이지 않는 최상위 레벨 Win2 및 위젯 내 GUI를 시작 찾고 있어요 요약 그래서

start_window().mainloop() 

를하지만의 버튼을 클릭하여 루트 프레임 Win2 위젯을 볼 수 있습니다. 아무도이 작업을 수행하는 방법에 대한 아이디어가 있다면 도움을 주시면 감사하겠습니다.

답변

0

당신이 쓴 :

I expected the command: win2.deiconify() to provide the desired functionality but I get the following error message when I attempt this:

AttributeError: start_window instance has no attribute 'deiconify'

무슨 말을하고 어떤 오류 메시지가 다른 두 가지가 말했다. win2.deiconify()이 아닌 start_window.deiconify()으로 전화하십시오.

win2.deiconify() 실제로 보이지 않는 최상위 창을 표시하는 방법입니다. win2Toplevel의 인스턴스에 대한 참조입니다.

이 솔루션은 콜백에서 참조 할 수 있도록 최상위 창에 대한 참조를 저장하는 작업을 포함합니다.

클래스 start_window (프레임) : 데프 초기화 (자기 부모 = 없음) : ... self.win2 = 최상위 레벨() ... BTN = 버튼 (자기, 텍스트 예를 들어, = '위젯 보이기', command = self.win2.deiconify)

코드에 미묘한 버그가 있습니다. 원래 코드에는 command=self.win2.master.deiconif())이 있습니다. command 특성은 함수를 참조합니다. 대신 deiconify 메서드를 호출하고 반환되는 값이 command 특성에 할당됩니다. 자네가 더 복잡해진다.win2에는 master 속성이 없습니다. (: command=self.win2.deiconify) 예)

+0

안녕하세요, 당신의 응답을 주셔서 감사합니다

이 솔루션은 self.win2command 속성을 설정하는 것입니다. start_window가 Frame 서브 클래스이고 따라서 (에러 메시지에 따라) deiconify 메소드가 없다는 것을 이해합니다. 어떻게하는지 이해하지 못하면 현재 코드 설정에서 deiconify 메소드가 작동합니다. start_window 클래스를 Frame의 하위 클래스로 만들지 않아도됩니까? 당신의 도움을 주셔서 감사합니다. 감사합니다. George – user3798654

+0

"start_window 인스턴스에 'deiconify'속성이 없지만 사용중인 명령은"command = win2.master.deiconify() "입니다. (마스터 부분은 오타입니다. using은 실제로 win2.deiconify()입니다. 그래서 win2를 지정하는 이유를 모르지만 오류 메시지가 start_window를 지정하고 있습니다. 내가 사용하는 버튼/명령이 start_window 내에 있지만 사실 루트 프레임 (이 경우에는 Frame에 서브 클래 싱 된 start_window) 내에서 Gui (이 경우 win2)의 한 측면을 제어 할 수없는 것은 사실입니까? 도와 주셔서 감사합니다. 감사합니다. George – user3798654

+0

@ user3798654 : 코드를 다시 확인해야합니다. 파이썬 오류는 대개 거짓말을하지 않습니다. 'start_window.deiconify()'에 에러가 있다고 말하면, 그것이 정확히 호출 된 코드라고 생각하는 것이 안전합니다. –

관련 문제