Python错误;图形挂起;Tkin

2024-04-25 20:20:57 发布

您现在位置:Python中文网/ 问答频道 /正文

  1. 我正试着在特金特的画布上画两幅图。你知道吗
  2. 问题是在几幅数据之后,图形被挂起。 它占用了大量的窗口资源,所以我不得不将其强行杀掉。你知道吗
  3. 因为我必须像CRO/DSO那样连续地绘制图形,所以我必须逐行而不是使用任何库。没有找到任何连续打印图形的库。你知道吗
  4. 我在windows8 64位上使用python2.7。你知道吗
  5. 但最后我不得不将代码转移到Raspberry pi板。你知道吗

代码: 从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()

Tags: of图形newifvaluetkinterscreenglobal
1条回答
网友
1楼 · 发布于 2024-04-25 20:20:57

这句话并不像你想象的那样:

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)

相关问题 更多 >