2016-10-14 8 views
0

テキストファイルに保存されている角度に従って車両の位置を示すキャンバスを作成するTkinterプログラムを作成しています。私が持っている問題は、プログラムが開始時にのみテキストファイルを読み込み、検査を続行しないということです。私はそれがかなりシンプルでなければならないことを知っていますが、私は多くのサイトを見ており、これを行う方法を見つけることができません。ここに私のコードは、使用root.after(miliseconds, function_name)が再度ファイルを読み込み、キャンバス上に線を移動しますどの機能を定期的に実行するためにテキストファイルに基づいてTkinterの行の位置を更新しようとしています

  from Tkinter import * 
      import math 
      import time 

      root = Tk() 
      root.geometry("800x480") 
      root.configure(background='darkgrey') 
      content = Frame(root,) 

      #Dimensions of Window 
      w = 800 
      h = 480 

      #Get Angle 
      file_angle = open("current_angle.txt") 
      #read file and remove "/n" from the end of the string 
      string_angle = ((file_angle.read())[:-2]) 
      #Convert string to a number 
      read_angle = float(string_angle) 

      if read_angle > 90 or read_angle <-90: 
       deg_angle = read_angle - 180 

      else: 
       deg_angle = read_angle 

      angle = math.radians(deg_angle) 
      swath_width = w/5 

      if deg_angle > -90 and deg_angle < 90: 
       #Center Line 
       centertopx = (w/2) + ((h/1.5) * math.tan(angle)) 
       centertopy = 0 
       centerbottomx = (w/2) + ((h - (h/1.5)) * math.tan(-1 * angle)) 
       centerbottomy = h 

      if deg_angle == 90 or deg_angle == -90: 
       centertopx = 0 
       centertopy = h/2 
       centerbottomx = w 
       centerbottomy = h/2 

      #Create Guidance Map 
      livemap = Canvas(root, width=w, height=h) 
      #Outline of map 
      livemap.create_rectangle(0,0,w,h, outline="black", width=5, fill="#9E9E9E") 


      #Drawing lines 
      livemap.create_line(centertopx, centertopy, centerbottomx, centerbottomy, fill="red", width=4) 

      #stationary parts of map 
      #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) 

      #Put canvas into window 
      livemap.grid(column=0, row=0, columnspan=4, rowspan=4) 

      root.mainloop() 
+0

'root.after(miliseconds、function_name)'を使用すると、関数を定期的に実行してファイルを再度読み取ることができます。 BTW:ファイルをもう一度読み込み、それを再び開いて開くか、file_angle.seek(0)を使ってファイルの先頭に移動する必要があります。 – furas

答えて

0

です。

コードでは、1回だけ(車両の前に)行を作成し、後で行の位置を変更します。

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() 
+0

ありがとうございます。これは完全に機能しました。 – user7016824

関連する問題