[Python] i'm trying to print an image and a line on canvas but code does not work

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    im trying to build a canvas with a line and an image with the following code but it only shows a canvas

    ` from tkinter import * from PIL import Image, ImageTk

    # Create an instance of tkinter frame or window
    win=Tk()

    # Set the size of the tkinter window
    win.geometry("700x350")

    from numpy import asarray
    # load the image
    image = Image.open('image1.jpg')
    # convert image to numpy array
    data = asarray(image)
    print(type(data))
    # summarize shape
    print(data.shape)

    # create Pillow image
    image2 = Image.fromarray(data)
    print(type(image2))

    # summarize image details
    print(image2.mode)
    print(image2.size)

    class Start_Screen:
    def __init__(self, master):
    self.master = master
    # Create a canvas widget
    canvas=tkCanvas(win, width=500, height=300)


    # Add a line in canvas widget
    self.canvas.create_line(50,35,500,35, fill="green", width=5)
    self.start_img = Image.open("./image1.jpg")
    image1 = ImageTk.PhotoImage(image)

    # Add the image to the canvas
    self.canvas.create_image(50, 45, anchor='nw', image=image1)

    self.canvas.create_line(50,135,500,135, fill="green", width=5)

    self.canvas.create_line(50,235,500,235, fill="green", width=5)

    self.canvas.pack()

    self.canvas.pack(expand=YES, fill=BOTH)
    # myvar = canvas.create_line(50,35,500,35, fill="green", width=5)
    #mytuple = (int myvar)
    #myit = iter(mytuple)


    win.mainloop()`


    i tried runnning this in spyder but it only shows canvas

    Continue reading...

Compartilhe esta Página