1

I want the user to submit their csv excel file and choose the columns from a dropdown menu he wants for analysis.

import pandas as pd 
import os 
import sys
from tkinter import *

root = Tk()
root.title('Eng3')

filepath = input('Enter filepath: ')    
assert os.path.exists(filepath), "I did not find the file at, " + str(filepath)
f = open(filepath, 'r+')
print("Hooray we found your file!")   
f.close()   

file = pd.read_csv(filepath, encoding='latin1', delimiter=',')   
column_list = file.columns.tolist()
print(column_list)

So I made the columns names from the excel file into a list. How can I make a dropdown menu from this list(column_list) to show all column names? When I tried:

tkvar = StringVar(column_list)
menu = OptionMenu(root, tvkar, column_list)

I get this error:

AttributeError: 'list' object has no attribute '_root'
Moon
  • 305
  • 1
  • 2
  • 11

1 Answers1

0

I looked around and found this post How can I create a dropdown menu from a List in Tkinter?. Very useful

file = pd.read_csv(filepath, encoding='latin1', delimiter=',')  
column_list = file.columns.tolist() #convert pandas dataframe to simple python list    

OPTIONS = column_list  #this is what solved my problem

master = Tk()
master.title('Eng3')
variable = StringVar(master)
variable.set(OPTIONS[0]) # default value


w = OptionMenu(master, variable, *OPTIONS)
w.pack()

def ok():
  print ("value is:" + variable.get())

button = Button(master, text="OK", command=ok)
button.pack()
Moon
  • 305
  • 1
  • 2
  • 11