Tkinter图像标签未出现

2024-09-28 21:56:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道为什么这样不行。我正在mac上使用Python3。我试过了 标签.图像但它不起作用。这是我的密码:

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)

Tags: imageplacewindowgifuserslabelteamfile
1条回答
网友
1楼 · 发布于 2024-09-28 21:56:38

图像可能会有点棘手-但我想我可以帮助两个答案!你知道吗

  1. creates the image

  2. how to load a bunch of images from a folder somewhere.

一:

让按钮有一个图像可能有点棘手(你需要一个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。或者根。等)

现在,要更改图像,只需执行以下操作:

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

二:

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)

相关问题 更多 >