Advertisement
Guest User

Untitled

a guest
Apr 24th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from tkinter import Tk, Canvas, BOTH, SW
  5. import csv
  6.  
  7. def csv_einlesen(file_name):
  8.     data = []
  9.     with open(file_name) as csvfile:
  10.         reader = csv.reader(csvfile, delimiter=';')
  11.         for line in reader:
  12.             data.append(line)
  13.     return data
  14.  
  15. def calculate_scaling_factors(data, width, height):
  16.     x_coords = [float(item[4]) for item in data[1:]]
  17.     y_coords = [float(item[5]) for item in data[1:]]
  18.     x_max = max(x_coords)
  19.     x_min = min(x_coords)
  20.     y_max = max(y_coords)
  21.     y_min = min(y_coords)
  22.     scaling_x = width/(x_max-x_min)
  23.     scaling_y = height/(y_max-y_min)
  24.     n_x = - scaling_x * x_min
  25.     n_y = - scaling_y * y_min
  26.     return scaling_x, scaling_y, n_x, n_y
  27.  
  28. def print_data_on_canvas(data, canvas, scaling_x, scaling_y, n_x, n_y):
  29.     hauptbahnhoefe = [item for item in data[1:] if 'Hbf' in item[2]]
  30.     bahnhoefe = [item for item in data[1:] if 'Hbf' not in item[2]]
  31.     for _, _, name, _, x, y, _, _ in bahnhoefe:
  32.         x = float(x)*scaling_x + n_x + 20
  33.         y = float(canvas.cget("height")) - (float(y)*scaling_y + n_y) + 20
  34.         canvas.create_oval(x, y, x+4, y+4,
  35.                             outline='blue', fill='black', width=1)
  36.     for _, _, name, _, x, y, _, _ in hauptbahnhoefe:
  37.         x = float(x)*scaling_x + n_x + 20
  38.         y = float(canvas.cget("height")) - (float(y)*scaling_y + n_y) + 20
  39.         canvas.create_oval(x, y, x+4, y+4,
  40.                             outline='red', fill='black', width=1)
  41.         canvas.create_text(x, y, fill='red', text=name[:3],
  42.                             anchor=SW, activefill="yellow")
  43.  
  44.  
  45. def main():
  46.     width = 600
  47.     height = 800
  48.     data = csv_einlesen('bahnhoefe.csv')
  49.     scaling_x, scaling_y, n_x, n_y = calculate_scaling_factors(data, width,
  50.                                                                height)
  51.     root = Tk()
  52.     canvas = Canvas(root, width = width, height = height, background='white')
  53.     canvas.pack(fill=BOTH, expand=1)
  54.     print_data_on_canvas(data, canvas, scaling_x, scaling_y, n_x, n_y)
  55.     root.geometry('{}x{}'.format(width+40,height+40))
  56.     root.title('Bahnhöfe Deutschlandweit')
  57.     root.mainloop()
  58.  
  59. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement