OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Tkinter Image Shows up Like a Grey Box (Python) [duplicate]

  • Thread starter Thread starter Eshaan Agarwal
  • Start date Start date
E

Eshaan Agarwal

Guest
I'm trying to create an app for work, but I'm new to Tkinter. I want the company's logo to show up in the top left corner. However, all I can get to show up is a blank grey box. It is worth noting that this is an image with a transparent background, however, I was able to get that to show up before when my code was not organized into classes. The image size should also not be a problem. I resized it specifically to fit in the frame that I am using. Blank grey box

I've tried this several different ways: using a Canvas widget, and using a Label widget. However, neither of these work. When I use the Canvas widget, all that shows up is a blank grey box. I get nothing at all out of the label widget.

My code is below. The exact same code was working when the program wasn't organized into classes. But, I decided that an incomprehensible stream of code isn't the best when building out an app.

I only pasted the main class and the logo class here. There are others, but they are not relevant to what I am currently doing.

Code:
from tkinter import *
from tkinter import ttk
from PIL import ImageTk,Image

class MainApp(Tk):
    def __init__(self, title, size):
        super().__init__()

        #Main Setup
        self.title(title)
        self.geometry(f"{size[0]}x{size[1]}")
        self.minsize(size[0],size[1])

        #Main Frames
        self.logo = Logo(self)
        self.left = Left(self)
        self.right = Right(self)
        self.content = Content(self)

        self.columnconfigure(1, weight=6)
        self.columnconfigure(0, weight=0)
        self.columnconfigure(2, weight=0)
        self.rowconfigure(1, weight=6) 
        self.rowconfigure(0, weight=0)

        self.mainloop()

class Logo(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        
        self = ttk.Frame(parent, borderwidth=5, relief="ridge", width = 1000, height = 50)

        unipart = Canvas(self, width = 171, height = 50)
        self.grid(row=0,column=0, columnspan=3, rowspan=1, sticky=("N", "S", 'E', 'W'))

        image = Image.open("Py GUI\\unipart-logo.png")
        unipart = ImageTk.PhotoImage(image)

        self.label=Label()
        self.label['image'] = unipart
        self.label.grid(column=0, row=0, columnspan=1, rowspan=1, sticky=("S"))

        # unipart = ttk.Label(self)
        # img = ImageTk.PhotoImage(file='Py GUI\\unipart-logo.png')
        # unipart['image'] = img
        # unipart.grid(column=0, row=0, columnspan=1, rowspan=1)
<p>I'm trying to create an app for work, but I'm new to Tkinter. I want the company's logo to show up in the top left corner. However, all I can get to show up is a blank grey box. It is worth noting that this is an image with a transparent background, however, I was able to get that to show up before when my code was not organized into classes. The image size should also not be a problem. I resized it specifically to fit in the frame that I am using.
<a href="https://i.sstatic.net/tCvaE3iy.png" rel="nofollow noreferrer">Blank grey box</a></p>
<p>I've tried this several different ways: using a Canvas widget, and using a Label widget. However, neither of these work. When I use the Canvas widget, all that shows up is a blank grey box. I get nothing at all out of the label widget.</p>
<p>My code is below. The exact same code was working when the program wasn't organized into classes. But, I decided that an incomprehensible stream of code isn't the best when building out an app.</p>
<p>I only pasted the main class and the logo class here. There are others, but they are not relevant to what I am currently doing.</p>
<pre><code>from tkinter import *
from tkinter import ttk
from PIL import ImageTk,Image

class MainApp(Tk):
def __init__(self, title, size):
super().__init__()

#Main Setup
self.title(title)
self.geometry(f"{size[0]}x{size[1]}")
self.minsize(size[0],size[1])

#Main Frames
self.logo = Logo(self)
self.left = Left(self)
self.right = Right(self)
self.content = Content(self)

self.columnconfigure(1, weight=6)
self.columnconfigure(0, weight=0)
self.columnconfigure(2, weight=0)
self.rowconfigure(1, weight=6)
self.rowconfigure(0, weight=0)

self.mainloop()

class Logo(ttk.Frame):
def __init__(self, parent):
super().__init__(parent)

self = ttk.Frame(parent, borderwidth=5, relief="ridge", width = 1000, height = 50)

unipart = Canvas(self, width = 171, height = 50)
self.grid(row=0,column=0, columnspan=3, rowspan=1, sticky=("N", "S", 'E', 'W'))

image = Image.open("Py GUI\\unipart-logo.png")
unipart = ImageTk.PhotoImage(image)

self.label=Label()
self.label['image'] = unipart
self.label.grid(column=0, row=0, columnspan=1, rowspan=1, sticky=("S"))

# unipart = ttk.Label(self)
# img = ImageTk.PhotoImage(file='Py GUI\\unipart-logo.png')
# unipart['image'] = img
# unipart.grid(column=0, row=0, columnspan=1, rowspan=1)
</code></pre>
 
Top