Can't draw to canvas in Tkinter after() loop


discrete tomatoes

I'm having a problem, when I try to create an image on the canvas, I can't generate the image. However, I can create and configure my image before starting the after() loop. Also, I am able to use my canvas to delete objects canvas.delete()in my after()loop, so I still have some level of control.

I'm on windows 8.1 , using Python 3.5.4

import tkinter as tk
from PIL import Image, ImageTk
from math import floor
import numpy as np
from scipy import ndimage

root = tk.Tk()
HEIGHT = 600
WIDTH = 600
CANVAS_MID_X = WIDTH / 2
CANVAS_MID_Y = HEIGHT / 2

def my_mainloop():
    #THIS WORKS!!! REMOVES THE DIAL CREATED BEFORE THE MAINLOOP
    canvas.delete(dial_1)

    #THIS WORKS BELOW BEFORE MAINLOOP, BUT NOT IT WONT WORK! (use opencv?)
    img = dial_1_img_resized
    img2 = img.rotate(45, expand=True)
    dial_1_photo_new = ImageTk.PhotoImage(img2)
    dial_2 = canvas.create_image((dial_1_center), image=dial_1_photo_new, anchor=tk.E)
    '''CANT DRAW TO CANVAS IN AFTER LOOP'''
    print("loop!")
    root.after(4000,my_mainloop)

'''-------------------Create Canvas, and starting dials in their starting positions---------------------'''

canvas = tk.Canvas(root, width=HEIGHT, height=WIDTH, bg="black")
canvas.grid(row=0, column=0)
dial_1_path = "gauge1.png"
dial_1_width = 400

dial_1_img = Image.open(dial_1_path, 'r') #open image
dial_1_img_ratio = int(dial_1_img.size[1]) / int(dial_1_img.size[0])
dial_1_img_resized = dial_1_img.resize((dial_1_width, floor(dial_1_img_ratio * dial_1_width)), 1)
dial_1_photo = ImageTk.PhotoImage(dial_1_img_resized)
dial_1_center = (CANVAS_MID_X, CANVAS_MID_Y)

#CREATE DIAL ON CANVAS, THIS WORKS!!
dial_1 = canvas.create_image((dial_1_center), image=dial_1_photo)



'''Start Main Loop'''
root.after(0, my_mainloop)
root.mainloop()

Hence my question: is there a way to manipulate and create canvas images in a after()loop ? (called my_mainloop) Any help is appreciated!

busy bear

You need to save a reference to the photo as it will be garbage collected after it my_mainloopruns . For example, you can add this to your canvasobject:

canvas.dial_1_photo_new = ImageTk.PhotoImage(img2)
dial_2 = canvas.create_image((dial_1_center), image=canvas.dial_1_photo_new, anchor=tk.E)

Related


Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw in a loop

Saba Silagadze Why draw arent rectangles in a loop? I just started a snake game n trying to draw the snake's body through a loop, but for some reason ctx is not drawing the loop. If I write the rectangle without the loop, it works. var canv = document.getEleme

Canvas doesn't draw in a loop

Saba Silagadze Why draw arent rectangles in a loop? I just started a snake game n trying to draw the snake's body through a loop, but for some reason ctx doesn't draw the loop. If I write the rectangle without the loop, it works. var canv = document.getElement

Can't draw line on canvas

Bill Bulson I'm having trouble drawing lines on an HTML canvas with JavaScript. For the record, I don't want to use any pre-written line drawing methods, I need to do it using pixel manipulation. I try to draw a line on a canvas of 500x500 pixels, for which I

Can't draw squares on canvas

Lords Hi all, I'm starting a snake game with JavaScript. Now, what I'm trying to do is draw a green square in the center of the canvas. I set the fillStyle and used the fillRect method, but I get nothing. Can someone explain the problem, I'd really appreciate

Can't draw on canvas HTML

sky I'm trying to include a canvas element in a form where the user can draw a signature. The border and box are created, but when I try to draw on it, nothing happens. When I put the code that defines the canvas tag outside the form tag, the stroke seems to b

Can't draw squares on canvas

Lords Hi all, I'm starting a snake game with JavaScript. Now, what I'm trying to do is draw a green square in the center of the canvas. I set the fillStyle and used the fillRect method, but I get nothing. Can someone explain the problem, I'd really appreciate

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Tkinter canvas to draw windrose

