0

I am trying to make it so my logo is at the top of the page. My image SSM contains the logo but when I run the program it returns a empty tab with no text nor image on it and it also isn't the correct pixel size. How do I make it so the image displays correctly?

import tkinter as tk
from PIL import Image, ImageTk
import os

def main_root(root):
    root.geometry('600x400')
    root.title('Skies Login System')

    image = Image.open('SSM.png')
    resized_image = image.resize(('600x400'), Image.ANTIALIAS)
    conv_image = ImageTk.PhotoImage(resized_image)

    label = tk.label(root, image= conv_image, width = '300', height = '150', bg = 'white', fg = 'white')
    label.pack()

    root.title('Skies Login System')
    Label(text ='Skies Stock Manager', bg = 'grey', width = '300', height = '2', font = ('Calibri', 13, 'bold')).pack()
    Label(text = '').pack()
    Button(text = 'Login').pack()
    Label(text = '').pack()
    Button(text = 'Register').pack()

    root.mainloop()

if __name__ == '__main__':
    root = tk.Tk()
    root.mainloop()
  • 3
    Most of your code is in a function that is never called, and is therefore irrelevant. Once you fix that, you'll face the problem described in https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function – jasonharper Dec 19 '21 at 02:16
  • I am not sure, but can you try converting the .png to .ico file and then opening the SSM.ico image file? – Swagrim Dec 19 '21 at 06:31
  • why are you creating empty `Label`s? if you want to add some spacing in between, then know that `pack` has `padx` and `pady` arguments that do this, you don't need to create empty `Label`s – Matiiss Dec 19 '21 at 09:12
  • just use `main_root(root)` after `root=tk.Tk()`.There you go:) – Faraaz Kurawle Dec 19 '21 at 16:08

1 Answers1

0

Some issues in your code, like

  • Function main_root not called, how come the image or labels.
  • 'tk' missed when call Label and Button.

Update code

import tkinter as tk
from PIL import Image, ImageTk
import os

def main_root(root):

    root.geometry('600x400')
    root.title('Skies Login System')

    image = Image.open('SSM.png')
    resized_image = image.resize((600, 400), Image.ANTIALIAS)
    conv_image = ImageTk.PhotoImage(resized_image)

    label = tk.Label(root, image=conv_image, width=300, height=150, bg='white', fg='white')
    label.pack()

    tk.Label(text='Skies Stock Manager', bg='grey', width=300, height=2, font = ('Calibri', 13, 'bold')).pack()
    tk.Label(text='').pack()
    tk.Button(text='Login').pack()
    tk.Label(text='').pack()
    tk.Button(text='Register').pack()

    root.mainloop()

if __name__ == '__main__':
    root = tk.Tk()
    main_root(root)
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • there is no need to use empty `Label`s, `pack` has a `pady` argument for this, also it seems that `global conv_image` can be removed since the function doesn't complete until the window is closed so the image doesn't get garbage collected – Matiiss Dec 19 '21 at 13:51
  • Empty lables not questioned here, `pady` may get different result if code updated by new font size. I thought I removed the global statement after I moved `mainloop` statement into function `main_root`. – Jason Yang Dec 19 '21 at 13:59
  • `pady` actually is the one that wouldn't give different results based on the font size, the padding will always be the same specified amount, as to whether it was asked about: if it is something that can easily be improved why not improve it and show the OP that there is a better (intended) way of doing what they are doing – Matiiss Dec 19 '21 at 14:04
  • How to set pady for one-line spacing ? IMO, 'better' means nothing, there maybe always with something better than 'better', then no stop for next question here ... For example, rewrite all the code to better one ? IMO, it's not necessary. If you feel this reply not good, you can reply one for it. – Jason Yang Dec 19 '21 at 14:29
  • I understand that of course one can always improve something but if it is a small change then why not do it? your answer otherwise is good and it does answer the question, its that it IMO doesn't make much sense as to why to do some improvised padding with other widgets if layout managers already provide that functionality and in a better manner in the sense that that is the intended way to add padding and the GUI will have only the widgets needed. It doesn't take much effort to remove those labels and add some arguments to `pack` so this all can easily be applied at least as a suggestion – Matiiss Dec 19 '21 at 15:09
  • For me, using `pady` is not a good selection for one-line spacing. – Jason Yang Dec 19 '21 at 15:15