Python ValueError:<;tkinter.OptionMenu对象。!选项菜单>;不在列表中
我正在尝试打印另一个列表中列表索引的对应值,如下所示: 打印(安全指数[化学指数(自下降2)]) 但是当我这样做的时候,我得到了上面的错误。我相信我在上一次迭代中已经实现了这一点,但是我找不到已经实现的Python ValueError:<;tkinter.OptionMenu对象。!选项菜单>;不在列表中,python,tkinter,indexing,Python,Tkinter,Indexing,我正在尝试打印另一个列表中列表索引的对应值,如下所示: 打印(安全指数[化学指数(自下降2)]) 但是当我这样做的时候,我得到了上面的错误。我相信我在上一次迭代中已经实现了这一点,但是我找不到已经实现的 import tkinter as tk from tkinter import ttk safeDis = [4,88,18,50,12,100] chem = ["HTP 50%","HTP 84%","HTP 90%",&quo
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()
问题在于
self.drop2
是OptionMenu
的对象,而不是它的值。要获取它返回的值,请对其定义的变量使用get()
方法(self.clicked2.get()
)
因此,它应该是:
print(safeDis[chem.index(self.clicked2.get())])
希望它解决了错误,如果还有任何疑问请告诉我
干杯试着说
self.drop2.get()
像print(safeDis[chem.index(self.drop2.get()))
我得到了错误“AttributeError:'OptionMenu'对象没有属性'get'”。哦,对不起,实际上我认为self.clicked2.get()
用于print(safeDis[chem.index(self.clicked2.get()))
使用过的打印(safeDis[chem.index(self.clicked2.get())]),这就解决了它。非常感谢您,这非常有帮助