2014-02-10 4 views
0

'a'변수의 선택 항목 아래에 텍스트를 표시하여 메뉴의 레이블로 표시하려고합니다. 코드는 다음과 같습니다.tkinter의 메뉴 레이블에 텍스트 전달

def popup(event): 
    a=t_start.get("sel.first", "sel.last") 
    menu.post(event.x_root, event.y_root) 
    return a 

def insert_word(): 
    pass 

t_start.bind("<Button-3>", popup) 


menu = Menu(root, tearoff=0) 
menu.add_command(label="Set selection 1", command=set_selection_1) 
menu.add_command(label="Set selection 2", command=set_selection_2) 
menu.add_command(label="%s" %popup, command=set_selection_2) 

바로 지금 나는 모든 기능 팝업 주소를 얻었습니다. popup.a를 시도하면 오류가 발생하며 함수에 'a'속성이 없습니다. 이를 어떻게 극복하고 메뉴 라벨로 인쇄 할 'a'가 무엇이든 얻을 수 있습니까?

+0

AFAIK, 콜백 메소드는 아무 것도 반환하지 않습니다. 'popup' 메쏘드 안에서 메뉴에'a '를 추가하십시오. 즉,'menu.add_command (label = a, command = ...)'를 거기에 옮겨보십시오. –

+0

@tobias_k - 덕분에 많은 일을했습니다. 나는 전에 이것을 시도했지만 분명히 오타가 있었거나 뭔가가있었습니다. – kaboom

+0

기꺼이 도와 드리겠습니다. 나는 이것을 답으로 추가했다. –

답변

0

콜백 방법이 아무것도 반환 안된다 예를 들어, 당신이해야 할 첫 번째 항목의 텍스트를 변경합니다. 대신 menu에서 내부로 조작해야합니다.

또한 Brian이 제안한 것처럼 버튼을 클릭 할 때마다 새 항목을 추가하는 대신 메뉴의 기존 항목을 수정하려고합니다. 이 경우 함수 밖에서 항목을 만들고 (지금처럼) 레이블에 일부 자리 표시자를 사용하십시오.

def popup(event): 
    a = t_start.get("sel.first", "sel.last") 
    menu.post(event.x_root, event.y_root) 
    menu.add_command(label=a, command=set_selection_2) # add to menu 
    # menu.entryconfig(2, label=a) # modify existing entry 
0

메뉴의 텍스트를 변경하려면 entryconfig 방법을 사용해야합니다. popup 같은

menu.entryconfig(0, label=a) 
관련 문제