如何存储用户从列表框中选择的输入

时间:2012-04-30 21:10:50

标签: python-2.7

这很像旅行商问题。我有一个带有大学名字的Listbox(支持我从Facebook Graph中获取的坐标)。我将选择模式设置为多个。我需要知道允许我使用他们选择的大学的代码,所以我可以通过远程方法。我只需要知道代码就能看到他们选择的内容。我尝试使用curselection(),但我仍然不明白。

以下是一些代码:

    self.listbox = Listbox(self.mid_frame,width = 42,selectmode ="multiple",
                                        highlightcolor = "orange",
                                        highlightthickness = "10",bd = "5")

    coordinates = []
    collegelist = []
    f = open(sys.argv[1],'r')
    # grab the college's lat and long from facebook graph
    for identity in f:
        urlquery='https://graph.facebook.com/'+identity
        obj = json.load(urllib2.urlopen(urlquery))
        college = obj["name"]
        latitude = obj["location"]["latitude"]
        longitude = obj["location"]["longitude"]
        coordinates.append((college,latitude, longitude))
        collegelist.append(college)

    #sort the colleges so they appear alphabetical order
    sortcollege = sorted(collegelist)
    #fill Listbox with the College names imported from a text file
    for college in sortcollege:
        self.listbox.insert(END, college)

    self.listbox.pack(side = LEFT)
    #The label where I would put the total distance
    self.output_totaldist_label = Label(self.mid_frame,
                                    width = 11,
                                    textvariable = self.totaldistance)
    self.totaldistance = StringVar()
    self.output_label = Label(self.mid_frame,
                              textvariable = self.totaldistance)
    self.output_totaldist_label.pack(side = LEFT)
    self.output_label.pack(side = LEFT)

1 个答案:

答案 0 :(得分:2)

很高兴看到你如何尝试诅咒,看看出了什么问题。

类似的东西:

for idx in self.listbox.curselection():
    selitem = self.listbox.get(idx)

应该做的伎俩。你试过了吗?