0

I'm making a program with Tkinter, I decided to use image buttons for easier to interact but PhotoImage by somehow not worked. The code example of the script contain image button:

from tkinter import *

class test:
    def __init__(self,master):
        self.master = master
        img = PhotoImage(r'E:\v1.1\import.png')
        b1 = Button(self.master, image = img).pack()

The script that contain source code that will load the above script:

from test2 import *
maingui = Tk() 
gui = test(maingui) 
maingui.mainloop()

By somehow the image is not loaded, leaving very small button. Anyone know what have I wrong?

EDIT 1: I have tried this solution (mentioned by acw1668) and edit the code of the first script but its not worked

from tkinter import *

class test:
    def __init__(self,master):
        global img
        self.master = master
        img = PhotoImage('E:\\v1.1\\import.png')
        imglabel = Label(image=img)
        imglabel.image = img
        imglabel.pack()

        b1 = Button(self.master, image = img).pack()

The results is the same as you can see on the link [1]: https://i.stack.imgur.com/Mo1XH.png

EDIT 2: I have tried to add self. into img and imglabel but its not worked too.

from tkinter import *

class test:
    def __init__(self,master):
    # I cannot global self.img so I have to delete it
        self.master = master
        self.img = PhotoImage('E:\\v1.1\\import.png')
        self.imglabel = Label(image=img)
        self.imglabel.image = self.img
        self.imglabel.pack()

        self.b1 = Button(self.master, image = self.img).pack()
Rotomegax
  • 25
  • 5
  • 1
    Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – acw1668 Jul 30 '21 at 09:34
  • @acw1668 Its not worked, I have tried all ways on that post and none of them worked. – Rotomegax Jul 30 '21 at 12:55
  • Please show what you've tried from that other answer, or we'll have to close this as a duplicate. – Bryan Oakley Jul 30 '21 at 14:21
  • @BryanOakley I will post a new code I recreate from the mentioned post when I'm at home immediately – Rotomegax Jul 31 '21 at 02:14
  • @BryanOakley I have added the new code on EDIT1 – Rotomegax Jul 31 '21 at 02:20
  • You aren't saving a reference to `imglabel`. – Bryan Oakley Jul 31 '21 at 03:09
  • @BryanOakley is close. The problem is that you need to save a reference to `img`. Tk doesn't do that automatically. Just change `img` to `self.img`. – Tim Roberts Jul 31 '21 at 03:13
  • @TimRoberts: they are already attempting to save a reference to `img`. However, they are saving it by attatching it to another object that is not being saved. – Bryan Oakley Jul 31 '21 at 03:19
  • Tkinter automatically has the parent widget (root in this case) take a reference to each child widget. So, `self.master` will hold the `imglabel` reference. But `imglabel` doesn't take a reference to the `PhotoImage`. There was another question about this exact behavior earlier today. – Tim Roberts Jul 31 '21 at 03:26
  • @TimRoberts Its still not worked, I have updated EDIT 2 on my original post, can you come and check what step I was wrong? – Rotomegax Aug 01 '21 at 09:48

1 Answers1

0

Change PhotoImage from

PhotoImage('E:\\v1.1\\import.png')

to

PhotoImage( file = 'E:\\v1.1\\import.png')
Derek
  • 1,916
  • 2
  • 5
  • 15