Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from tkinter import Tk, Canvas, BOTH, SW
- import csv
- def csv_einlesen(file_name):
- data = []
- with open(file_name) as csvfile:
- reader = csv.reader(csvfile, delimiter=';')
- for line in reader:
- data.append(line)
- return data
- def calculate_scaling_factors(data, width, height):
- x_coords = [float(item[4]) for item in data[1:]]
- y_coords = [float(item[5]) for item in data[1:]]
- x_max = max(x_coords)
- x_min = min(x_coords)
- y_max = max(y_coords)
- y_min = min(y_coords)
- scaling_x = width/(x_max-x_min)
- scaling_y = height/(y_max-y_min)
- n_x = - scaling_x * x_min
- n_y = - scaling_y * y_min
- return scaling_x, scaling_y, n_x, n_y
- def print_data_on_canvas(data, canvas, scaling_x, scaling_y, n_x, n_y):
- hauptbahnhoefe = [item for item in data[1:] if 'Hbf' in item[2]]
- bahnhoefe = [item for item in data[1:] if 'Hbf' not in item[2]]
- for _, _, name, _, x, y, _, _ in bahnhoefe:
- x = float(x)*scaling_x + n_x + 20
- y = float(canvas.cget("height")) - (float(y)*scaling_y + n_y) + 20
- canvas.create_oval(x, y, x+4, y+4,
- outline='blue', fill='black', width=1)
- for _, _, name, _, x, y, _, _ in hauptbahnhoefe:
- x = float(x)*scaling_x + n_x + 20
- y = float(canvas.cget("height")) - (float(y)*scaling_y + n_y) + 20
- canvas.create_oval(x, y, x+4, y+4,
- outline='red', fill='black', width=1)
- canvas.create_text(x, y, fill='red', text=name[:3],
- anchor=SW, activefill="yellow")
- def main():
- width = 600
- height = 800
- data = csv_einlesen('bahnhoefe.csv')
- scaling_x, scaling_y, n_x, n_y = calculate_scaling_factors(data, width,
- height)
- root = Tk()
- canvas = Canvas(root, width = width, height = height, background='white')
- canvas.pack(fill=BOTH, expand=1)
- print_data_on_canvas(data, canvas, scaling_x, scaling_y, n_x, n_y)
- root.geometry('{}x{}'.format(width+40,height+40))
- root.title('Bahnhöfe Deutschlandweit')
- root.mainloop()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement