将持续更新的动画情节与其他组件一起放在tkinter gui中

时间:2019-04-01 23:46:31

标签: python matplotlib tkinter graph tkinter-canvas

我想在GUI的页面之一中显示一个图。绘制的数据input = "This is an example" output = re.sub(r'(?<!\w)(?=\w)|(?<=\w)(?!\w)', '@', input) print(output) @This@ @is@ @an@ @example@ 将不断变化。每当theData更改时,theData的值就会写入1。时间和数据的映射在此处:

valueChangedtheData

我已经这样定义了GUI的容器:

xsize=100
xdata,ydata = [],[]
def data_gen():
    t = data_gen.t
    global theData
    global valueChangedtheData
    while True:
        if (valueChangedtheData == 1):
           valueChangedtheData = 0;
           t+=0.1
           val=float(theData);
           if val>1000:
               continue
           yield t, val

        else: pass


def animate(data):
    t, val = data
    if t>-1:
        xdata.append(t)
        ydata.append(val)
        if t>xsize: # Scroll to the left.
            a.set_xlim(t-xsize, t)
        line.set_data(xdata, ydata)
    return line,

def on_close_figure(event):
    sys.exit(0)

data_gen.t = -1
f = plt.figure()
f.canvas.mpl_connect('close_event', on_close_figure)
#f = Figure(figsize=(5,5), dpi=100)
a = f.add_subplot(111)
line, = a.plot([], [], lw=2)
a.set_ylim(0, 250)
a.set_xlim(0, xsize)
a.grid()

和显示图的页面:

class Gui(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill = "both", expand = TRUE)
        container.grid_rowconfigure(0, weight = 1)

        global theData;


        self.MyReading = StringVar()
        self.frames={}

        for F in (StartPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
      frame = self.frames[cont]
      frame.tkraise()

然后为了启动动画和Gui:

class StartPage(Frame):                         #The Graphical Page
    def __init__(self, parent, controller):
        Frame.__init__(self,parent)

        label = Label(self, text="StartPage")
        label.pack()
        label1 = Label(self, textvariable = controller.theData)
        label1.pack

        canvas = FigureCanvasTkAgg(f, self)
        canvas.draw() #changed from show to draw
        canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True)

        toolbar = NavigationToolbar2Tk(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True)

使用root = Gui() update_reading() ani = animation.FuncAnimation(f, animate, data_gen, blit = False, interval=100, repeat = False) root.mainloop() 更新标签:

update_reading()

但是,在页面上添加画布之后,所有依赖于变量类的标签都将拒绝购买,包括def update_reading(): global theData global valueChangedtheData theData = randint(1,20) #This is just an example of the changing value print(theData) valueChangedtheData = 1; root.MyReading.set(str(theData)); root.after(100,update_reading) 的值,但图表正在绘制。同样,显示图像的标签也将拒绝显示。在对画布,数据映射和动画进行注释之后,标签将重新显示。我是否缺少重要的初始化代码?同样,在绘制过程中,GUI窗口中发生了相当大的“缓慢”现象。通过更好的代码编写可以缓解这种情况吗?谢谢

进口:

theData

编辑:添加了缺少和必要的代码 EDIT2:包括所有导入

0 个答案:

没有答案