Python Tkinter-调节组合框中菜单或多选项的显示项目数

Python Tkinter-调节组合框中菜单或多选项的显示项目数,python,tkinter,combobox,Python,Tkinter,Combobox,我有一个很大的国家列表,我希望用户从中选择一个或多个国家 我找到了符合我确切需要的解决方案: 唯一仍然不理想的是,菜单下拉菜单与屏幕加上下拉菜单一样大 是否有可能限制显示的项目数量?e、 g.10项,然后使用现有的向下/向上滚动 我知道这对于Tkinters组合框是可能的,但我不可能在那里进行多重选择 下面是我的示例代码: countries = ['Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Anguilla', 'An

我有一个很大的国家列表,我希望用户从中选择一个或多个国家

我找到了符合我确切需要的解决方案:

唯一仍然不理想的是,菜单下拉菜单与屏幕加上下拉菜单一样大

是否有可能限制显示的项目数量?e、 g.10项,然后使用现有的向下/向上滚动

我知道这对于Tkinters组合框是可能的,但我不可能在那里进行多重选择

下面是我的示例代码:

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()
编辑:

我使用了@JohnT answer,但添加了一些功能,因此用户不必确认每个选择:

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()

这里有一个链接,我从中得到了输入:

我相信Listbox会处理你想要的一切,更多关于这个小部件的信息请点击这里

下面是使用列表框的示例代码,该列表框可滚动,并允许通过打印输出多项选择

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()

listBox = tk.Listbox(frame, width=20, height=20, 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.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))

def select():
    output = []
    selection = listBox.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)

outBtn = tk.Button(window, text = 'print selection', command = select)
outBtn.pack()

window.mainloop()

谢谢你@JohnT。这不是使用菜单按钮或组合框,但它可能是更好的解决方案!我添加了一些小的调整,所以在选择一个或多个选项后,列表会在选择后自动更新。谢谢你的正确答案!