1

我正在尝试制作一个下拉菜单以从列表中选择一个项目,并使用该项目从另一个列表中选择第二个项目,最后使用最后一个从数据库中提取数据。现在我可以选择第一个选项菜单,但无论出于何种原因,我都无法使用第二个选项菜单。无论我改变什么,选择总是保持我设置的样子。

import tkinter as tk

station =""


def chosse_station(*args):

    global segments_list, list1, list2, station_choose,  root,stations_list
    #print (segments_list)
    print (segment.get())
    if segment.get() == segments_list[0]:
        stations_list = list1

    else:
        stations_list = list2



    station = tk.StringVar(root)
    station.set("choose a station")
    opt1 = tk.OptionMenu(root, station, *stations_list)
    opt1.pack()
    opt1.place(bordermode='outside', rely=00.13, relx=0.10, height=50, width=200)
    station_choose = station.get()
    print (station.get())
    station.trace("w", choose_meter)


def choose_meter(*args):

    global segments_list, list1, list2, Tags_list, Data , station_choose
    print(segment.get())
    print(station_choose)

    Tags_list = Data[station_choose]
    tag = tk.StringVar(root)
    tag.set(Data[station_choose][0])
    opt1 = tk.OptionMenu(root, tag, *Tags_list)

    opt1.pack()
    opt1.place(bordermode='outside', rely=00.13, relx=0.25, height=50, width=200)
    print(Data[station_choose])
    print(tag.get())





FIELD_TAG_DATA_RAW =0

date = '03/08/2020'
chosen_date = {}
segments_list = ['Magpie - Bicentannial', 'Dickenson H22 - Dodge']
list1 = ['Magpie' , 'Bicentannial' , 'Trottor']
list2 = ['Dickenson' , 'Dodge']
Magpie = ['Error', 'DATA[2]', 'DATA[3]', 'DATA[1]', 'DATA[9]','DATA[4]']
Trottor = ['_Error', 'DATA[5]', 'DATA[6]', 'DATA[7]', 'DATA[8]']
Bicentannial=['_Error1', 'DATA[36]', 'DATA[39]', 'DATA[40]']
Dickenson = ['Error', 'DATA[12]', 'DATA[13]', 'DATA[11]', 'DATA[19]','DATA[14]']
Dodge = ['Error', 'DATA[42]', 'DATA[43]', 'DATA[41]', 'DATA[43]','DATA[44]']
Data = {'Magpie': Magpie, 'Bicentannial': Bicentannial, 'Trottor': Trottor,'Dickenson': Dickenson , 'Dodge': Dodge }


Name = 'REAL_DATA[8]'

root = tk.Tk()
canvas = tk.Canvas(root, height=950, width=1600, bg= '#263D42')
canvas.pack()



frame= tk.Frame(root, bg='white')
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.15)


segment = tk.StringVar(root)
segment.set(segments_list[0])
opt = tk.OptionMenu(root, segment, *segments_list)
#print(segment.get())
segment.trace("w", chosse_station)

opt.place(bordermode='outside' ,rely = 00, relx = 0.10,height=50, width=200)

sations_list =list1
Tags_list=[]


root.mainloop()
4

1 回答 1

1

stringvar您为第二个绑定了错误OptionMenu,并且您没有使用 global 在另一个函数中定义它。

现在你的代码应该是:

import tkinter as tk

station =""


def chosse_station(*args):

    global segments_list, list1, list2, station_choose,  root,stations_list, station
    #print (segments_list)
    print (segment.get())
    if segment.get() == segments_list[0]:
        stations_list = list1

    else:
        stations_list = list2



    station = tk.StringVar(root)
    station.set("choose a station")
    opt1 = tk.OptionMenu(root, station, *stations_list)
    opt1.pack()
    opt1.place(bordermode='outside', rely=00.13, relx=0.10, height=50, width=200)
    station_choose = station.get()
    print (station.get())
    station.trace("w", choose_meter)


def choose_meter(*args):

    global segments_list, list1, list2, Tags_list, Data , station_choose, station
    print(segment.get())
    print(station.get())

    Tags_list = Data[station.get()]
    tag = tk.StringVar(root)
    tag.set(Data[station.get()][0])
    opt1 = tk.OptionMenu(root, tag, *Tags_list)

    opt1.pack()
    opt1.place(bordermode='outside', rely=00.13, relx=0.25, height=50, width=200)
    print(Data[station.get()])
    print(tag.get())





FIELD_TAG_DATA_RAW =0

date = '03/08/2020'
chosen_date = {}
segments_list = ['Magpie - Bicentannial', 'Dickenson H22 - Dodge']
list1 = ['Magpie' , 'Bicentannial' , 'Trottor']
list2 = ['Dickenson' , 'Dodge']
Magpie = ['Error', 'DATA[2]', 'DATA[3]', 'DATA[1]', 'DATA[9]','DATA[4]']
Trottor = ['_Error', 'DATA[5]', 'DATA[6]', 'DATA[7]', 'DATA[8]']
Bicentannial=['_Error1', 'DATA[36]', 'DATA[39]', 'DATA[40]']
Dickenson = ['Error', 'DATA[12]', 'DATA[13]', 'DATA[11]', 'DATA[19]','DATA[14]']
Dodge = ['Error', 'DATA[42]', 'DATA[43]', 'DATA[41]', 'DATA[43]','DATA[44]']
Data = {'Magpie': Magpie, 'Bicentannial': Bicentannial, 'Trottor': Trottor,'Dickenson': Dickenson , 'Dodge': Dodge }


Name = 'REAL_DATA[8]'

root = tk.Tk()
canvas = tk.Canvas(root, height=950, width=1600, bg= '#263D42')
canvas.pack()



frame= tk.Frame(root, bg='white')
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.15)


segment = tk.StringVar(root)
segment.set(segments_list[0])
opt = tk.OptionMenu(root, segment, *segments_list)
#print(segment.get())
segment.trace("w", chosse_station)

opt.place(bordermode='outside' ,rely = 00, relx = 0.10,height=50, width=200)

sations_list =list1
Tags_list=[]


root.mainloop()
于 2020-03-14T05:26:38.793 回答