Pieter De Vleugel
Pieter De Vleugel

Reputation: 7

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

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

Upvotes: -1

Views: 37

Answers (1)

furas
furas

Reputation: 142985

I see mess in your code so it is hard to say what is real problem.

You have wrong indentations - some code is directly in class instead of __init__ - so it may run it in wrong way.

You never create instance of Start_Screen - so it can't run code which puts image on canvas - create_image(). And maybe this is your problem`

Inside class you create canvas but later you use self.canvas - this code should gives you error message.

You may have common problem with bug in PhotoImage - it removes image from memory when it assigned to local variable inside function - and later you see only empty canvas. You may have to use self.image1 instead of image1

You have to clean your code before you put it in question.


Minimal working code:

import tkinter as tk  # pep8: `import *` is not preferred
from PIL import Image, ImageTk
import numpy  # pep8: all imports at the beginning

# --- constants --- # pep8: constants before classes
                    # pep8: `UPPER_CASE_NAMES` for constants (with `_`)

# --- classes ---  # pep8: classes before functions and other code
                   # pep8: `CamelCaseNames` for classes (without `_`)

class StartScreen:
    def __init__(self, master):
        self.master = master

        # Create a canvas widget
        self.canvas = tk.Canvas(master, width=500, height=300)  # you can use `master` instead of `win`
        self.canvas.pack(expand=True, fill='both')
    
        # Add the image to the canvas
        self.start_img = Image.open("image1.jpg")
        self.image1 = ImageTk.PhotoImage(self.start_img)
        self.canvas.create_image(50, 45, anchor='nw', image=self.image1)
        
        # Add a line in canvas widget
        self.canvas.create_line(50,35,500,35, fill="green", width=5)
        self.canvas.create_line(50,135,500,135, fill="green", width=5)
        self.canvas.create_line(50,235,500,235, fill="green", width=5)
       
        
# --- functions ---  # pep8: functions after classes and before other code
                     # pep8: `lower_case_names` for functions (with `_`)

def not_used_code():
    """I don't use this code"""
    
    # load the image
    image = Image.open('image1.jpg')

    # convert image to numpy array
    data = numpy.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)

    # myvar = canvas.create_line(50,35,500,35, fill="green", width=5)
    #mytuple = (int myvar)
    #myit = iter(mytuple)
        
# --- main ---        

win = tk.Tk()

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

screen = StartScreen(win)
screen.canvas.create_line(50, 335, 500, 335, fill="red", width=5)  # access canvas (created in StartScreen) outside class

win.mainloop()

Result:

enter image description here


Image source: Wikipedia: Lenna

PEP 8 – Style Guide for Python Code

Upvotes: 0

Related Questions