简体   繁体   中英

Image/Label not showing with PIL/Python 3.6

I am trying to insert an image into a label widget. If I use the show() function it successfully loads my image. For some reason the image won't display in the GUI though. I've been trying to get it to show for the entire background of the app, but for now it would be acceptable just to show in one frame while I figure out how to get it to the rest of the GUI. The following is a screen shot of the output and code

https://i.imgur.com/6Z2qv8S.png

import sys
from tkinter import *
from PIL import Image,ImageTk

class Game_Client(object):

def __init__(self):
    self.tk = Tk()
    self.tk.title("Untitled Game")
    self.tk.wm_state('zoomed')

    # Create the frame holders
    top_left = self.create_topleft()
    top_mid = self.create_topmid()
    top_right = self.create_topright()
    btm_left = self.create_btmleft()
    btm_mid = self.create_btmmid()
    btm_right = self.create_btmright()
    border_left = self.create_borderL()
    border_right = self.create_borderR()
    border_top = self.create_borderT()
    border_bottom = self.create_borderB()

    self.tk.grid_rowconfigure(0, weight=1)
    self.tk.grid_rowconfigure(1, weight=4)
    self.tk.grid_rowconfigure(2, weight=4)
    self.tk.grid_rowconfigure(3, weight=1)
    self.tk.grid_columnconfigure(0, weight=1)
    self.tk.grid_columnconfigure(1, weight=4)
    self.tk.grid_columnconfigure(2, weight=4)
    self.tk.grid_columnconfigure(3, weight=4)
    self.tk.grid_columnconfigure(4, weight=1)

    background_image = Image.open('client-bg.jpg')
    #background_image.show()
    background_image = ImageTk.PhotoImage(background_image)
    background_label = Label(top_mid, image=background_image)
    #background_label.grid(row=0, column=0, rowspan=4, columnspan=5)
    background_label.place(x=0,y=0,relheight=1, relwidth=1)
    background_label.lift(aboveThis=None)

    # Position the frames and set rescale weight
    top_left.grid(row=0, column=0, sticky="nsew")
    top_mid.grid(row=0, column=1, sticky="nsew")
    top_right.grid(row=0, column=2, sticky="nsew")
    btm_left.grid(row=1, column=0, sticky="nsew")
    btm_mid.grid(row=1, column=1, sticky="nsew")
    btm_right.grid(row=1, column=2, sticky="nsew")
    border_left.grid(row = 0, column = 0, rowspan = 4, sticky="ns")
    border_right.grid(row=0, column=4, rowspan=4, sticky="ns")
    border_top.grid(row=0, column=0, columnspan=5, sticky="ew")
    border_bottom.grid(row=4, column=0, columnspan=5, sticky="ew")

    txtOutFrame = Frame(btm_mid, bg="purple", height=625, width=560)
    txtOutFrame.pack(side=TOP, fill=BOTH)
    txtOutFrame.pack_propagate(False)
    txtOut = Text(txtOutFrame)
    txtOut.config(bg='black', fg='green', relief="groove")
    txtOut.pack(expand=True, fill=BOTH)

    txtChatFrame = Frame(btm_mid, bg="pink", height=150, width=560)
    txtChatFrame.pack(side=BOTTOM, fill=X)
    txtChatFrame.pack_propagate(False)
    txtChat = Text(txtChatFrame)
    txtChat.config(bg='white', fg = 'black', relief="groove")
    txtChat.pack(expand=True, fill=X)

    self.state = False
    self.tk.bind("<F11>", self.toggle_fullscreen)
    self.tk.bind("<Escape>", self.end_fullscreen)

def toggle_fullscreen(self, event=None):
    self.state = not self.state  # Just toggling the boolean
    self.tk.attributes("-fullscreen", self.state)
    return "break"

def end_fullscreen(self, event=None):
    self.state = False
    self.tk.attributes("-fullscreen", False)
    return "break"

def create_topleft(layout):
    f = Frame(layout.tk, bg='red', width=430, height=295)
    return f

def create_topmid(layout):
    f = Frame(layout.tk, bg='orange', width=1050, height=295)
    return f

def create_topright(layout):
    f = Frame(layout.tk, bg='purple', width=430, height=295)
    return f

def create_btmleft(layout):
    f = Frame(layout.tk, bg='blue', width=430, height=775)
    return f

def create_btmmid(layout):
    f = Frame(layout.tk, bg='yellow', width=1050, height=775)
    return f

def create_btmright(layout):
    f = Frame(layout.tk, bg='green', width=430, height=775)
    return f

def create_borderL(layout):
    f = Frame(layout.tk, bg='black', width=5, height=1080)
    return f

def create_borderR(layout):
    f = Frame(layout.tk, bg='black', width=5, height=1080)
    return f

def create_borderT(layout):
    f = Frame(layout.tk, bg='black', width=1920, height=5)
    return f

def create_borderB(layout):
    f = Frame(layout.tk, bg='black', width=1920, height=5)
    return f

if __name__ == '__main__':
    w = Game_Client()
    w.tk.mainloop()

This is a very common problem. You need to keep a reference to the PhotoImage object.

background_image = Image.open('client-bg.jpg')
background_image = ImageTk.PhotoImage(background_image)
background_label = Label(top_mid, image=background_image)
background_label.image_ref = background_image # keep the reference!

Otherwise the image gets garbage collected.

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