python中tkinter出选择题_Python从Tkinter列表框获取多个选择的问题

我终于找到了自己问题的答案。如果您想从列表框中捕获多个响应,这非常有用。我评论了很多。希望有帮助!在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值