gpt4 book ai didi

python - Python 从 Tkinter 列表框中获取多个选择的问题

转载 作者:行者123 更新时间:2023-11-30 23:39:09 26 4
gpt4 key购买 nike

这与我今天早些时候提出的问题相同,你们中的一些人试图帮助我解决这个问题,但我无法让它发挥作用。我想做的就是用单击列表框时所做的多个选择来填充“ichose”。

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

无论我做什么,“ichose”都会以空元组 () 的形式出现。很明显,函数“entered”从未被调用,因为我从未看到测试字符串“hello”。

我也不知道“双按钮-”、“<>”等各种选项是什么。在哪里可以找到每个选项的列表和说明?

如果有人可以修改我的程序以便“print ichose”起作用,我将非常感激。你可以从我的程序中看到,我并不真正知道自己在做什么,但很想学习。谢谢。

最佳答案

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

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

关于python - Python 从 Tkinter 列表框中获取多个选择的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13828531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com