【问题标题】:Python Tkinter multiple selection ListboxPython Tkinter 多选列表框
【发布时间】:2012-12-30 23:46:39
【问题描述】:

我尝试在这里搜索,但没有找到正确的答案。
我有一个使用selection='multiple' 设置的列表框。 然后我尝试通过代码name.get(ACTIVE) 获取用户选择的所有选项的列表。 问题是它并不总是能得到我在列表框 GUI 中突出显示的所有选项。

如果我突出显示一个,它会正确地恢复它。
如果我突出显示两个或更多(通过单击每个)它只返回我选择的最后一个项目
如果我有多个突出显示,然后单击以取消突出显示一个,则即使未突出显示,也会返回我单击的最后一个。

任何帮助都会非常棒。谢谢。我希望代码能够恢复突出显示的内容。

设置列表框的代码是:

self.rightBT3 = Listbox(Frame1,selectmode='multiple',exportselection=0)

检索选择的代码是:

selection = self.rightBT3.get(ACTIVE)

这是应用程序运行状态的屏幕截图,在顶部您可以看到控制台只注册了一个选择(我点击的最后一个)。

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    似乎在 Tkinter 列表框中获取所选项目列表的正确方法是使用 self.rightBT3.curselection(),它返回一个包含所选行的从零开始的索引的元组。然后,您可以使用这些索引get() 每一行。

    (虽然我还没有实际测试过)

    【讨论】:

    • 听起来不错@Tharwen 谢谢。我曾希望我错过了一个让我避免使用curselection() 的技巧,但我只需要停止懒惰:)
    【解决方案2】:

    我发现上述解决方案有点“晦涩”。特别是当我们在这里与正在学习工艺或学习 python/tkinter 的程序员打交道时。

    我想出了一个更具解释性的解决方案,如下所示。我希望这对你有更好的效果。

    #-*- coding: utf-8 -*-
    # Python version 3.4
    # The use of the ttk module is optional, you can use regular tkinter widgets
    
    from tkinter import *
    from tkinter import ttk
    
    main = Tk()
    main.title("Multiple Choice Listbox")
    main.geometry("+50+150")
    frame = ttk.Frame(main, padding=(3, 3, 12, 12))
    frame.grid(column=0, row=0, sticky=(N, S, E, W))
    
    valores = StringVar()
    valores.set("Carro Coche Moto Bici Triciclo Patineta Patin Patines Lancha Patrullas")
    
    lstbox = Listbox(frame, listvariable=valores, selectmode=MULTIPLE, width=20, height=10)
    lstbox.grid(column=0, row=0, columnspan=2)
    
    def select():
        reslist = list()
        seleccion = lstbox.curselection()
        for i in seleccion:
            entrada = lstbox.get(i)
            reslist.append(entrada)
        for val in reslist:
            print(val)
    
    btn = ttk.Button(frame, text="Choices", command=select)
    btn.grid(column=1, row=1)
    
    main.mainloop()
    

    请注意,ttk 主题小部件的使用是完全可选的。您可以使用普通 tkinter 的小部件。

    【讨论】:

      【解决方案3】:

      要获得在列表框中选择的文本项列表,我发现以下解决方案是最优雅的:

      selected_text_list = [listbox.get(i) for i in listbox.curselection()]
      

      【讨论】:

      • 是的,这是一个更好的答案......对我有用
      【解决方案4】:

      我也遇到了同样的问题。经过一些研究,我找到了一个可行的解决方案,它允许在列表框中进行多项选择。这并不理想,因为字段中仍然缺少滚动条(一个 UX 线索,表明存在附加值)。但是,它确实允许多选。

      from tkinter import *
      window_app = Tk()
      
      
      # ### Allows for multi-selections ###
      def listbox_used(event):
          curselection = listbox.curselection()
          for index in curselection:
              print(listbox.get(index))  # Gets current selection from listbox
              # Only challenge with this implementation is the incremental growth of the list.
              # However, this could be resolved with a Submit button that gets the final selections.
      
      
      listbox = Listbox(window_app, height=4, selectmode='multiple')
      fruits = ["Apple", "Pear", "Orange", "Banana", "Cherry", "Kiwi"]
      for item in fruits:
          listbox.insert(fruits.index(item), item)
      listbox.bind("<<ListboxSelect>>", listbox_used)  # bind function allows any selection to call listbox_used function.
      listbox.pack(padx=10, pady=10)  # Adds some padding around the field, because...fields should be able to breathe :D
      
      window_app.mainloop()
      

      免责声明:我只参加了 100 天训练营的第 27 天。如果我遗漏了一些明显的东西,我欢迎提供建设性的反馈。

      另外,这是我在 StackOverflow 上的第一篇文章 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-07
        • 2014-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多