Tkinter - 调节组合框中菜单或多选项的显示数量
Tkinter - regulate the number of displayed items of a Menu OR multiselection in 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()
编辑:
我使用了@JohnT 答案,但添加了一些功能,因此用户不必确认每个选择:
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()
这是 link t post 我从以下位置得到的输入:Getting a callback when a Tkinter Listbox selection is changed?
我相信 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()
我有一个很大的国家列表,我希望用户从中选择一个或多个国家。
我发现这个解决方案完全符合我的需要:
唯一还不够理想的是下拉菜单和屏幕一样大加上一个下拉菜单。
是否可以限制显示的项目数量?例如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()
编辑:
我使用了@JohnT 答案,但添加了一些功能,因此用户不必确认每个选择:
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()
这是 link t post 我从以下位置得到的输入:Getting a callback when a Tkinter Listbox selection is changed?
我相信 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()