Python错误;图形挂起; Tkinter

时间:2015-07-14 08:01:38

标签: python tkinter tkinter-canvas

  1. 我试图在Tkinter画布上绘制两个图形。
  2. 问题是在几个数据图之后,图表被绞死。 它占用了巨大的窗口资源,然后我必须强行杀死它。
  3. 因为我必须像CRO / DSO那样连续绘制图形,所以我必须逐行而不是使用任何库。没有找到任何连续打印图表的库。
  4. 我在64位窗口上使用python 2.7。
  5. 但最后我必须将代码转移到Raspberry pi board。
  6. 代码:         来自Tkinter进口*         导入Tkinter         进口时间         来自随机导入randint         导入数学

        #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()
    

1 个答案:

答案 0 :(得分:1)

此声明并不符合您的想法:

screen.after(100,pixel(C))

您认为它的作用是每100毫秒运行一次pixel(C),但它实际上做的是立即运行pixel(C) ,然后安排None执行100毫秒。这可能是为什么它会咀嚼你所有的CPU并最终挂起。

一种解决方法是将其更改为screen.after(100, pixel, C)然而,自定义无限循环是使用Tkinter每100ms运行一个函数的错误方法。

您需要做的是删除无限循环,并利用现有的无限循环(mainloop())。将while循环的主体移动到一个函数,并让该函数使用after在事件队列上再次调用自身。它会工作,然后安排自己在100毫秒内再次运行,永远。如果您希望能够暂停或取消重绘,则可以检查全局标记。

它看起来像这样:

def updateGraph(canvas):
    <do the work up updating the graph>
    canvas.after(100, updateGraph, canvas)