0
  1. I am trying to plot two graph in Tkinter canvas.
  2. Problem is after few plots of data, graph gets hanged. It takes up huge resources of window, then i have to kill it forcefully.
  3. Since I have to plot the graph continuously like that in CRO/DSO so I have to go line by line rather than using any library. Didn't find any lib where graph is printed continuously.
  4. I am using python 2.7 on window 8 64 bit.
  5. But in the end I have to shift the code to Raspberry pi board.

code: from Tkinter import * import Tkinter import time from random import randint import math

    #global vars
    hor_pixel = 5                      #starting point of horizontal pixel
    old_ver_pixel_1 = 0                #last pixel value of graph 1
    old_ver_pixel_2 = 0                #last pixel value of graph 2
    new_ver_pixel_1 = 0                #new pixel value of graph 1
    new_ver_pixel_2 = 0                #new pixel value of graph 2
    y0_1 = 0                           # y cordinate of graph 1 & 2
    y1_1 = 0
    y0_2 = 0
    y1_2 = 0

    def pixel(C):
        global hor_pixel
        global old_ver_pixel_1
        global old_ver_pixel_2    
        global new_ver_pixel_1
        global new_ver_pixel_2
        global y0_1
        global y1_1
        global y0_2
        global y1_2    

        if(new_ver_pixel_1 == old_ver_pixel_1):
            new_ver_pixel_1 = new_ver_pixel_1 + 1
        if(new_ver_pixel_2 == old_ver_pixel_2):
            new_ver_pixel_2 = new_ver_pixel_2 + 1            

        if(new_ver_pixel_1 > old_ver_pixel_1):
            y0_1 = old_ver_pixel_1
            y1_1 = new_ver_pixel_1
        else:
            y0_2 = old_ver_pixel_2
            y1_2 = new_ver_pixel_2        

        coord = hor_pixel, y0_1, hor_pixel, y1_1
        coord2 = hor_pixel, y0_2, hor_pixel, y1_2        
        hor_pixel = hor_pixel + 1

        if(hor_pixel > 400):
            hor_pixel = 5;

        old_ver_pixel_1 = new_ver_pixel_1
        old_ver_pixel_2 = new_ver_pixel_2

        C.create_line(hor_pixel , 0 , hor_pixel , 500, fill = 'black')
        C.create_line(coord, fill = 'red')
        C.create_line(coord2, fill = 'yellow')
        C.pack()
        #print(new_ver_pixel_1 , new_ver_pixel_2)



    def graph():
        global new_ver_pixel_1
        global new_ver_pixel_2

        screen = Tkinter.Tk()
        screen.title("Analog Channel")
        screen.geometry("800x800")
        C = Tkinter.Canvas(screen , bg = "black" , height = 800, width = 800)
        C.pack()

        while True:
            var = 0
            var = var + 1
            new_ver_pixel_1 = randint(0,200) + 200;
            new_ver_pixel_2 = randint(0,200) + 400;
            #print(new_ver_pixel_1 , new_ver_pixel_2)
            #pixel(C)
            screen.after(100,pixel(C))
            screen.update_idletasks()

        screen.mainloop()


    graph()
0

1 Answer 1

1

This statement doesn't do what you think it does:

screen.after(100,pixel(C))

What you think it does is runs pixel(C) every 100ms, but what it actually does is run pixel(C) immediately, and then schedules None to execute in 100ms. This is likely why it is chewing up all your CPU and eventually hanging.

One fix is to change it to screen.after(100, pixel, C). However, a custom infinite loop is the wrong way to run a function every 100ms with Tkinter.

What you need to do instead is remove your infinite loop, and leverage the existing infinite loop (mainloop()). Move the body of your while loop to a function, and have that function put another call to itself on the event queue using after. It will do work, then schedule itself to run again in 100 ms, forever. You can put a check on a global flag if you want to be able to pause or cancel the redrawing.

It looks something like this:

def updateGraph(canvas):
    <do the work up updating the graph>
    canvas.after(100, updateGraph, canvas)
Sign up to request clarification or add additional context in comments.

4 Comments

screen.after(100,(lambda :pixel(C)))
@SDilmac: I don't know what the point of your comment is. Yes, you can do it that way, but the way I wrote works, too.
Update with argument(s) canvas.after(100, updateGraph, canvas) only function , not allowed with arguments
@SDilmac: you are incorrect. after absolutely does accept extra arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.