为什么这个python tkinter函数没有使用给定的图像



我对这个小tkinter程序进行了编码,但如果你选择并放置了一些东西,它就不会使用给定的图片。这是代码:

# Imports
import tkinter as tk
from PIL import ImageTk, Image
# Vars
mainbgc = "#2c2c2c"
label_mainleftc = "#3c3c3c"
label_maintopc = "#4c4c4c"
label_leftsel = "#5c5c5c"
# Tk
root = tk.Tk()
root.title("Zilius")
root.iconbitmap("zilius128.ico")
screen_width = round(root.winfo_screenwidth() / 2)
screen_height = round(root.winfo_screenheight() / 2)
root.geometry(f"{screen_width}x{screen_height}")
root.configure(bg=mainbgc)

cable = False
computer = False
switch = False
count = 0
def placesmth(x, y, img):
image_png_placesmth = Image.open(img).resize((60, 60))
photo_png_placesmth = ImageTk.PhotoImage(image=image_png_placesmth)
addsmth = tk.Label(root, image=photo_png_placesmth)
addsmth.place(x=x, y=y)
def getorigin(eventorigin):
global x, y
x = eventorigin.x
y = eventorigin.y
if x >= 75 and y >= 45:
if computer == True:
placesmth(x, y, "png_leftcomputer.png")
if cable == True:
print(f"Cable {x, y}")
if switch == True:
placesmth(x, y, "png_leftswitch.png")
root.bind("<Button 1>", getorigin)
def button_run():
print("Runned")
def changesel_cable():
global computer
global switch
button_png_leftcable.configure(bg=label_leftsel)
button_png_leftswitch.configure(bg=label_mainleftc)
button_png_leftcomputer.configure(bg=label_mainleftc)
computer = False
switch = False
def changesel_computer():
global cable
global switch
button_png_leftcomputer.configure(bg=label_leftsel)
button_png_leftswitch.configure(bg=label_mainleftc)
button_png_leftcable.configure(bg=label_mainleftc)
switch = False
cable = False
def changesel_switch():
global cable
global computer
button_png_leftswitch.configure(bg=label_leftsel)
button_png_leftcomputer.configure(bg=label_mainleftc)
button_png_leftcable.configure(bg=label_mainleftc)
computer = False
cable = False
def button_leftcable():
global cable
cable = True
changesel_cable()
def button_leftcomputer():
global computer
computer = True
changesel_computer()
def button_leftswitch():
global switch
switch = True
changesel_switch()

label_mainleft = tk.Label(bg=label_mainleftc)
label_mainleft.place(x=0, y=0, height=int(round(root.winfo_screenheight())), width=75)
label_maintop = tk.Label(bg=label_maintopc)
label_maintop.place(x=0, y=0, width=int(round(root.winfo_screenwidth())), height=40)
image_png_runbutton = Image.open("png_runbutton.png").resize((30, 30))
photo_png_runbutton = ImageTk.PhotoImage(image=image_png_runbutton)
button_png_runbutton = tk.Button(root, image=photo_png_runbutton, bg=label_maintopc, activebackground=label_maintopc, borderwidth=-1, command=button_run)
button_png_runbutton.place(x=50,y=4)
image_png_leftcable = Image.open("png_leftcable.png").resize((60, 60))
photo_png_leftcable = ImageTk.PhotoImage(image=image_png_leftcable)
button_png_leftcable = tk.Button(root, image=photo_png_leftcable, bg=label_mainleftc, activebackground=label_mainleftc, borderwidth=-1, command=button_leftcable)
button_png_leftcable.place(x=5,y=40)
image_png_leftcomputer = Image.open("png_leftcomputer.png").resize((60, 60))
photo_png_leftcomputer = ImageTk.PhotoImage(image=image_png_leftcomputer)
button_png_leftcomputer = tk.Button(root, image=photo_png_leftcomputer, bg=label_mainleftc, activebackground=label_mainleftc, borderwidth=-1, command=button_leftcomputer)
button_png_leftcomputer.place(x=5,y=105)
image_png_leftswitch = Image.open("png_leftswitch.png").resize((60, 60))
photo_png_leftswitch = ImageTk.PhotoImage(image=image_png_leftswitch)
button_png_leftswitch = tk.Button(root, image=photo_png_leftswitch, bg=label_mainleftc, activebackground=label_mainleftc, borderwidth=-1, command=button_leftswitch)
button_png_leftswitch.place(x=5,y=170)
root.mainloop()

这是一个功能,应该放置一个标签与给定的图像:

def placesmth(x, y, img):
image_png_placesmth = Image.open(img).resize((60, 60))
photo_png_placesmth = ImageTk.PhotoImage(image=image_png_placesmth)
addsmth = tk.Label(root, image=photo_png_placesmth)
addsmth.place(x=x, y=y)

placesmth函数应该用给定的图像放置一个标签,但实际情况是,该函数只放置了一个没有图像的标签——只是一个框。有人能告诉我如何解决这个问题吗?为什么会发生这种情况?非常感谢。

试试这个,它会工作

def placesmth(x, y, img):
image_png_placesmth = Image.open(img).resize((60, 60))
photo_png_placesmth = ImageTk.PhotoImage(image=image_png_placesmth)
addsmth = tk.Label(root, image=photo_png_placesmth)
addsmth.image = photo_png_placesmth
addsmth.place(x=x, y=y)

我不知道,但这是一个bug或其他什么。我也遇到了同样的问题。

要解决这个问题,您只需要将此图像保存在标签类中。

addsmth.image如果将其更改为任何类似addsmth.img=photo_png_placesmthaddsmth.aaa=photo_png_placesmthaddsmth.bbb=photo_png_placesmth的东西,它在任何情况下都会起作用。

您必须在代码中添加addsmth.image,这将防止图像来自垃圾收集器。

最新更新