2017-10-06 71 views
10

我是新来的Tkinter,创建Tkinter的一个简单的应用程序,用于显示地图

我有一个程序,它采用CSV含输入,输出的地理位置, 显示它在地图上,将其保存为HTML。我的CSV的

格式:

outlet_code Latitude Longitude 
100   22.564  42.48 
200   23.465  41.65 
...  and so on  ... 

下面是我的Python代码借此CSV,并把它在地图上。

import pandas as pd 
import folium 
map_osm = folium.Map(location=[23.5747,58.1832],tiles='https://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}',attr= 'Imagery from <a href="http://giscience.uni-hd.de/">GIScience Research Group @ University of Heidelberg</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>') 

df = pd.read_excel("path/to/file.csv") 
for index, row in df.iterrows(): 
    folium.Marker(location=[row['Latitude'], row['Longitude']], popup=str(row['outlet_code']),icon=folium.Icon(color='red',icon='location', prefix='ion-ios')).add_to(map_osm) 

map_osm 

这将需要显示map_osm

另一种方法是map_osm保存为HTML

map_osm.save('path/map_1.html') 

我所寻找的是一个图形用户界面,这将做同样的事情。

即即时用户输入CSV,然后执行我的代码,并显示结果 或至少将它保存在一个位置。

任何线索将有助于

+0

#1不提供 “线索”。正如书面这个问题太广泛。 –

+0

所以,如果我正确地理解你。你想要的是一种将图像与来自CSV的坐标列表重叠的方式,然后将其保存为自己的图像? –

+0

@EthanField库folium从我的数据框中获取坐标并将其绘制在地图上(html格式)。 – Shubham

回答

5

你的问题会,如果你已经提供您尝试写你的问题的GUI部分的任何代码可以更好地接受。我知道(以及其他所有在你的评论中发表的人)tkinter是有据可查的,并有无数的教程网站和YouTube视频。

但是,如果您已经尝试使用tkinter编写代码,并且只是不明白发生了什么,我已经写了一个小的基本示例来说明如何编写一个GUI来打开文件并将每行打印出来控制台。

这不会正确回答你的问题,但会指出你在正确的方向。

这是一个非OOP版本,根据您可能更好理解的现有代码来判断。

# importing tkinter as tk to prevent any overlap with built in methods. 
import tkinter as tk 
# filedialog is used in this case to save the file path selected by the user. 
from tkinter import filedialog 

root = tk.Tk() 
file_path = "" 

def open_and_prep(): 
    # global is needed to interact with variables in the global name space 
    global file_path 
    # askopefilename is used to retrieve the file path and file name. 
    file_path = filedialog.askopenfilename() 

def process_open_file(): 
    global file_path 
    # do what you want with the file here. 
    if file_path != "": 
     # opens file from file path and prints each line. 
     with open(file_path,"r") as testr: 
      for line in testr: 
       print (line) 

# create Button that link to methods used to get file path. 
tk.Button(root, text="Open file", command=open_and_prep).pack() 
# create Button that link to methods used to process said file. 
tk.Button(root, text="Print Content", command=process_open_file).pack() 

root.mainloop() 

有了这个例子,你应该能够弄清楚如何打开你的文件并在tkinter GUI中处理它。

对于更OOP选项:

import tkinter as tk 
from tkinter import filedialog 

# this class is an instance of a Frame. It is not required to do it this way. 
# this is just my preferred method. 
class ReadFile(tk.Frame): 
    def __init__(self): 
     tk.Frame.__init__(self) 
     # we need to make sure that this instance of tk.Frame is visible. 
     self.pack() 
     # create Button that link to methods used to get file path. 
     tk.Button(self, text="Open file", command=self.open_and_prep).pack() 
     # create Button that link to methods used to process said file. 
     tk.Button(self, text="Print Content", command=self.process_open_file).pack() 

    def open_and_prep(self): 
     # askopefilename is used to retrieve the file path and file name. 
     self.file_path = filedialog.askopenfilename() 

    def process_open_file(self): 
     # do what you want with the file here. 
     if self.file_path != "": 
      # opens file from file path and prints each line. 
      with open(self.file_path,"r") as testr: 
       for line in testr: 
        print (line) 

if __name__ == "__main__": 
    # tkinter requires one use of Tk() to start GUI 
    root = tk.Tk() 
    TestApp = ReadFile() 
    # tkinter requires one use of mainloop() to manage the loop and updates of the GUI 
    root.mainloop() 
相关问题