Author Topic: Python error; graph hangs;Tkinter  (Read 2327 times)

0 Members and 1 Guest are viewing this topic.

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 491
Python error; graph hangs;Tkinter
« on: July 14, 2015, 07:51:14 am »
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: code

Code: [Select]
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()

« Last Edit: July 14, 2015, 08:00:15 am by Vindhyachal.takniki »
 

Offline Hideki

  • Frequent Contributor
  • **
  • Posts: 256
  • Country: no
Re: Python error; graph hangs;Tkinter
« Reply #1 on: July 14, 2015, 08:23:12 pm »
Could you please stop asking new questions and start posting some responses in all your other threads?
 

Offline Muxr

  • Super Contributor
  • ***
  • Posts: 1375
  • Country: us
Re: Python error; graph hangs;Tkinter
« Reply #2 on: July 15, 2015, 04:53:29 am »
you're using screen.after() wrong, RTFM

Never 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-top

Quote
The callback is only called once for each call to this method. To keep calling the callback, you need to reregister the callback inside itself

so 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.
« Last Edit: July 15, 2015, 07:05:02 am by Muxr »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf