[英]Python multiple if statements in one function
我是一名新的 Python 程序員。 我希望用戶從三個項目(藍色、紫色或黃色)之一的下拉菜單中選擇 select,並且根據他們的選擇,我希望在表單的另一部分顯示一組卷。 我正在嘗試在一個 function 中編寫多個“if”語句。 如所寫,它將執行第一個“if”語句和“else”,但不會執行中間的兩個“if”語句。 我究竟做錯了什么?
def selected(event):
if Clicked.get() == "Blue 50-1,000 µl":
Volume1 = Label(root, text="0.1000")
Volume2 = Label(root, text="0.5000")
Volume3 = Label(root, text="1.0000")
Volume1.place(x=175, y=520)
Volume2.place(x=525, y=520)
Volume3.place(x=825, y=520)
elif Clicked.get() == "Yellow 5-100 µl":
Volume1 = Label(root, test="0.0100")
Volume2 = Label(root, text="0.0500")
Volume3 = Label(root, text="0.1000")
Volume1.place(x=175, y=520)
Volume2.place(x=525, y=520)
Volume3.place(x=825, y=520)
elif Clicked.get() == "Purple 0.2 – 5 ml":
Volume1 = Label(root, test="0.5000")
Volume2 = Label(root, text="2.5000")
Volume3 = Label(root, text="5.000")
Volume1.place(x=175, y=520)
Volume2.place(x=525, y=520)
Volume3.place(x=825, y=520)
else:
Volume1 = Entry(root)
Volume2 = Entry(root)
Volume3 = Entry(root)
Volume1.place(x=175, y=520, width=75)
Volume2.place(x=525, y=520, width=75)
Volume3.place(x=825, y=520, width=75)
此 function 正在使用以下內容:
#Pipette Options Selection List
Pipette_Options = [
"Select",
"Blue 50-1,000 µl",
"Yellow 5-100 µl",
"Purple 0.2 – 5 ml"
]
#Drop-Down for Pipettes
Clicked = StringVar()
Clicked.set(Pipette_Options[0])
Pipette_Drop = OptionMenu(root, Clicked, *Pipette_Options, command=selected)
Pipette_Drop.place(x=280, y=445)
改用字典:
def f1():
Volume1 = Label(root, text="0.1000")
Volume2 = Label(root, text="0.5000")
Volume3 = Label(root, text="1.0000")
Volume1.place(x=175, y=520)
Volume2.place(x=525, y=520)
Volume3.place(x=825, y=520)
def f2():
Volume1 = Label(root, test="0.0100")
Volume2 = Label(root, text="0.0500")
Volume3 = Label(root, text="0.1000")
Volume1.place(x=175, y=520)
Volume2.place(x=525, y=520)
Volume3.place(x=825, y=520)
def f3():
Volume1 = Label(root, test="0.0100")
Volume2 = Label(root, text="0.0500")
Volume3 = Label(root, text="0.1000")
Volume1.place(x=175, y=520)
Volume2.place(x=525, y=520)
Volume3.place(x=825, y=520)
d = {"Blue 50-1,000 µl": f1,
"Yellow 5-100 µl": f2,
"Purple 0.2 – 5 ml": f3}
if d.has_key(Clicked.get()):
d[Clicked.get()]()
else:
print("another action...")
Python 一旦發現命中就停止 if-elif-else 語句。 因此, if Clicked.get() == "Blue 50-1,000 µl"
返回True
,它將不會繼續評估 elif/else 語句的 rest。 據我可以從您的代碼中確定,這應該是經過深思熟慮的。
如果您希望 'if' 和 'else' 像您所說的那樣一起運行,為什么不將它們合並在一起。 我認為您的代碼需要更多描述。 你到底想做什么?
看起來else
選項在您的程序中是不可能的(或至少是不可取的)。 一旦擺脫它,您可以獲得一個非常靈活的解決方案:
def selected(event):
opt = Clicked.get()
params = {
"Blue 50-1,000 µl":
{"text": ["0.1000", "0.5000", "1.0000"],
"x" : [175, 525, 520],
"y" : [520, 520, 520]},
# The other two options go here
}
volumes = [Label(root, text=params[opt]["text"][i]) for i in range(3)]
for i in range(3):
volumes[i].place(x=params[opt]["x"][i], y=params[opt]["y"][i])
這是一個錯字:
比較這些行:
Volume1 = Label(root, test="0.0100")
Volume2 = Label(root, text="0.0500")
_tkinter.TclError:未知選項“-test”
關閉標志
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.