Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this python tkinter function not using the given image?

I coded this small tkinter program but if you select something and place it, it does not use the given picture.
This is the code:

# 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()

This is the function which should place an Label with the given image:

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)

The placesmth function should place a label with the given image but what happens is, that the function just places an label without an image – just a box. Can someone tell me how I can fix this problem and why this happens? Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this it will work

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)

I don’t know but it’s a bug or anything else. I also got in the same problem .

To solve this problem you just have to save this image inside the label class.

addsmth.image you change it to any thing like addsmth.img=photo_png_placesmth or addsmth.aaa=photo_png_placesmth or addsmth.bbb=photo_png_placesmth, it will work in all cases.

You have to add addsmth.image in the code it will prevent the image from the garbage collector.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading