Home > Blockchain >  how to pack into frame class python tkinter
how to pack into frame class python tkinter

Time:10-22

Here I pack a Label into a frame class but it doesn't pack inside the frame it packs in the other side the window class. Here is a screenshot of the results in a screenshot. Code:

from PIL import Image,ImageTk
import tkinter as tk

class Window(tk.Tk):
    def __init__(self):
        super().__init__()
    def AddImage(path, size):
        rawimage = Image.open(path)
        rawimage = rawimage.resize(size, Image.Resampling.LANCZOS)
        cleanimage = ImageTk.PhotoImage(rawimage)
        print("add image run")
        return cleanimage


class LeftFrame(tk.Frame):
    def __init__(self, container):
        super().__init__(container,bg="red",width=400,height=600)
        self.pack(side=tk.LEFT)
        self.widgets()

    def widgets(self):
        self.test = Window.AddImage(r"assets\images\add-word.png",size=(20,20))
        self.testLabel = tk.Label(image = self.test)
        self.testLabel.pack()


        
        
    
        

if __name__ == "__main__":
    win = Window()
    frame = LeftFrame(win)
    win.geometry("800x600")
    win.resizable(0, 0)
    win.configure(bg="#58e0dc")
    win.mainloop()

CodePudding user response:

Just add self as the first argument of your testLabel. This sets the parent for the label so it knows where to pack itself.

   def widgets(self):
        self.test = Window.AddImage(r"assets\images\add-word.png",size=(20,20))
        self.testLabel = tk.Label(self, image = self.test)
        self.pack_propagate(0)
        self.testLabel.pack()
  • Related