11

I am new to Tkinter,

I have a program which takes CSV as input containing, outlet's geo-location, display it on a map, saving it as HTML.

format of my csv:

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

Below is my python code to take this CSV and put it on a map.

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

This will take display map_osm

Alternate way is to save map_osm as HTML

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

What I am looking for is a GUI which will do the same thing.

i.e prompt user to input the CSV, then execute my code below and display result or at least save it in a location.

Any leads will be helpful

Fazle Rabbi
  • 1
  • 4
  • 16
Shubham R
  • 7,382
  • 18
  • 53
  • 119
  • Stackoverflow isn't for providing "leads". As written this question is too broad. – Bryan Oakley Oct 06 '17 at 12:23
  • So, if I'm understanding you correctly. What you want is a way to have an image be overlaid with a list of coordinates from a CSV and then save that back as it's own image? – Ethan Field Oct 06 '17 at 14:04
  • @EthanField The library folium takes coordinates from my dataframe and plot it on a map(which is in html format). – Shubham R Oct 09 '17 at 05:03
  • So what exactly do you need your program to do? – Ethan Field Oct 09 '17 at 08:39
  • @EthanField an upload button which takes csv from the user, then execute my python code, which takes csv in a dataframe and process it further, and save it in a directory! i dont need to display a map(if its difficult) i will manually go to directory and open the html. just need a response that the file is saved in 'location specified' – Shubham R Oct 09 '17 at 08:53
  • So what do you need an answer from us on? You seem pretty clear on how to do this, what are we needed for? – Ethan Field Oct 09 '17 at 08:54
  • @EthanField i am new to tkinter, i tried to create upload button but was not able to upload csv. Just need a brief code. – Shubham R Oct 09 '17 at 08:56
  • 1
    Brief code for what? What does the upload button need to do? Where does it need to upload to? In what form? From where? Nothing about this question is clear. – Ethan Field Oct 09 '17 at 08:57
  • 2
    Some of the leads can be found [here](http://effbot.org/tkinterbook/) and [there](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html). It's clear what you ask, but it's unclear why. `tkinter` is a well documented and explained library with many and more of examples around the net! Don't be afraid of something new, improve yourself with knowledge, just do it! It's unclear with what you're struggling (laziness?) The only one point about why your question is still unanswered - nobody wants to lower the bar of the SO (by doing your job for you). Maybe for the right price they could... – CommonSense Oct 09 '17 at 13:45
  • 1
    Just start a `filedialog` via "upload" button, [get a file path](https://stackoverflow.com/a/28373515/6634373), do some basic checks on the result, proceed with your code. Is it hard? Just start from a scratch and you will feel how things starting to get easier! – CommonSense Oct 09 '17 at 13:59
  • What you are asking to do is not hard. Maybe 10 to 20 lines of extra code for the GUI. – Mike - SMT Oct 09 '17 at 14:30

1 Answers1

6

You question would be better received if you had provided any code you attempted to write for the GUI portion of your question. I know (as well as everyone else who posted on your comments) that tkinter is well documented and has countless tutorial sites and YouTube videos.

However if you have tried to write code using tkinter and just don't understand what is going on, I have written a small basic example of how to write up a GUI that will open a file and print out each line to the console.

This won't right out answer your question but will point you in the right direction.

This is a non-OOP version that judging by your existing code you might better understand.

# 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()

With this example you should be able to figure out how to open your file and process it within a tkinter GUI.

For a more OOP option:

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()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79