根据另一个OptionMenu中选择的内容更改OptionMenu,并使用该选择从数据库中提取数据

问题描述 投票:0回答: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()
python tkinter optionmenu tkinter.optionmenu
1个回答
0
投票

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