Python error; graph hangs;Tkinter - Page 1
Forum section: electronics / beginners

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: July 14, 2015, 07:51:14 am
content: 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.Edit: codeCode:from Tkinter import *import Tkinterimport timefrom random import randintimport math#global varshor_pixel = 5 #starting point of horizontal pixelold_ver_pixel_1 = 0 #last pixel value of graph 1old_ver_pixel_2 = 0 #last pixel value of graph 2new_ver_pixel_1 = 0 #new pixel value of graph 1new_ver_pixel_2 = 0 #new pixel value of graph 2y0_1 = 0 # y cordinate of graph 1 & 2y1_1 = 0y0_2 = 0y1_2 = 0def pixel(C):global hor_pixelglobal old_ver_pixel_1global old_ver_pixel_2global new_ver_pixel_1global new_ver_pixel_2global y0_1global y1_1global y0_2global y1_2if(new_ver_pixel_1 == old_ver_pixel_1):new_ver_pixel_1 = new_ver_pixel_1 + 1if(new_ver_pixel_2 == old_ver_pixel_2):new_ver_pixel_2 = new_ver_pixel_2 + 1if(new_ver_pixel_1 > old_ver_pixel_1):y0_1 = old_ver_pixel_1y1_1 = new_ver_pixel_1else:y0_2 = old_ver_pixel_2y1_2 = new_ver_pixel_2coord = hor_pixel, y0_1, hor_pixel, y1_1coord2 = hor_pixel, y0_2, hor_pixel, y1_2hor_pixel = hor_pixel + 1if(hor_pixel > 400):hor_pixel = 5;old_ver_pixel_1 = new_ver_pixel_1old_ver_pixel_2 = new_ver_pixel_2C.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_1global new_ver_pixel_2screen = Tkinter.Tk()screen.title("Analog Channel")screen.geometry("800x800")C = Tkinter.Canvas(screen , bg = "black" , height = 800, width = 800)C.pack()while True:var = 0var = var + 1new_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()

author: Hideki
author_rank: Frequent Contributor
timestamp: July 14, 2015, 08:23:12 pm
content: Could you please stop asking new questions and start posting some responses in all your other threads?

author: Muxr
author_rank: Super Contributor
timestamp: July 15, 2015, 04:53:29 am
content: you're using screen.after() wrong, RTFMNever used Tkinter and it took me 1 minute to figure out what's wrong with your code.https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method?answertab=active#tab-topQuoteso after() needs to be called in pixel(). Or actually keep reading. You should probably have a redraw() (pick a different name if you want) wrapper function which implements the after() pattern properly.Also you call the mainloop() at the end. How is the program supposed to ever execute it if there is a while True: loop? Pretty sure it's redundant, since update_idletasks() handles drawing and that while True: loop never exits.Actually you have to refactor your while True loop entirely, it should go away since you're creating some sort of a cartesian product with two loops.Basically you have one loop, the while True loop, which is registering millions of after() events. But after() if implemented properly is a loop on its own. So it's not at all surprising you're running out of resources.Use the proper form. As shown in that example I linked.