2012-08-08 3 views
3

의심 할 바없이 이것은 초보자 용 질문입니다. Python 2.7에서 Tkinter의 그리드 레이아웃 관리자를 사용하고 있습니다. 클릭시 목록 상자를 숨기는 버튼을 원합니다. 여기 내 코드는 지금까지의 :그리드 레이아웃이있는 Tkinter 리프트 및 로우 메서드

from Tkinter import * 
root = Tk() 
frame = Frame(root) 
pyList = ["Eric", "Terry", "Graham", "Terry", "John", "Carol?", "Michael"] 
arbList = ['ham', 'spam', 'eggs', 'potatos', 'tots', 'home fries'] 
pythons = Listbox(frame, width=10, height=5, selectmode=EXTENDED, exportselection=0) 
food = Listbox(frame, width=10, height=5, selectmode=EXTENDED, exportselection=0) 
def hider(): 
    if pythons.selection_includes(4): 
     food.lower() 
    elif pythons.selection_includes(0): 
     food.lift() 
b2 = Button(frame, text="Hide!", command=hider) 
b2.grid(row=2, column=1) 
food.grid(row=0, column=1) 
pythons.grid(row=1, column=1, pady=10) 
frame.grid() 

for python in pyList: 
     pythons.insert('end', python) 

for thing in arbList: 
     food.insert('end', thing) 


root.mainloop() 

불행하게도,이로 돌아온다 것은 내가/리프트 내 프레임 위 또는 아래에 내 목록 상자를 낮출 수 없다는 오류가 발생 나타납니다. pack() 관리자를 사용하지만 grid()는 사용할 수 없습니다.

무엇이 누락 되었습니까?

답변

1

상위 아래에 위젯을 낮출 수 없습니다. official tk docs에 따르면

aboveThis 만약 인수 명령이 그래서 스택 순서에서의 형제 자매 (이 어떤 형제에 의해 가려하지 않습니다 모든 위에 을 창을 제기하고를 모호합니다 다음 생략 형제 자매는 입니다. above가 지정되면 윈도우의 형제 인 윈도우의 경로 이름이거나 윈도우의 형제 인 의 자손이어야합니다. 이 경우 raise 명령은 창 을 위의 스태킹 순서 (또는 창 크기가 위의 위의 스태킹 순서)에 삽입합니다. 이것은 창을 올리거나 내릴 수 있습니다.

프레임 및 목록 상자의 형제 자매을 원하는 효과를 얻으려면, 다음을 포장하기 위해 in_ 매개 변수를 사용하여 (NB는. TK에 raise 명령은 lift() 실제로 가장 낮은 수준에서 호출하는 것입니다) 프레임 안의 목록 상자 :

food.grid(row=0, column=1, in_=frame) 
pythons.grid(row=1, column=1, pady=10, in_=frame) 
+0

와우, 그 것이 아름답게 작동했습니다. 나는 그것이 단순한 무엇인지 알았다. 나는 형제 자매와 부모에게 충분한 생각을하지 않은 것 같아요. 도와 주셔서 감사합니다! – JMarotta

0

죄송하지만 어느 코드도 나에게 도움이되지 않습니다. 그러나 다음 수정은 작동합니다. 트릭은 목록 상자의 부모를 frame 대신 root으로하고 liftlowerframe에 대해 상대적으로 갖는 것입니다. 이것이 Tkinter의 다른 버전들 때문인지 궁금합니다.

from Tkinter import * 
root = Tk() 
frame = Frame(root) 
pyList = ["Eric", "Terry", "Graham", "Terry", "John", "Carol?", "Michael"] 
arbList = ['ham', 'spam', 'eggs', 'potatos', 'tots', 'home fries'] 
pythons = Listbox(root, width=10, height=5, selectmode=EXTENDED, exportselection=0) 
food = Listbox(root, width=10, height=5, selectmode=EXTENDED, exportselection=0) 
def hider(): 
    if pythons.selection_includes(4): 
     food.lower(frame) 
    elif pythons.selection_includes(0): 
     food.lift(frame) 
b2 = Button(frame, text="Hide!", command=hider) 
b2.grid(row=2, column=1) 

food.grid(row=0, column=1, in_=frame) 
pythons.grid(row=1, column=1, pady=10, in_=frame) 
frame.grid() 

for python in pyList: 
     pythons.insert('end', python) 

for thing in arbList: 
     food.insert('end', thing) 


root.mainloop() 
+0

거의 확실하게 코드의 버그입니다. 위젯을 올리거나 내릴 수있는 능력은 20 년 후에도 변하지 않았습니다. 그러나 목록 상자와 프레임이 모두 형제가되어야 제대로 작동합니다. listbox는'lift' 명령이 원하는대로 동작 할 수 있도록 프레임의 자식이 될 수 없습니다. –

관련 문제