【问题标题】:Tkinter image label not appearingTkinter 图像标签未出现
【发布时间】:2018-04-25 11:39:15
【问题描述】:

我不知道为什么这不起作用。我在 Mac 上使用 python 3。我努力了 label.image 但它不起作用。这是我的代码:

File=open("/Users/user/Desktop/Python/File.txt")
FileContents=File.readlines()
ThingAmount=Team[0]
ActiveThing=Team[1]
ActiveThingImage=PhotoImage("/Users/user/Desktop/Python/"+ActiveThing+"/"+ActiveThing+".gif")
EnemyThingImage=PhotoImage("/Users/user/Desktop/Python/Enemy/EnemyThing.gif")
ActiveThingLabel=Label(window,image=ActiveThingImage)
ActiveThingLabel.image=ActiveThingImage
ActiveThingLabel.place(x=0,y=0)
EnemyThingLabel=Label(window,image=EnemyThingImage)
EnemyThingLabel.place(x=100,y=100)

【问题讨论】:

  • 请创建一个minimal reproducible example。一小块漂浮在空中的代码是不够的。
  • 您在此处发布的代码是否与您的本地代码相同?看起来您在 ActiveThingImage= 行中缺少 " 引号
  • @BryanOakley 这是我能得到的最完整和可验证的内容
  • 它的完整之处是什么?如果我运行它,我会收到多个错误。例如,你没有任何导入,你有不平衡的引号,你没有调用mainloop,你没有创建一个根窗口,变量windowTeam,和`Active_Pokemon_Sprint"是未定义,仅列出最明显的问题。
  • @BryanOakley 首先,很明显我做了导入和窗口,这是标准的,其次,我搞砸了一些变量,我的错,只是告诉我而不是链接无用的文章。我现在记得为什么我首先停止使用 stackoverflow。

标签: python python-3.x tkinter


【解决方案1】:

图片可能有点棘手 - 但我想我可以提供 2 个答案!

  1. 创建图像

  2. 如何从某个文件夹中加载一堆图片。

1:

让按钮拥有图像可能有点棘手(您需要一种 2 层方法),但更改图像很容易。

x_image = 'x.png'

o_image = 'o.png'

那么……

x_image_for_button = PhotoImage(file=x_image)
o_image_for_button = PhotoImage(file=o_image)

然后.....

button = tk.Button(self.controller, image=o_image_for_button, command=lambda: command_or_something)
button.config(width="40", height="40")
button.place(x=5, y=5)

(添加self.或root.等)

现在你要做的就是改变图像:

button.set(image=o_image_for_button)
#on second thought... maybe use `button.config(image=o_image_for_button)` insted

2:

image_folder = 'theme_icons'
if self.controller.background_theme == '1':
    image_pathway = 'classic'

elif self.controller.background_theme == '2':
    image_pathway = 'blue1'

elif self.controller.background_theme == '3':
    image_pathway = 'blue2'

elif self.controller.background_theme == '4':
    image_pathway = 'classic_blue'


self.house_image_d = os.path.join(image_folder, image_pathway, 'Image1.png')
self.line_d = os.path.join(image_folder, image_pathway, 'Image2.png')


self.house_image = tk.PhotoImage(file=self.house_image_d)
self.my_line = tk.PhotoImage(file=self.line_d)

【讨论】:

    猜你喜欢
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多