简体   繁体   中英

ValueError: <tkinter.OptionMenu object .!optionmenu> is not in list

I am trying to print a corresponding value to the index of a list from another list like so:

print(safeDis[chem.index(self.drop2)])

but when doing this i get the above error. I believe i had this working in a previous iteration but i cannot find the one that was.

import tkinter as tk
from tkinter import ttk

safeDis = [4,88,18,50,12,100]
chem = ["HTP 50%","HTP 84%","HTP 90%","Kerosene","Benzene"]

class Page4:
    def __init__(self,root):
        self.root = root           
        self.toplbl = ttk.Label(root, text="Select firing point meterials ",font=("arial",12)) #lable for select meterial 1
        self.lbl1 = ttk.Label(root, text="Meterial 1: ",font=("arial",10)) #lable for select meterial 1
        self.lbl2 = ttk.Label(root, text = "Meterial 2: " ,font = ("arial",10)) #lable for meterial 2 
        self.masslbl = ttk.Label(root, text="Mass in Kg:",font=("arial",10)) 
        self.masslbl2 = ttk.Label(root, text="Mass in Kg:",font=("arial",10)) 
        self.typelbl = ttk.Label(root, text="Type:",font=("arial",10))
        self.typelbl2 = ttk.Label(root, text="Type:",font=("arial",10))
        self.Apply = ttk.Button(root, text="Apply", command = self.new_window)  #button to confirm the meterial choices      
        self.Back = ttk.Button(root, text="Back", command = print("DONG"))
        self.mass1 = ttk.Entry(root, width=8)                       
        self.mass2 = tk.Entry(root,width=8)
        self.clicked = tk.StringVar()   #set the variable to a string value allowing the meterial names to apeer in it
        self.clicked.set(chem[3])    #set the default meterial from the chem list
        self.clicked2 = tk.StringVar()
        self.clicked2.set(chem[3])
        self.drop2 = tk.OptionMenu(root, self.clicked2, *chem)   #setup the dropdown menue with optionmenue function set to the chem list
        self.drop = tk.OptionMenu(root, self.clicked, *chem) 
        
        self.toplbl.grid(column=0, row=0,columnspan=3,sticky="w",padx=10,pady=10)    #place meterial label 1
        self.lbl1.grid(column=0, row=1,padx=10)    #place meterial label 1
        self.lbl2.grid(column=0, row=3,padx=10)      #place meterial label 2
        self.mass1.grid(column=2, row=2) 
        self.mass2.grid(column=2, row=4)
        self.masslbl.grid(column=1, row=2)  
        self.masslbl2.grid(column=1, row=4)            
        self.typelbl.grid(column=1, row=1,sticky="w") 
        self.typelbl2.grid(column=1, row=3,sticky="w")
        self.drop.grid(column=2, row=1)        #place the dropdown menue
        self.drop2.grid(column=2, row=3)                                                               
        self.Apply.grid(column=2,row=5,pady=10,padx=10) 
        self.Back.grid(column=1,row=5,pady=10,padx=10)
        print(safeDis[chem.index(self.drop2)])
    def new_window(self):       
        
        #print(dongalong) 
        for widget in self.root.winfo_children():
            widget.destroy()              
        self.app = Page3(self.root)
#class Page5:
def main(): 
    root = tk.Tk()
    app = Page4(root)
    root.mainloop()

if __name__ == '__main__':
    main()

The problem was that self.drop2 is an object of OptionMenu , not the value of it. To get the value returned by it, use the get() method on its variable defined ( self.clicked2.get() )

So it should be:

print(safeDis[chem.index(self.clicked2.get())])

Hope it solved the error, do let me know if any more doubts

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM