在tkinter中创建一个简单的应用程序以显示地图

Shu*_*m R 10 python tkinter

我是Tkinter的新手,

我有一个程序,它将CSV作为输入,包含outlet的地理位置,将其显示在地图上,并将其另存为HTML.

我的csv的格式:

outlet_code   Latitude    Longitude
 100           22.564      42.48
 200           23.465      41.65
 ...       and so on        ...
Run Code Online (Sandbox Code Playgroud)

下面是我的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
Run Code Online (Sandbox Code Playgroud)

这将显示 map_osm

替代方法是保存map_osm为HTML

map_osm.save('path/map_1.html')
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是一个GUI,它将做同样的事情.

即提示用户输入CSV,然后执行下面的代码并显示结果或至少将其保存在某个位置.

任何线索都会有所帮助

Mik*_*SMT 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()
Run Code Online (Sandbox Code Playgroud)

通过这个例子,您应该能够弄清楚如何打开文件并在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()
Run Code Online (Sandbox Code Playgroud)