我正在尝试将图像插入标签小部件中。如果我使用show()函数,它会成功加载我的图像。由于某种原因,图像不会显示在GUI中。我一直试图让它显示应用程序的整个背景,但是现在只需要在一个框架中显示,同时我弄清楚如何将它移植到GUI的其余部分是可以接受的。以下是输出和代码的屏幕截图
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()
答案 0 :(得分:2)
这是一个非常常见的问题。您需要保留对PhotoImage对象的引用。
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!
否则图像会被垃圾收集。