简体   繁体   中英

Tkinter not showing image

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM