我正在创建一个Tkinter程序,该程序创建一个画布,根据保存到文本文件的角度显示车辆位置。我遇到的问题是程序只在启动时才读取文本文件,并且不会继续检查。我知道它应该相当简单,但我已经查看了很多网站,但找不到办法。这是我的代码
$ awk '$4==1{$4="NA"} $4==2{$4=$3; $3="NA"} 1' OFS='\t' 1.txt
008 750 16.3763 NA
028 572 NA -91.1915
031 421 -19.2564 NA
113 265 -5.05956 NA
147 099 38.017 NA
154 161 -4.43984 NA
170 101 28.2171 NA
179 728 NA -31.2691
答案 0 :(得分:0)
使用root.after(miliseconds, function_name)
定期运行将再次读取文件并在画布上移动线条的功能。
在代码中我只创建一次行(在车辆之前),然后我只改变行位置。
from Tkinter import *
import math
import time
w = 800
h = 480
# --- functions ---
def update_line():
deg_angle = open("current_angle.txt")
deg_angle = deg_angle.read()[:-2]
deg_angle = float(deg_angle)
if deg_angle > 90 or deg_angle <-90:
deg_angle -= 180
if deg_angle == 90 or deg_angle == -90: # you need
centertopx = 0
centertopy = h / 2
centerbottomx = w
centerbottomy = h / 2
else: # deg_angle > -90 and deg_angle < 90:
angle = math.radians(deg_angle)
#Center Line
centertopx = (w/2) + ((h/1.5)*math.tan(angle))
centertopy = 0
centerbottomx = (w/2) + ((h - (h/1.5))*math.tan(-angle))
centerbottomy = h
# move line
livemap.coords(line, centertopx, centertopy, centerbottomx, centerbottomy)
# repeat after 200 ms (0.2s)
root.after(200, update_line)
# --- main ---
root = Tk()
root.geometry("800x480")
root.configure(background='darkgrey')
content = Frame(root,)
livemap = Canvas(root, width=w, height=h)
livemap.create_rectangle(0, 0, w, h, outline="black", width=5, fill="#9E9E9E")
# create line (without angle) before vehicle
line = livemap.create_line(w/2, 0, w/2, h, fill="red", width=4)
#Triangle that represents vehicle
livemap.create_polygon(
(w/2)-(w*0.04), (h/1.5)+(h*0.1),
(w/2)+(w*0.04), (h/1.5)+(h*0.1),
w/2, h/1.5,
outline="black", fill="darkorange", width=2)
livemap.create_line(w/2, (h/1.5)+(h*0.07), w/2, h/1.5, fill="black", width=2)
livemap.create_polygon(
(w/2)-(w*0.04), (h/1.5)+(h*0.1),
(w/2)+(w*0.04), (h/1.5)+(h*0.1),
w/2, (h/1.5)+(h*0.07),
outline="black", fill="darkorange", width=2)
livemap.grid(column=0, row=0, columnspan=4, rowspan=4)
# update line first time
update_line()
root.mainloop()