這個問題在這里已經有了答案: 如果在函式中創建,為什么 Tkinter 影像不顯示? (4 個回答) 20 小時前關閉。
我撰寫了這個小 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 函式應該用給定的影像放置一個標簽,但實際情況是,該函式只是放置一個沒有影像的標簽 - 只是一個框。有人可以告訴我如何解決這個問題以及為什么會這樣嗎?謝謝!
uj5u.com熱心網友回復:
試試這個它會作業
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)
我不知道,但這是一個錯誤或其他任何東西。我也遇到了同樣的問題。
要解決此問題,您只需將此影像保存在標簽類中。
addsmth.imageaddsmth.img=photo_png_placesmth您將其更改為oraddsmth.aaa=photo_png_placesmth或之類的任何東西addsmth.bbb=photo_png_placesmth,它將在所有情況下都有效。
您必須addsmth.image在代碼中添加它會阻止垃圾收集器中的影像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/504267.html
上一篇:Tkinter-如何在標簽中列印串列值,每個值在不同的行上,最新的值在前
下一篇:如何驗證條目小部件?Python
