Python error; graph hangs;Tkinter -
- i trying plot 2 graph in tkinter canvas.
- problem after few plots of data, graph gets hanged. takes huge resources of window, have kill forcefully.
- since have plot graph continuously in cro/dso have go line line rather using library. didn't find lib graph printed continuously.
- i using python 2.7 on window 8 64 bit.
- but in end have shift code raspberry pi board.
code: tkinter import * import tkinter import time 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()
this statement doesn't think does:
screen.after(100,pixel(c))
what think runs pixel(c)
every 100ms, run pixel(c)
immediately, , schedules none
execute in 100ms. why chewing cpu , hanging.
one fix change screen.after(100, pixel, c)
. however, custom infinite loop wrong way run function every 100ms tkinter.
what need instead remove infinite loop, , leverage existing infinite loop (mainloop()
). move body of while loop function, , have function put call on event queue using after
. work, schedule run again in 100 ms, forever. can put check on global flag if want able pause or cancel redrawing.
it looks this:
def updategraph(canvas): <do work updating graph> canvas.after(100, updategraph, canvas)
Comments
Post a Comment