I have a big list of countries which I want the user to choose one or more countries from.
I found this solution which is fitting my exact need: How do I enable multiple selection of values from a combobox?
The only thing that is still not ideal is that the menu dropdown is as big as the screen plus a dropdown menu.
Is there a possibility to limit the number of items shown? e.g. 10 Items and then use the already existing scroll down / up.
I know that this is possible for Tkinters Combobox
but I don´t have the possibility of MultiSelection there.
Here is my example code:
countries = ['Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Anguilla', 'Antigua And Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda', 'Bhutan', 'Bolivia', 'Bosnia And Herzegovina', 'Botswana', 'Bouvet Island', 'Brazil', 'British Virgin Islands', 'Brunei', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Cayman Islands', 'Chad', 'Chile', 'China', 'Colombia', 'Comoros', 'Congo', 'Cook Islands', 'Costa Rica', 'Croatia', 'Curacao', 'Cyprus', 'Czech Republic', 'Democratic Republic Of The Congo', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Timor', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Estonia', 'Ethiopia', 'Faroe Islands', 'Fiji', 'Finland', 'France', 'French Guiana', 'French Polynesia', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Gibraltar', 'Greece', 'Greenland', 'Grenada', 'Guadeloupe', 'Guam', 'Guatemala', 'Guernsey', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Honduras', 'Hong Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Isle Of Man', 'Israel', 'Italy', 'Ivory Coast', 'Jamaica', 'Japan', 'Jersey', 'Jordan', 'Kazakhstan', 'Kenya', 'Kosovo', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Libyan Arab Jamahiriya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macao', 'Macau', 'Macedonia', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Martinique', 'Mauritania', 'Mauritius', 'Mexico', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nepal', 'Netherlands', 'Netherlands Antilles', 'New Caledonia', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Norway', 'Oman', 'Pakistan', 'Palestine', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Poland', 'Portugal', 'Puerto Rico', 'Qatar', 'Reunion', 'Romania', 'Russia', 'Russian Federation', 'Rwanda', 'Saint Kitts And Nevis', 'Saint Lucia', 'Saint Martin', 'Saint Pierre And Miquelon', 'Saint Vincent And The Grenadines', 'Samoa', 'San Marino', 'Saudi Arabia', 'Senegal', 'Serbia', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Somalia', 'South Africa', 'South Korea', 'South Sudan', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Swaziland', 'Sweden', 'Switzerland', 'Taiwan', 'Tajikistan', 'Tanzania', 'Tanzania, United Republic Of', 'Thailand', 'Togo', 'Tonga', 'Trinidad And Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Turks And Caicos Islands', 'U.S. Virgin Islands', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Venezuela', 'Vietnam', 'Wallis And Futuna', 'Yemen', 'Zambia', 'Zimbabwe']
def country_confirmation():
ChooseVAC_choosen_Country = [k for k,v in ChooseVAC_CountryChoices.items() if v.get() == 1]
if ChooseVAC_choosen_Country != []:
country_bool = True
else:
country_bool = False
print ("country is / countries are:", ChooseVAC_choosen_Country)
from tkinter import *
root = Tk()
ChooseVAC_choosen_Country = []
country_bool = False
ChooseVAC_countries_StringVar = StringVar()
ChooseVAC_countrySelection = Menubutton(root,text='Choose wisely',indicatoron=True, borderwidth=1, relief="raised")
ChooseVAC_menu = Menu(ChooseVAC_countrySelection, tearoff=False)
ChooseVAC_countrySelection.configure(menu=ChooseVAC_menu)
ChooseVAC_CountryChoices = {}
for CountryChoice in countries:
ChooseVAC_CountryChoices[CountryChoice] = IntVar(value=0)
ChooseVAC_menu.add_checkbutton(label=CountryChoice, variable=ChooseVAC_CountryChoices[CountryChoice],
onvalue=1, offvalue=0,
command=country_confirmation)
ChooseVAC_countrySelection.grid(row=0,column=1,sticky='nsew')
root.mainloop()
EDIT:
I used @JohnT answer but added some functionality so the user doesn´t have to confirm each selection:
import tkinter as tk
window = tk.Tk()
#generic window size, showing listbox is smaller than window
window.geometry("600x480")
frame = tk.Frame(window)
frame.pack()
def select(evt):
event = evt.widget
output = []
selection = event.curselection()
#.curselection() returns a list of the indexes selected
#need to loop over the list of indexes with .get()
for i in selection:
o = listBox.get(i)
output.append(o)
print(output)
listBox = tk.Listbox(frame, width=20, height = 5, selectmode='multiple')
#height/width are characters wide and tall,
#height = 20 will show first 20 items in list
#change font size to scale to desired height once @ number of items shown
#i recommend setting width to the length of the name of your longest country name +1
listBox.bind('<<ListboxSelect>>',select)
listBox.pack(side="left", fill="y")
scrollbar = tk.Scrollbar(frame, orient="vertical")
scrollbar.config(command=listBox.yview)
scrollbar.pack(side="right", fill="y")
listBox.config(yscrollcommand=scrollbar.set)
for x in range(100):
listBox.insert('end', str(x))
window.mainloop()
Here is a link t the post I got the input from: Getting a callback when a Tkinter Listbox selection is changed?