Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter button.config()if语句_Python_Tkinter - Fatal编程技术网

Python Tkinter button.config()if语句

Python Tkinter button.config()if语句,python,tkinter,Python,Tkinter,我有一些包含图像的按钮,单击这些按钮将更改按钮中的图像。 如果图像为空星形,则单击后按钮将更新为全星形,再次单击后,此选项将恢复为按钮中的空星形图像。 我试过: firststar=partial(starsystemfeed,1) star_1=按钮(帧馈送,图像=emptystar,component=“right”,命令=firststar) emptystar=PhotoImage(文件='icons/emptystar.png') fullstar=PhotoImage(文件='ico

我有一些包含图像的按钮,单击这些按钮将更改按钮中的图像。
如果图像为空星形,则单击后按钮将更新为全星形,再次单击后,此选项将恢复为按钮中的空星形图像。
我试过:

firststar=partial(starsystemfeed,1)
star_1=按钮(帧馈送,图像=emptystar,component=“right”,命令=firststar)
emptystar=PhotoImage(文件='icons/emptystar.png')
fullstar=PhotoImage(文件='icons/fullstar.png')
def starsystemfeed(数量):
全球之星1、2、3、4、5
全球全明星
全球空星
如果num==1:
如果star_1.config(image=emptystar)为True:
打印(“1”)
star_1.config(image=fullstar)
其他:
打印(“2”)
star_1.config(image=emptystar)
但这似乎不起作用,因为我一直在
emptystar
图像上看到选项“2”,这表明我做错了尝试以下方法:

firststar=partial(starsystemfeed,1)
star_1=按钮(帧馈送,图像=emptystar,component=“right”,命令=firststar)
emptystar=PhotoImage(文件='icons/emptystar.png')
fullstar=PhotoImage(文件='icons/fullstar.png')
def starsystemfeed(数量):
全球之星1、2、3、4、5
全球全明星
全球空星
如果num==1:
如果str(star_1.cget(“image”)==str(emptystar):
打印(“1”)
star_1.config(image=fullstar)
其他:
打印(“2”)
star_1.config(image=emptystar)

您可以使用
.cget(“”)
从小部件获取属性。在本例中,我们希望返回图像,因此我们使用
star\u 1.cget(“图像”)
,并检查这是否与
emptystar

相同。请尝试
if star\u 1.cget(“图像”):…
这是一个通过PhotoImage导入的图像,因此我得到了\u tkinter.TclError:未知选项“-emptystar”您是否可以在不创建新变量的情况下执行此操作,因为这意味着需要再创建4个变量代码会有点复杂。@Bond007唯一的事情(除了使用变量)是
star\u 1.cget(“image”)
,但是你说它不工作。我不知道它为什么会引起这个错误。结果是我做了if star_1.cget(“emptystar”):让我得到错误,但现在它已修复,它不会恢复,因为我使用了else语句,但我如何指定它应该查找的图像?@Bond007现在请尝试我答案中的代码。我认为它现在应该可以解决同一个问题,不会改变,并停留在选项“2”。试着像和elif一样做,但这次没有通过声明。