从Tkinter列表框中获取所选项目而不使用列表框绑定

时间:2015-07-13 14:04:21

标签: python-2.7 tkinter listbox

我有2个列表框(它们是连接的,因此项目可以从一个移动到另一个),最后我想通过使用“确定”按钮获取第二个列表框中的所有条目(或者只是关闭帧)。每次选择一个项目时,我都可以在列表中添加/删除值(如下面代码的注释部分所示),但我宁愿在<head>的{​​{1}}行中添加一行代码。 1}}功能,但我无法使其正常工作。

代码:

[master.selected.get(idx) for idx in master.selected.curselection()]

我试图在关闭函数中使用以下小片段:

close

但是,def measurementPopup(self,master): self.chargeCarrier = StringVar() self.massModifiers = StringVar() self.chargeCarrier.set("[M+xH]") def onselect1(evt): w = evt.widget index = int(w.curselection()[0]) value = w.get(index) # My Dirty fix -> Here I could enter the selected value to a buffer list (to be returned in the ok function). master.selected.insert(END,value) master.avail.delete(index) def onselect2(evt): w = evt.widget index = int(w.curselection()[0]) value = w.get(index) # My Dirty fix -> Here I could remove the selected value from a buffer list (to be returned in the ok function). master.selected.delete(index) master.avail.insert(END,value) def close(self): # Here I would return the buffer list and close the window master.measurementWindow = 0 top.destroy() if master.measurementWindow == 1: return master.measurementWindow = 1 top = self.top = Toplevel() top.protocol( "WM_DELETE_WINDOW", lambda: close(self)) self.charge = Label(top, text = "Charge", width = 10) self.charge.grid(row = 0, column = 0, sticky = W) self.min = Label(top, text = "Min", width = 5) self.min.grid(row=0, column = 1, sticky = W) self.minCharge = Spinbox(top, from_= 1, to = 3, width = 5) self.minCharge.grid(row = 0, column = 2, sticky = W) self.max = Label(top, text = "Max", width = 5) self.max.grid(row = 0, column = 3, sticky = W) self.maxCharge = Spinbox(top, from_ = 1, to=3, width=5) self.maxCharge.grid(row = 0, column = 4, sticky = W) self.chargeCarrier = OptionMenu(top, self.chargeCarrier, "[M+xH]", "[M+xNa]") self.chargeCarrier.grid(row = 0, column = 5, sticky = W) self.availMass = Label(top, text = "Available") self.availMass.grid(row = 1, column = 1, sticky = W) self.selectMass = Label(top, text = "Selected") self.selectMass.grid(row = 1, column = 3, sticky = W) self.massMod = Label(top, text = "Mass Mods") self.massMod.grid(row = 2, column = 0, sticky = W) self.avail = Listbox(top) for i in UNITS: if BLOCKS[i]['available'] == 1: self.avail.insert(END,BLOCKS[i]['human_readable_name']) self.avail.grid(row = 2, column = 1, columnspan = 2, sticky = W) self.avail.bind('<<ListboxSelect>>',onselect1) self.selected = Listbox(top) self.selected.grid(row = 2, column = 3, columnspan = 2, sticky = W) self.selected.bind('<<ListboxSelect>>',onselect2) self.ok = Button(top,text = 'Ok',command = lambda: close(self)) self.ok.grid(row = 3, column = 0, sticky = W) 细分不返回任何内容。我希望这是因为实际上没有选择任何东西,但我需要一些相反的东西,沿着 values = [master.selected.get(idx) for idx in master.selected.curselection()] print ', '.join(values) (如果它存在并且如果我理解正确的话)。

摘要

如何获得1个特定列表框中的所有项目?

1 个答案:

答案 0 :(得分:3)

.get()窗口小部件的Listbox函数允许您指定一系列项目,可以指定为0END以返回所有项目的元组项目。

示例:

from Tkinter import *
root = Tk()
l = Listbox(root, width = 15)
l.pack()
l.insert(END, "Hello")
l.insert(END, "world")
l.insert(END, "here")
l.insert(END, "is")
l.insert(END, "an")
l.insert(END, "example")

def close():
    global l, root
    items = l.get(0, END)
    print(items)
    root.destroy()

b = Button(root, text = "OK", command = close).pack()
root.mainloop()

我希望这有帮助,如果它不是你想要的,请在评论中告诉我,我可以尝试扩展我的答案。