python - Tkinter 不会将图像文件更新为新目录中的一个
问题描述
我正在尝试创建一个顶部带有图像图标的按钮,该按钮应将图标切换为在单击按钮时调用的函数中指定的图像。但是,该按钮在没有错误消息的情况下与原始图像保持原样,我错过了什么吗?
from tkinter import *
sampleW = Tk()
sampleW.geometry("250x250")
sampleW.title("god help me")
def imageSwitch():
icon1Directory == PhotoImage(file = r"C:\Users\txvpa\OneDrive\Desktop\hentai\Atom Projects\The Bread Exchange\bread man.png") # new image directory
print("button has been pressed")
icon1Directory = PhotoImage(file = r"C:\Users\txvpa\OneDrive\Desktop\hentai\Atom Projects\The Bread Exchange\plus_black.png") # original image directory
icon1_photoImage = icon1Directory.subsample(7, 7)
button = Button(sampleW, relief = "ridge", padx = 70, pady = 5,image = icon1_photoImage, command = imageSwitch)
button.grid(column = 0, row = 0)
sampleW.mainloop()
解决方案
我认为你应该在函数中更改这一行:
icon1Directory == PhotoImage(file = r"C:\Users\txvpa\OneDrive\Desktop\hentai\Atom Projects\The Bread Exchange\bread man.png")
你写了==
,但你应该写=
。
您的语法意味着您正在使用 False return 而不是变量减速进行语句。
推荐阅读
- javascript - 如何确保结束日期总是比开始日期多 7 天?
- javascript - How to change a message in a boolean variable in node.js
- php - 我想显示数组
- c# - 有没有办法从访问器获取 PropertyInfo?
- ios - IOS版本12.2和12.3 UlWebView点击视频播放不起作用
- google-cloud-platform - 提高 AWS S3 文件到 Google Cloud Storage 的传输速度
- javascript - 函数包含在 javascript 中
- drupal-8 - 如何将参数对象变量从 behat.yml 传递给 FeatureContext.php 构造函数?
- awk - 比较两个表并显示第二个表中的选定值
- google-cloud-dataflow - 根据时间或结束标记聚合流中的记录