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

问题描述 投票:0回答:1

我有很多国家/地区,我希望用户从中选择一个或多个国家/地区。

我找到了适合我确切需要的解决方案:How do I enable multiple selection of values from a combobox?

唯一仍然不理想的是菜单下拉菜单与屏幕以及下拉菜单一样大。

是否有可能限制显示的项目数量?例如10个项目,然后使用已经存在的向下/向上滚动。

我知道这对Tkinters Combobox是可行的,但我在那里没有多重选择的可能性。

这是我的示例代码:

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()
python tkinter combobox
1个回答
0
投票

我相信Listbox会处理您想要的一切,有关此小部件的更多信息,请点击此处http://effbot.org/tkinterbook/listbox.htm

这里是使用列表框的示例代码,该列表框是可滚动的,并允许通过打印输出多个选择内容

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()
© www.soinside.com 2019 - 2024. All rights reserved.