0

Estoy tratando de dibujar una linea a modo de gráfica en tiempo real que despliegue un vector de datos con tkinter, el problema es que usaba un while(true) pero por lo visto tiene conflicto con el mainloop, investigue y decían que puede usarse la función after() así que metí lo del while en una función y lo use con after, pero al integrarla a mi código hace lo mismo y solo despliega una línea y se queda ahí, ¿Cómo puedo hacer que continúe con el ciclo del código para desplegar la gráfica?. El código que use es este:

from tkinter import *
from tkinter import ttk
import ctypes

raiz=Tk()
raiz.config(bg='blue')
raiz.title('Monitor V1.1')
Fr=Frame()
Fr.pack()
Fr.pack(fill="both", expand="True")
Fr.config(bg='black')

trazo1= Canvas(Fr, width=1000, height=200, background="black", 
highlightthickness=0)
trazo1.grid(row=0, column=0)

Irojo=[-0.33, -0.35, -0.37, -0.38, -0.39, -0.41, -0.42, -0.44, -0.47, -0.5, -0.53, -0.56, -0.58, -0.6, -0.62, -0.63, -0.64, -0.65, -0.66, -0.67, -0.67, -0.68, -0.69, -0.7, -0.7, -0.67, -0.61, -0.53, -0.42, -0.3, -0.16, -0.02, 0.11, 0.23, 0.33, 0.41, 0.46, 0.5, 0.51, 0.51, 0.49, 0.47, 0.45, 0.43, 0.41, 0.38, 0.36, 0.35, 0.33, 0.32, 0.31, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.29, 0.28, 0.27, 0.25, 0.23, 0.21, 0.18, 0.16, 0.13, 0.1, 0.07, 0.04, 0.01, 0.0, -0.03, -0.06, -0.08, -0.11, -0.13, -0.16, -0.18, -0.2, -0.22, -0.23, -0.25, -0.26, -0.27, -0.29, -0.3, -0.32, -0.34, -0.36, -0.38, -0.4, -0.42, -0.45, -0.47, -0.49, -0.5, -0.48, -0.44, -0.38, -0.29, -0.18, -0.06, 0.05, 0.16, 0.27, 0.35, 0.42, 0.47, 0.51, 0.53, 0.54, 0.53, 0.52, 0.5, 0.47, 0.44, 0.41, 0.38, 0.36, 0.34, 0.32, 0.31, 0.31, 0.3, 0.3, 0.29, 0.29, 0.28, 0.27, 0.26, 0.25, 0.23, 0.21, 0.18, 0.15, 0.12, 0.09, 0.06, 0.04, 0.01, -0.01, -0.04, -0.07, -0.1, -0.12, -0.15, -0.18, -0.21, -0.24, -0.27, -0.29, -0.31, -0.32, -0.33, -0.34, -0.35, -0.37, -0.38, -0.4, -0.42, -0.44, -0.46, -0.48, -0.49, -0.51, -0.52, -0.52, -0.53, -0.54, -0.54, -0.54, -0.52, -0.47, -0.41, -0.31, -0.2, -0.07, 0.06, 0.19, 0.31, 0.41, 0.49, 0.54, 0.58, 0.59, 0.6, 0.59, 0.59, 0.58, 0.57, 0.57, 0.56, 0.54, 0.53, 0.52, 0.52, 0.51, 0.5, 0.5, 0.49, 0.47, 0.45, 0.43, 0.4, 0.37, 0.35, 0.32, 0.3, 0.27, 0.25, 0.24, 0.22, 0.2, 0.18, 0.16, 0.13, 0.11, 0.08, 0.05, 0.03, 0.0, -0.01, -0.03, -0.06, -0.09, -0.12, -0.15, -0.17, -0.19, -0.21, -0.23, -0.25, -0.27, -0.3, -0.32, -0.34, -0.36, -0.38, -0.4, -0.42, -0.44, -0.46, -0.47, -0.49, -0.5, -0.51, -0.51, -0.5, -0.48, -0.44, -0.37]

w=1000
h=200
antL=[]

for i in range(w):
    antL.append(trazo1.create_line(0,0,0,0))

def plet(self):
    x=0
    ant=0
    i=0
    x=(x+1)%w
    valor=(h/2)-Irojo[i]*100
    trazo1.delete(antL[x])
    antL[x]=trazo1.create_line(x-1,ant,x,valor,fill="yellow")
    ant=valor
    i=i+1
    if i > len(Irojo)-1:
        i=0
    raiz.after(1000, plet)

raiz.after(1000, plet)

raiz.mainloop()

1 respuesta 1

1

Lorena, la función after(), ejecuta una función (llamada normalmente "callback"), luego del tiempo específicado. En tu caso, la línea:

raiz.after(1000, plet)

Ejecutará la función plet, después de 1000 milisegundos, de haber llegado a ésta instrucción.

1
  • Gracias, ajuste el tiempo del after y encontré un pequeño problema con mis variables dentro de la función, creo que ese era el problema por que reiniciaba automáticamente los valores, las hice globales y quedo bien. Gracias por la ayuda!
    – Lorena
    el 26 feb. 2019 a las 15:26

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.