Rodrigo Prodol I'm having some trouble drawing Windrose in a tkinter window. I can't get windrose to show itself, the window shows another grid behind the plot (see image below). Is there any way I can show only windrose without the extra grid in the backgroun

Android Canvas drawLine can't draw on MainActivity

Broadwell I want to draw a line on Main Activity using Canvas. The problem is, it doesn't draw anything. I have the following code: Bitmap bitmap = Bitmap.createBitmap(1920, 1080, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = ne

Can't draw square on canvas using setInterval?

User 10870573 Hey guys, I'm trying to use setInterval to draw a square on the canvas every 70ms. Interval is set to call function draw()this function, which contains the fillRect() method. But I can't get it to work any ideas? Oh, I tried clearRect() but it di

Canvas draw text - can't get string

Italy String str = new String(); str = "111"; x = width / 2; y = Math.round(height / 1.3); while (pressed != true){ if (!holder.getSurface().isValid()) continue; Canvas c = holder.lockCanvas(); c.drawBitmap(galaxy, 0, 0, null); c.draw

Can't draw on canvas with custom view

Mirko I have a class like the GameActivityfollowing (I only posted the relevant parts): public class GameActivity extends AppCompatActivity { // initiate variables private GameView mGameView; private Display mDisplay; private Point mSize; public static int w

Can't draw on html5 canvas

who I am I created a Hi dpi canvas and then tried to draw the box, but something went wrong. Why can't I paint on canvas, can someone help me? No error occurs, the plot function is executing, but I have no results var HiDPICanvas = function(container_id, color

Can't draw square on canvas using setInterval?

User 10870573 Hey guys, I'm trying to use setInterval to draw a square on the canvas every 70ms. Interval is set to call function draw()this function, which contains the fillRect() method. But I can't get it to work any ideas? Oh, I tried clearRect() but it di

Why can't I draw a square on the canvas?

Lords Hey, I'm making a snake game in JS, and now all I want to do is draw a snake in the center of the canvas. I've set the canvas size to the board size, so everything scales correctly, but nothing shows up. Any help would be greatly appreciated :) //declare

Can't draw editText in Android canvas

User 3858843 I am trying to add editText box to my android project. This project is almost entirely done in Java (almost no XML), so I would like to know how to do this in Java. The implementation I'm currently encountering the runtime error with is the follow

Can't draw editText in Android canvas

User 3858843 I am trying to add editText box to my android project. This project is almost entirely done in Java (almost no XML), so I would like to know how to do this in Java. The implementation I'm currently encountering the runtime error with is the follow

Can't draw on html5 canvas

who I am I created a Hi dpi canvas and then tried to draw the box, but something went wrong. Why can't I paint on canvas, can someone help me? No error occurs, the plot function is executing, but I have no results var HiDPICanvas = function(container_id, color

Can't draw proper circle using canvas

Xiajin In any case, I can't get a proper circle to draw. I am always oval. Here is sample code to show you what I mean: function canvasClicked(number) { var c = "canvas" + number; var canvas = document.getElementById(c); var context = canvas.getContext("

Android Canvas drawLine can't draw on MainActivity

Broadwell I want to draw a line on Main Activity using Canvas. The problem is, it doesn't draw anything. I have the following code: Bitmap bitmap = Bitmap.createBitmap(1920, 1080, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = ne

Canvas draw text - can't get string

Italy String str = new String(); str = "111"; x = width / 2; y = Math.round(height / 1.3); while (pressed != true){ if (!holder.getSurface().isValid()) continue; Canvas c = holder.lockCanvas(); c.drawBitmap(galaxy, 0, 0, null); c.draw

Delphi: Can't draw on panel's canvas

clothing I'm trying to draw an image on a TPanel's canvas following the procedure for this panel. When I do this in the "Paint Method" of the panel, it works fine, but when I try to paint on the Canvas in another procedure or in the constructor, nothing happen

Can't draw on html5 canvas

who I am I created a Hi dpi canvas and then tried to draw the box, but something went wrong. Why can't I paint on canvas, can someone help me? No error occurs, the plot function is executing, but I have no results var HiDPICanvas = function(container_id, color