Tkinter实时图形画布停止更新,直到鼠标移动

时间:2014-12-08 10:02:17

标签: python tkinter animated tkinter-canvas

这是一个来自n00b的Python 2.7问题:

我正在运行一个应用程序,它从串行端口读取数据并使用Tkinter-Canvas生成实时图形。 我没有使用matplotlib,因为这个lib在A20平台上不可用我想运行该程序。 (mpl的某些依赖关系尚未达到A20的必要版本)

我知道这不是绘制实时图形的最优雅的方式,但对我来说它似乎是最兼容的,因为它只是依赖于Tkinter作为lib,它可以在每个平台上使用。 我基本上删除画布并在每次新数据进入时用一个位置偏移重绘它。它有点闪烁,但正如我所说...我是一个n00b。 :)

这是我绘制图表的代码:

class TeleGraph(Canvas):
    def __init__(self, master, FrameTitle, Col, Row, Height):
        Canvas.__init__(self, master)
        self.FrameTitle = FrameTitle
        self.Value1 = range(60)
        self.Value2 = range(60)
        self.Value3 = range(60)
        self.configure(height = Height, width = 750, bg = 'grey', bd = 3,relief = GROOVE)
        self.grid()
        self.grid_propagate(0)
        self.Col = Col
        self.Row = Row
        self.place(y = Col, x = Row)

    def NewEntry(self, NewValue1, NewValue2, NewValue3 = 0, Centerline = False, ThirdLine = False):
        self.delete("all")
        self.create_text( 380,20,text = self.FrameTitle, fill = 'black')
        if Centerline == True:
            self.create_line(0,90,760,90, fill = 'dark grey')
        for x in range(1,59):
            self.Value1[x] = self.Value1[x+1]
            self.Value2[x] = self.Value2[x+1]
            self.Value3[x] = self.Value3[x+1]
        self.Value1[59] = NewValue1
        self.Value2[59] = NewValue2
        self.Value3[59] = NewValue3
        for x in range(0,59):
            self.create_line(-13+(x*13),self.Value1[x],0+(x*13),self.Value1[x+1], fill='blue' )
            self.create_line(-13+(x*13),self.Value2[x],0+(x*13),self.Value2[x+1], fill='red' )
            if ThirdLine == True:
                self.create_line(-13+(x*13),self.Value3[x],0+(x*13),self.Value3[x+1], fill='yellow' )

到目前为止工作正常,但我注意到一个奇怪的错误:

大约半分钟左右,Canvas停止更新,即使程序本身仍然运行良好。当我将鼠标移到应用程序上时,Canvas会再次更新。 我每隔0.5秒调用一次NewEntry函数。 我想过不时地产生一个鼠标事件,但这将是一个令人讨厌的工作...... 任何帮助都非常感谢!

谢谢,

甲板

1 个答案:

答案 0 :(得分:1)

首先我认为一个新的代码解决了这个问题,但事实证明,它只是不经常发生。而且似乎在A20ARM上的Debian上也没有发生。

以下是代码:

class TeleGraph(Canvas):
    def __init__(self, master, FrameTitle, Col, Row, Height):
        Canvas.__init__(self, master)
        self.FrameTitle = FrameTitle
        self.Value1 = range(60)
        self.Value2 = range(60)
        self.Value3 = range(60)
        self.Line1 = range(60)
        self.Line2 = range(60)
        self.Line3 = range(60)
        self.configure(height = Height, width = 750, bg = 'grey', bd = 3,relief = GROOVE)
        self.grid()
        self.grid_propagate(0)
        self.Col = Col
        self.Row = Row
        self.place(y = Col, x = Row)
        self.create_text( 380,20,text = self.FrameTitle, fill = 'black')
        for x in range(0,59):
            self.Line1[x] = self.create_line(-13+(x*13),90,0+(x*13),90, fill='blue', width = 0 )
            self.Line2[x] = self.create_line(-13+(x*13),90,0+(x*13),90, fill='red' , width = 0 )
            self.Line3[x] = self.create_line(-13+(x*13),90,0+(x*13),90, fill='yellow' , width = 0 )

    def NewEntry(self, NewValue1, NewValue2 = 0, NewValue3 = 0, Centerline = False, Dritter = False):
        for x in range(1,59):
            self.Value1[x] = self.Value1[x+1]
            self.Value2[x] = self.Value2[x+1]
            self.Value3[x] = self.Value3[x+1]
        self.Value1[59] = NewValue1
        self.Value2[59] = NewValue2
        self.Value3[59] = NewValue3
        for x in range(0,59):
            self.coords(self.Line1[x], -13+(x*13), self.Value1[x],(x*13), self.Value1[x+1])
            self.coords(self.Line2[x], -13+(x*13), self.Value2[x],(x*13), self.Value2[x+1])
            self.coords(self.Line3[x], -13+(x*13), self.Value3[x],(x*13), self.Value3[x+1])