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

标签 python if-statement tkinter conditional-statements

假设我有一个这个按钮:

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()

关于python - 如何使用小部件选项值作为条件来制作 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813984/

相关文章:

python - 如何删除(或取消网格)存储在列表中的 Tkinter GUI 对象 (Python)

python - 使用颜色条时 get_position() 会做奇怪的事情

python - 这是一种将 Avro 模式编译为 python 类的方法吗?

c - 使用 for 循环打印特定的 "non-vowel, vowel, non-vowel"单词

python - 值错误 : Dataset with data_id 554 not found

类类型的 C# 开关

jQuery if/else 语句失败

python - tkinter 按钮的高度和宽度

python - tkinter gui 编程中的 AttributeError

python - Tkinter 在单个窗口中显示多个随机选择的图像