简体   繁体   中英

How to display only one column of csv using tkinter and overwrite with next value after 100ms

Contents of csv file:

Latitude(deg), Longitude(deg), Speed(ms-1)
12.8742,13.6543, 0.23,
12.8743,13.6544, 0.25,
12.8744,13.6545, 0.29,
import tkinter
import csv

root = tkinter.Tk()

# open file
with open("File.csv", newline = "") as file:
   reader = csv.reader(file)

   # Reads all rows and columns[enter image description here][1]
   r = 0
   for col in reader:
      c = 0
      for row in col:
         label = tkinter.Label(root, width = 10, height = 2, \
                               text = row, relief = tkinter.RIDGE)
         label.grid(row = r, column = c)
         c += 1
      r += 1

root.mainloop()

Current output:

在此处输入图片说明

Expected result update speed column with next values overwriting the previous value with same size of text box:

在此处输入图片说明

For any kind of loop in tkinter, you should look into the ".after(ms, func)" method, which allows you to specify a loop where values are updated after a number of milliseconds

Here is a minimum working example, just replace the data with your csv file (and catch the StopIteration error when reaching the end of your dataset)

import tkinter as tk
import numpy as np

data = (x for x in np.random.random_sample(1000))

root = tk.Tk()
svar = tk.StringVar()

tk.Label(root, text='Speed(ms-1)').pack()
tk.Label(root, textvariable=svar).pack()

def update_speed():
    svar.set(next(data))
    root.after(500, update_speed)

update_speed()
root.mainloop()

I have modified this code. I need to plot as well as display speed value by tkinter. But below code is working sequentially after displaying all speed values and closing the display box the plotting of latitude and longitude begins. Can I do it parallelly both displaying and plotting has same file ?

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
import numpy as np
import tkinter as tk


HV = pd.read_csv('file.csv')
lat_HV = []
lon_HV = []
speed_HV = []

lat_HV = HV['Latitude(deg)']
lon_HV = HV[' Longitude(deg)']
speed_HV = HV[' Speed(ms-1)']
      
data = iter(speed_HV)

root = tk.Tk()
svar = tk.StringVar()

tk.Label(root, text='Speed(ms-1)').pack()
tk.Label(root, textvariable=svar).pack()

def update_speed():
    svar.set(next(data))
    root.after(100, update_speed)

update_speed()
root.mainloop()

plt.style.use('seaborn')
fig = plt.figure()
ax = fig.add_subplot(1,1,1)

def animation(i):
  HV = pd.read_csv('file.csv')
  lat_HV = []
  lon_HV = []
  speed_HV = []

  lat_HV = HV[0:i]['Latitude(deg)']
  lon_HV = HV[0:i][' Longitude(deg)']
  speed_HV = HV [0:i][' Speed(ms-1)']

  ax.clear()
  plt.title('IMA Scenario')
  plt.xlabel('Longitude')
  plt.ylabel('Latitude')
  ax.scatter(lon_HV, lat_HV,s=30, marker="s",label='HV')
  plt.legend(loc='upper right');

animation = FuncAnimation(fig, func=animation, interval=100)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM