Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable multiple selection of values from a combobox?

Python 3.4.3, Windows 10, Tkinter

I am attempting to create a combobox that allows for multiple selections from the dropdown. I have found similar work for listbox (Python Tkinter multiple selection Listbox), but cannot get it work with the combobox.

Is there a simple way to enable multiple selection from the dropdown of the combobox?

like image 526
jknicely Avatar asked Dec 31 '15 17:12

jknicely


People also ask

Can you select multiple items in a combobox?

A multi-select combo box allows you to select multiple values from a picklist. You can use this type of field whenever you need to create an item that can take more than one value.

How can we select multiple values from dropdown?

Selecting multiple options vary in different operating systems and browsers: For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.

What is combobox tkinter?

Introduction to the Tkinter Combobox widget A combobox is a combination of an Entry widget and a Listbox widget. A combobox widget allows you to select one value in a set of values. In addition, it allows you to enter a custom value.


1 Answers

By design the ttk combobox doesn't support multiple selections. It is designed to allow you to pick one item from a list of choices.

If you need to be able to make multiple choices you can use a menubutton with an associated menu, and add checkbuttons or radiobuttons to the menu.

Here's an example:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        menubutton = tk.Menubutton(self, text="Choose wisely", 
                                   indicatoron=True, borderwidth=1, relief="raised")
        menu = tk.Menu(menubutton, tearoff=False)
        menubutton.configure(menu=menu)
        menubutton.pack(padx=10, pady=10)

        self.choices = {}
        for choice in ("Iron Man", "Superman", "Batman"):
            self.choices[choice] = tk.IntVar(value=0)
            menu.add_checkbutton(label=choice, variable=self.choices[choice], 
                                 onvalue=1, offvalue=0, 
                                 command=self.printValues)
    def printValues(self):
        for name, var in self.choices.items():
            print "%s: %s" % (name, var.get())

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
like image 128
Bryan Oakley Avatar answered Oct 03 '22 20:10

Bryan Oakley