2012-12-11 2 views
1

이것은 이전에 제기 한 것과 동일한 문제이며 두 사람이 저를 도우 려 시도했지만 작동시키지 못합니다. 내가하고 싶은 일은 목록 상자를 클릭 할 때 내가 선택한 여러 항목으로 "일각표"를 채우는 것입니다.파이썬에서 Tkinter에서 여러 선택 항목을 가져 오는 중 문제가 발생했습니다.

import Tkinter as tk 
from Tkinter import * 
global ichose 

class App(tk.Frame): 
    def __init__(self, master): 
     tk.Frame.__init__(self,master) 
     self.master=master 
     self.grid() 
     self.ichose =() 

     self.l = Listbox(self, height=10, selectmode=EXTENDED) 
     # Selectmode can be SINGLE, BROWSE, MULTIPLE or EXTENDED. Default BROWSE 
     self.l.grid(column=0, row=0, sticky=(N,W,E,S)) 
     self.l.bind("Double-Button-1", self.entered) 

     s = Scrollbar(self, orient=VERTICAL, command=self.l.yview) 
     s.grid(column=0, row=0, sticky=(N,S,E)) 
     self.l['yscrollcommand'] = s.set 

     for i in range(1,101): 
      self.l.insert('end', 'Line %d of 100' % i) 


    def entered(self, event): 
     self.ichose = self.selection_get() 
     self.ichose = ('hello') 

root=tk.Tk() 
root.title('Listbox Problem') 
root.geometry('200x200') 
app=App(root) 
root.mainloop() 

print app.ichose 

"무엇이든지"빈 튜플()로 나옵니다. "hello"라는 테스트 문자열을 보지 못하기 때문에 "entered"함수가 호출되지 않습니다.

"Double-Button-", "<>"등과 같이 다양한 옵션이 무엇인지 알지 못합니다. 각 목록 및 설명은 어디에서 찾을 수 있습니까?

"인쇄 ichose"가 작동하도록 누군가 제 프로그램을 수정 해줄 수 있다면 정말 감사 할 것입니다. 내 프로그램에서 볼 수있는 것은 내가하고있는 일을 실제로 알지 못하고 있지만 배우고 싶어한다는 점입니다. 고맙습니다.

답변

2

나는 내 자신의 질문에 대한 답을 찾았습니다. 이것은 목록 상자에서 여러 응답을 캡처하려는 경우에 유용합니다. 나는 많이 논평했다. 희망이 도움이됩니다!

import Tkinter as tk 
from Tkinter import * 

class App(tk.Frame): 
    def __init__(self, master): 
     tk.Frame.__init__(self,master) 
     self.master=master 
     self.grid() 
     self.ichose = [] 

     self.l = Listbox(self, height=10, selectmode=MULTIPLE) 
     # Selectmode can be SINGLE, BROWSE, MULTIPLE or EXTENDED. Default BROWSE 
     self.l.grid(column=0, row=0, sticky=(N,W,E,S)) 

     s = Scrollbar(self, orient=VERTICAL, command=self.l.yview) 
     s.grid(column=0, row=0, sticky=(N,S,E)) 
     self.l['yscrollcommand'] = s.set 

     for i in range(1,101): 
      self.l.insert('end', 'Line %d of 100' % i) 

     # Create Textbox that will display selected items from list 
     self.selected_list = Text(self,width=20,height=10,wrap=WORD) 
     self.selected_list.grid(row=12, column=0, sticky=W)   

     # Now execute the poll() function to capture selected list items 
     self.ichose = self.poll() 

    def poll(self): 
     items =[] 
     self.ichose = [] 
     # Set up an automatically recurring event that repeats after 200 millisecs 
     self.selected_list.after(200, self.poll) 
     # curselection retrieves the selected items as a tuple of strings. These 
     # strings are the list indexes ('0' to whatever) of the items selected. 
     # map applies the function specified in the 1st parameter to every item 
     # from the 2nd parameter and returns a list of the results. So "items" 
     # is now a list of integers 
     items = map(int,self.l.curselection()) 

     # For however many values there are in "items": 
     for i in range(len(items)): 
      # Use each number as an index and get from the listbox the actual 
      # text strings corresponding to each index, and append each to 
      # the list "ichose". 
      self.ichose.append(self.l.get(items[i])) 
     # Write ichose to the textbox to display it. 
     self.update_list() 
     return self.ichose 

    def update_list(self): 
     self.selected_list.delete(0.0, END) 
     self.selected_list.insert(0.0, self.ichose) 


root=tk.Tk() 
root.title('Listbox Multi-Capture') 
root.geometry('200x340') 
app=App(root) 
root.mainloop() 

print app.ichose 
1
# ----------------[ Listbox EXAMPLE ]---------------- 

    self.sarcCountries = (
     "Bangladesh", 
     "India", 
     "Pakistan", 
     "Nepal", 
     "Bhutan", 
     "Sri Lanka", 
     "Afghanistan" 
    ) 

    self.listData = StringVar(value = self.sarcCountries) 

    self.listbox = Listbox(
     master   = self, 
     height   = 10, 
     listvariable  = self.listData, 
     selectmode  = MULTIPLE, 
     selectbackground = "#BC80CC" 
    ) 

    self.listbox.bind("<<ListboxSelect>>", self.OnListboxSelectionChanged) 

    self.listbox.pack(fill = BOTH, expand = 0, padx = 10, pady = 10) 
# ----------------[ Listbox EXAMPLE ]---------------- 


def OnListboxSelectionChanged(self, val): 
    # NOTE: If your listbox's select mode is MULTIPLE, then you may use this portion of code 
    selections = val.widget.curselection() 

    print("---------------------------") 

    if (selections !=()): 
     for index in selections: 
      print(self.sarcCountries[int(index)]) 

    print("---------------------------") 
관련 문제