我需要在同一个 tkinter 组合框上执行多个绑定语句,但只有第一个可以工作
I need to do multiple bind statements on the same tkinter combobox, but only the first one will work
我试图将多个绑定语句添加到一个组合框中,但它不起作用。
我有一个 tkinter window 可以将数据输入到 sql 数据库中,该数据库具有多个相关的组合框,以便在输入错误的内容会导致致命错误的地方更容易输入数据。州名组合框会将州缩写排列在一个组合框中,另一个组合框用于该州可用的国家公园名称。选择的公园随后会触发另一个组合框,其中唯一的选项应该是公园的缩写。我遇到的问题是将州缩写和公园名称组合框绑定到州组合框。它们在两个单独的语句中,我一次只能得到一个来工作。我必须注释掉状态缩写绑定才能使用公园名称和公园缩写组合框,反之亦然。有什么想法吗?
from tkinter import *
from tkinter import ttk
# list of states
states = ['Alaska', 'Arizona', 'Arkansas']
# dictionaries
STATE_DICT = {
'state_name': {
'AK': 'Alaska',
'AR': 'Arkansas',
'AZ': 'Arizona'}
}
PARKS_DICT = {
'state_name': {
'DENA': 'Alaska',
'GLBA': 'Alaska',
'KATM': 'Alaska',
'KEFJ': 'Alaska',
'HOSP': 'Arkansas',
'GRCA': 'Arizona',
'PEFO': 'Arizona',
'SAGU': 'Arizona'
},
'park_name': {
'DENA': 'Denali National Park',
'GLBA': 'Glacier Bay National Park',
'KATM': 'Katmai National Park',
'KEFJ': 'Kenai Fjords National Park',
'HOSP': 'Hot Springs National Park',
'GRCA': 'Grand Canyon National Park',
'PEFO': 'Petrified Forest National Park',
'SAGU': 'Saguaro National Park'}
}
root2 = Tk()
root2.title("data_entry_form")
root2.geometry("800x500")
state_name_label = Label(root2, text="State Name", width=20, font=("bold", 12),bd=1, relief="raised")
state_abbr_label = Label(root2, text="State Abbr", width=20, font=("bold", 12),bd=1, relief="raised")
park_name_label = Label(root2, text="Park Name", width=20, font=("bold", 12),bd=1, relief="raised")
park_abbr_label = Label(root2, text="Park Abbr", width=20, font=("bold", 12),bd=1, relief="raised")
nearest_city_label = Label(root2, text="Nearest City", width=20, font=("bold", 12),bd=1, relief="raised")
geo_location_label = Label(root2, text="Geo Location", width=20, font=("bold", 12),bd=1, relief="raised")
hike_name_label = Label(root2, text="Hike Name", width=20, font=("bold", 12),bd=1, relief="raised")
length_miles_label = Label(root2, text="Length in Miles", width=20, font=("bold", 12),bd=1, relief="raised")
elevation_gain_feet_label = Label(root2, text="Elevation in Feet", width=20, font=("bold", 12),bd=1, relief="raised")
nps_difficulty_rating_label = Label(root2, text="NPS Difficulty Rating", width=20, font=("bold", 12),bd=1, relief="raised")
route_type_label = Label(root2, text="Route Type", width=20, font=("bold", 12),bd=1, relief="raised")
# Data entry fields
hike_name_entry = Entry(root2,width=100)
nearest_city_entry = Entry(root2,width=50)
geo_location_entry = Entry(root2,width=50)
length_miles_entry = Entry(root2,width=50)
elevation_gain_feet_entry = Entry(root2,width=50)
# submit button
submitbutton = Button(root2, text='Submit New Hike!', width=20, bg="black", fg='white')
# comboboxes
def parks_list(e):
p=list()
x=list()
for key,value in PARKS_DICT['state_name'].items():
if value==state_combo.get():
p.append(key)
for k,v in PARKS_DICT['park_name'].items():
if k in p:
x.append(v)
park_combo.config(value=x)
def park_abb(e):
p=list()
for key,value in PARKS_DICT['park_name'].items():
if value==park_combo.get():
p.append(key)
park_abbr_combo.config(value=p)
def state_abb(e):
p=list()
for key,value in STATE_DICT['state_name'].items():
if value==state_combo.get():
p.append(key)
state_abbr_combo.config(value=p)
state_combo = ttk.Combobox(root2, value=states)
state_combo.current(0)
#state_combo.bind("<<ComboboxSelected>>", state_abb) #-have to # this out to get the other statement to work.
state_combo.bind("<<ComboboxSelected>>", parks_list)
state_abbr_combo = ttk.Combobox(root2, value=[" "])
state_abbr_combo.current(0)
park_combo = ttk.Combobox(root2, value=[" "])
park_combo.current(0)
park_combo.bind("<<ComboboxSelected>>", park_abb)
park_abbr_combo = ttk.Combobox(root2, value=[" "])
park_abbr_combo.current(0)
nps_difficulty_rating= ttk.Combobox(root2, value =range(1,6))
route_type_combo=ttk.Combobox(root2, value=['loop','out and back','point to point'])
# Grid placement
state_name_label.grid(row=1, column=0, padx=10, pady=10)
state_combo.grid(row=1, column=1, padx=10, pady=10)
state_abbr_label.grid(row=2, column=0, padx=10, pady=10)
state_abbr_combo.grid(row=2, column=1, padx=10, pady=10)
park_name_label.grid(row=3, column=0, padx=10, pady=10)
park_combo.grid(row=3, column=1, padx=10, pady=10)
park_abbr_label.grid(row=4, column=0, padx=10, pady=10)
park_abbr_combo.grid(row=4, column=1, padx=10, pady=10)
nearest_city_label.grid(row=5, column=0, padx=10, pady=10)
nearest_city_entry.grid(row=5, column=1, padx=10, pady=10)
geo_location_label.grid(row=6, column=0, padx=10, pady=10)
geo_location_entry.grid(row=6, column=1, padx=10, pady=10)
hike_name_label.grid(row=7, column=0, padx=10, pady=10)
hike_name_entry.grid(row=7, column=1, padx=10, pady=10)
length_miles_label.grid(row=8, column=0, padx=10, pady=10)
length_miles_entry.grid(row=8, column=1, padx=10, pady=10)
elevation_gain_feet_label.grid(row=9, column=0, padx=10, pady=10)
elevation_gain_feet_entry.grid(row=9, column=1, padx=10, pady=10)
nps_difficulty_rating_label.grid(row=10, column=0, padx=10, pady=10)
nps_difficulty_rating.grid(row=10, column=1, padx=10, pady=10)
route_type_label.grid(row=11, column=0, padx=10, pady=10)
route_type_combo.grid(row=11, column=1, padx=10, pady=10)
submitbutton.grid(row=12, column=1, padx=10, pady=10)
root2.mainloop()
虽然可以进行多个绑定,但我认为这样做没有任何好处。相反,创建一个调用其他函数的函数。
def state_combo_changed(event):
state_abb(event)
parks_list(abb)
state_combo.bind("<<ComboboxSelected>>", state_combo_changed)
如果您坚持要进行两个单独的绑定,您可以将 add
属性设置为 True
:
state_combo.bind("<<ComboboxSelected>>", state_abb)
state_combo.bind("<<ComboboxSelected>>", parks_list, add=True)
我试图将多个绑定语句添加到一个组合框中,但它不起作用。
我有一个 tkinter window 可以将数据输入到 sql 数据库中,该数据库具有多个相关的组合框,以便在输入错误的内容会导致致命错误的地方更容易输入数据。州名组合框会将州缩写排列在一个组合框中,另一个组合框用于该州可用的国家公园名称。选择的公园随后会触发另一个组合框,其中唯一的选项应该是公园的缩写。我遇到的问题是将州缩写和公园名称组合框绑定到州组合框。它们在两个单独的语句中,我一次只能得到一个来工作。我必须注释掉状态缩写绑定才能使用公园名称和公园缩写组合框,反之亦然。有什么想法吗?
from tkinter import *
from tkinter import ttk
# list of states
states = ['Alaska', 'Arizona', 'Arkansas']
# dictionaries
STATE_DICT = {
'state_name': {
'AK': 'Alaska',
'AR': 'Arkansas',
'AZ': 'Arizona'}
}
PARKS_DICT = {
'state_name': {
'DENA': 'Alaska',
'GLBA': 'Alaska',
'KATM': 'Alaska',
'KEFJ': 'Alaska',
'HOSP': 'Arkansas',
'GRCA': 'Arizona',
'PEFO': 'Arizona',
'SAGU': 'Arizona'
},
'park_name': {
'DENA': 'Denali National Park',
'GLBA': 'Glacier Bay National Park',
'KATM': 'Katmai National Park',
'KEFJ': 'Kenai Fjords National Park',
'HOSP': 'Hot Springs National Park',
'GRCA': 'Grand Canyon National Park',
'PEFO': 'Petrified Forest National Park',
'SAGU': 'Saguaro National Park'}
}
root2 = Tk()
root2.title("data_entry_form")
root2.geometry("800x500")
state_name_label = Label(root2, text="State Name", width=20, font=("bold", 12),bd=1, relief="raised")
state_abbr_label = Label(root2, text="State Abbr", width=20, font=("bold", 12),bd=1, relief="raised")
park_name_label = Label(root2, text="Park Name", width=20, font=("bold", 12),bd=1, relief="raised")
park_abbr_label = Label(root2, text="Park Abbr", width=20, font=("bold", 12),bd=1, relief="raised")
nearest_city_label = Label(root2, text="Nearest City", width=20, font=("bold", 12),bd=1, relief="raised")
geo_location_label = Label(root2, text="Geo Location", width=20, font=("bold", 12),bd=1, relief="raised")
hike_name_label = Label(root2, text="Hike Name", width=20, font=("bold", 12),bd=1, relief="raised")
length_miles_label = Label(root2, text="Length in Miles", width=20, font=("bold", 12),bd=1, relief="raised")
elevation_gain_feet_label = Label(root2, text="Elevation in Feet", width=20, font=("bold", 12),bd=1, relief="raised")
nps_difficulty_rating_label = Label(root2, text="NPS Difficulty Rating", width=20, font=("bold", 12),bd=1, relief="raised")
route_type_label = Label(root2, text="Route Type", width=20, font=("bold", 12),bd=1, relief="raised")
# Data entry fields
hike_name_entry = Entry(root2,width=100)
nearest_city_entry = Entry(root2,width=50)
geo_location_entry = Entry(root2,width=50)
length_miles_entry = Entry(root2,width=50)
elevation_gain_feet_entry = Entry(root2,width=50)
# submit button
submitbutton = Button(root2, text='Submit New Hike!', width=20, bg="black", fg='white')
# comboboxes
def parks_list(e):
p=list()
x=list()
for key,value in PARKS_DICT['state_name'].items():
if value==state_combo.get():
p.append(key)
for k,v in PARKS_DICT['park_name'].items():
if k in p:
x.append(v)
park_combo.config(value=x)
def park_abb(e):
p=list()
for key,value in PARKS_DICT['park_name'].items():
if value==park_combo.get():
p.append(key)
park_abbr_combo.config(value=p)
def state_abb(e):
p=list()
for key,value in STATE_DICT['state_name'].items():
if value==state_combo.get():
p.append(key)
state_abbr_combo.config(value=p)
state_combo = ttk.Combobox(root2, value=states)
state_combo.current(0)
#state_combo.bind("<<ComboboxSelected>>", state_abb) #-have to # this out to get the other statement to work.
state_combo.bind("<<ComboboxSelected>>", parks_list)
state_abbr_combo = ttk.Combobox(root2, value=[" "])
state_abbr_combo.current(0)
park_combo = ttk.Combobox(root2, value=[" "])
park_combo.current(0)
park_combo.bind("<<ComboboxSelected>>", park_abb)
park_abbr_combo = ttk.Combobox(root2, value=[" "])
park_abbr_combo.current(0)
nps_difficulty_rating= ttk.Combobox(root2, value =range(1,6))
route_type_combo=ttk.Combobox(root2, value=['loop','out and back','point to point'])
# Grid placement
state_name_label.grid(row=1, column=0, padx=10, pady=10)
state_combo.grid(row=1, column=1, padx=10, pady=10)
state_abbr_label.grid(row=2, column=0, padx=10, pady=10)
state_abbr_combo.grid(row=2, column=1, padx=10, pady=10)
park_name_label.grid(row=3, column=0, padx=10, pady=10)
park_combo.grid(row=3, column=1, padx=10, pady=10)
park_abbr_label.grid(row=4, column=0, padx=10, pady=10)
park_abbr_combo.grid(row=4, column=1, padx=10, pady=10)
nearest_city_label.grid(row=5, column=0, padx=10, pady=10)
nearest_city_entry.grid(row=5, column=1, padx=10, pady=10)
geo_location_label.grid(row=6, column=0, padx=10, pady=10)
geo_location_entry.grid(row=6, column=1, padx=10, pady=10)
hike_name_label.grid(row=7, column=0, padx=10, pady=10)
hike_name_entry.grid(row=7, column=1, padx=10, pady=10)
length_miles_label.grid(row=8, column=0, padx=10, pady=10)
length_miles_entry.grid(row=8, column=1, padx=10, pady=10)
elevation_gain_feet_label.grid(row=9, column=0, padx=10, pady=10)
elevation_gain_feet_entry.grid(row=9, column=1, padx=10, pady=10)
nps_difficulty_rating_label.grid(row=10, column=0, padx=10, pady=10)
nps_difficulty_rating.grid(row=10, column=1, padx=10, pady=10)
route_type_label.grid(row=11, column=0, padx=10, pady=10)
route_type_combo.grid(row=11, column=1, padx=10, pady=10)
submitbutton.grid(row=12, column=1, padx=10, pady=10)
root2.mainloop()
虽然可以进行多个绑定,但我认为这样做没有任何好处。相反,创建一个调用其他函数的函数。
def state_combo_changed(event):
state_abb(event)
parks_list(abb)
state_combo.bind("<<ComboboxSelected>>", state_combo_changed)
如果您坚持要进行两个单独的绑定,您可以将 add
属性设置为 True
:
state_combo.bind("<<ComboboxSelected>>", state_abb)
state_combo.bind("<<ComboboxSelected>>", parks_list, add=True)