我有一個if塊,我需要在其中進行一些比較,例如:
if type(widget1) is tk.Entry or type(widget1) is tk.OptionMenu or type(widget1) is tk.Label:
# do something
如您所見,我正在三次撰寫 type(widget1) 。相反,有沒有更簡單的方法來做到這一點?
if type(widget1) is tk.OptionMenu or tk.Entry or tk.Label:
# do something
我知道上面的代碼在這里不起作用,但只是問,有沒有更簡單的方法呢?我正在多個地方進行此類比較,因此少寫會有所幫助。這不是一個真正的 tkinter 問題,任何人都可以回答!提前致謝!
uj5u.com熱心網友回復:
我認為您可以使用串列:
if type(widget1) in [tk.OptionMenu, tk.Entry, tk.Label]:
#Do something
uj5u.com熱心網友回復:
isinstance除了其典型用法外,您還可以使用which 接受一個元組作為其第二個引數:
isinstance(widget1, (tk.OptionMenu, tk.Entry, tk.Label))
請參閱檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497221.html
