Skip to main content
 首页 » 编程设计

python之如何使用小部件选项值作为条件来制作 if 语句

2025年05月04日31luoye11

假设我有一个这个按钮:

tl.config(bd=0 ,image=photo1 ,width="100",height="100",command=lambda: functionPlus(tl))   

函数是:

def functionPlus(button): 
 
   global turn 
 
   if (turn==1 or turn==3 or turn==5 or turn==7 or turn==9): 
       button.config(image=photo2,width="100",height="100") 
       turn +=1 
 
   elif (turn==2 or turn==4 or turn==6 or turn==8) : 
       button.config(image=photo3,width="100",height="100") 
       turn+=1 

我想在函数中添加一个“if”,它将按钮的图像作为条件。例如:

if button.config(image=photo2 == True) : 
   anotherFunction() 

提前致谢。

请您参考如下方法:

首先,永远不要使用表达式模式something=something else == True!

其次,看看this related (but not duplicate) question .

如您所见,cget方法将返回选项的当前值。作为this manual page mentions , cget 类似于 widget["option"]。


因此,要直接回答您的问题,您需要的 if 条件应该是:

if button['image']==photo2: 
   anotherFunction()