1

在下面的代码中,我无法让图表以特定的延迟更新,因此看起来好像在线更新,我使用 after 方法调用了一个函数,它会给它新的,但我仍然得到了整个图表有一次,我也试过time.sleep,即使那不起作用

#!/usr/bin/python

from numpy import *
from Tkinter import *
import time
from functools import partial


class Gui():



    def __init__(self,parent):


        self.parent=parent
        mL=[]
        mV=[]
        for i in range(0,10):
            ml_val=10 + i*0.1
            mv_val=1000 + i
            mL.append(ml_val)
            mV.append(mv_val)    
        frame = Frame(self.parent, width=900, height=900, bd=1)
        frame.pack()

        iframe5 = Frame(frame, bd=2,width=800, height=800, relief=RAISED)
        iframe5.pack( pady=10, padx=5)
        self.w = Canvas(iframe5, bg='white', width=600, height=510)
        self.w.pack()

        self.w.create_rectangle(1,1, 599, 509, fill="cyan")

        self.w.create_line(50,460,50,70,fill="#222")
        self.w.create_line(50, 460, 500, 460, fill="#222")
        self.w.create_text(400,55,text="mL vs mV Graph")
        self.w.create_text(390,75,text="mL : X - axis ")
        self.w.create_text(390,95,text="mV : Y - axis ")
        self.w.create_text(30,55,text="Y-Axis")
        self.w.create_text(530,480,text="X-Axis")
        y_range=450
        x_range=90

        self.y_plot=[]
        self.x_plot=[]
        for i in range(0,10):
            self.w.create_text(20,y_range,text=mV[i])
            self.w.create_text(x_range,480,text=mL[i])
            self.x_plot.append(x_range)
            self.y_plot.append(y_range)
            self.w.create_line(40,y_range,50,y_range, fill="blue")
            self.w.create_line(x_range,460,x_range,470, fill="blue")
            y_range -=40
            x_range +=40
       # time.sleep(10)
        for i in range(0,9):
            root.after(1000, partial(self.create_graph,i))        
            time.sleep(1)
    def create_graph(self,i):

        self.w.create_line(self.x_plot[i],self.y_plot[i],self.x_plot[i+1],self.y_plot[i+1], fill="blue")



if __name__== "__main__":
    root = Tk()
 #   root.attributes('-fullscreen',True)
    app=Gui(root)
    root.mainloop()
4

1 回答 1

2

使用after(1000, ...)所有点,您将安排它们在__init__完成后 1000 毫秒绘制,即它们都在完全相同的时间绘制。添加sleep仅会延迟__init__完成的时间,从而延迟显示 UI 的时间。

相反,您应该在以下时间安排不同的时间点__init__

def __init__(self, parent):
    # other initialization code
    for i in range(0, 9):
        root.after(1000 * i, partial(self.create_graph, i))        

或者,将一个after放在末尾__init__以安排绘制第一个点,另一个after放在create_graph绘制下一个点的方法中。

def __init__(self, parent):
    # other initialization code
    # at the end, remove the for loop and instead call after
    root.after(1000, partial(self.create_graph, 0, 9))        

def create_graph(self, i, k):
    self.w.create_line(self.x_plot[i],self.y_plot[i],self.x_plot[i+1],self.y_plot[i+1], fill="blue")
    # call after again to schedule drawing more points
    if i < k:
        root.after(1000, partial(self.create_graph, i+1, 9))        
于 2015-03-25T12:31:53.567 回答